���� Python �̳�
��9�� ���ݽṹ
��һҳ �б� ��һҳ

�б�

list�Ǵ���һ��������Ŀ�����ݽṹ�����������һ���б��д洢һ�� ���� ����Ŀ����������һ�������б��������������Ҫ��Ķ�����������������б��ˡ�ֻ��������Ĺ�����ϣ�����ÿ������������ռ��һ�У�����Python�У�����ÿ����Ŀ֮���ö��ŷָ

�б��е���ĿӦ�ð����ڷ������У�����Python��֪��������ָ��һ���б���һ���㴴����һ���б�����������ӡ�ɾ�����������б��е���Ŀ��������������ӻ�ɾ����Ŀ������˵�б��� �ɱ�� �������ͣ������������ǿ��Ա��ı�ġ�

��������Ŀ�������

������һֱ�Ƴ����۶�����࣬�������ڶ�������һ����Ϳ���ʹ����õ������б������ǻ�����Ӧ���½���ϸ̽��������⡣

�б���ʹ�ö�������һ�����ӡ�����ʹ�ñ���i��������ֵ��ʱ�򣬱��縳����5���������Ϊ�㴴����һ���������ͣ�int��������ʵ����i����ʵ�ϣ�����Կ�һ��help(int)�Ը��õ�������һ�㡣

��Ҳ��������������Ϊ�������غ���������������һ������Ķ����ʱ����ſ���ʹ����Щ���ܡ����磬PythonΪlist���ṩ��append��������������������б�β����һ����Ŀ������mylist.append('an item')�б�mylist�������Ǹ��ַ�����ע�⣬ʹ�õ����ʹ�ö���ķ�����

һ����Ҳ���������ǽ���Ϊ�������ı���������������һ������Ķ����ʱ����ſ���ʹ����Щ����/���ơ���Ҳͨ�����ʹ�ã�����mylist.field��

ʹ���б�

��9.1 ʹ���б�

#!/usr/bin/python
# Filename: using_list.py

# This is my shopping list

shoplist = ['apple', 'mango', 'carrot', 'banana']

print 'I have', len(shoplist),'items to purchase.'

print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
    print item,

print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist

print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist

print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist

��Դ�ļ���code/using_list.py��

���

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

������

����shoplist��ij�˵Ĺ����б�����shoplist�У�����ֻ�洢����Ķ����������ַ��������Ǽ�ס����������б������� �κ�����Ķ��� ���������������б���

����Ҳʹ����for..inѭ�����б��и���Ŀ��ݹ顣�����ڿ�ʼ����һ���Ѿ���ʶ���б�Ҳ��һ�����С����е����Ի��ں�����½������ۡ�

ע�⣬������print���Ľ�βʹ����һ�� ���� ������ÿ��print����Զ���ӡ�Ļ��з����������е��ѿ�������ȷʵ����Ч��

������������ʹ��append�������б���������һ����Ŀ������ǰ���Ѿ����۹���һ����Ȼ������ͨ����ӡ�б������������������Ŀ�Ƿ�ȷʵ�����ӽ��б��ˡ���ӡ�б�ֻ��򵥵ذ��б����ݸ�print��䣬���ǿ��Եõ�һ������������

�ٽ�����������ʹ���б���sort���������б�������Ҫ������ǣ��������Ӱ���б������������Ƿ���һ���޸ĺ���б����������ַ��������ķ�����ͬ�������������˵���б��� �ɱ�� ���ַ����� ���ɱ�� ��

��󣬵�������������г�����һ��������ʱ��������Ҫ�������б���ɾ��������ʹ��del������������������������ָ��������Ҫɾ���б��е��ĸ���Ŀ����del���Ϊ���Ǵ��б���ɾ����������ָ��������Ҫɾ���б��еĵ�һ��Ԫ�أ��������ʹ��del shoplist[0]����ס��Python��0��ʼ��������

�������Ҫ֪���б�����������з���������ͨ��help(list)���������֪ʶ��


��һҳ ��һ�� ��һҳ
��� ��ҳ Ԫ��