幾個實例展示python中數據結構list的魅力!
### list變量申明
~~~
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
~~~
### 訪問list元素
~~~
array= [1,2,5,3,6,8,4]
#其實這里的順序標識是
(0,1,2,3,4,5,6)
(-7,-6,-5,-4,-3,-2,-1)#有負的下標哦
array[0:]#列出index==0以后的
[1,2,5,3,6,8,4]
array[1:]#列出index==1以后的
[2,5,3,6,8,4]
array[:-1]#列出index==-1之前的
[1,2,5,3,6,8]
array[3:-3]#列出index==3到index==-3之間的,<span style="color:#ff0000;">注意不包含index==-3,即左閉右開</span>
[3]
~~~
### list應用
### loops and list
~~~
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
for i in change:
print "I got %r" % i
~~~
### enumerate
~~~
for i,j in enumerate([[1,2],['o',2],[9,3]]):
k1,k2=j
print i,k1,k2
~~~
output
~~~
0 1 2
1 o 2
2 9 3
~~~
### append/extend/pop/del
append()添加元素至list尾部
~~~
elements=[1,2,3,4,5]
elements.append(6)
~~~
extend()拼接兩個list
~~~
list1=[1,2,3,4,5]
list2=[6,7]
elements.extend(list2)
print elements
~~~
pop(int i=-1)刪除list中指定下標的元素,并返回該位置上的數,default :刪除最后一個
~~~
elements=[1,2,3,4,5]
print elements.pop()#default:delete index==-1,i.e. the last element
print elements
elements=[1,2,3,4,5]
elements.pop(-2)#delete index==-2,i.e. 4
print elements
elements.pop(1)
print elements
~~~
del
~~~
elements=[1,2,3,4,5]
del elements[0]#default:delete index==0,i.e. 1
print elements
elements=[1,2,3,4,5]
del elements[2:4]#delete index==2 and index==3,i.e.3 and 4
print elements
elements=[1,2,3,4,5]
del elements#delete all
elements
~~~
### do things to list
~~~
ten_things = "Apples Oranges Crows Telephone Light Sugar"
print "Wait there's not 10 things in that list, let's fix that."
stuff = ten_things.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
while len(stuff) != 10:
next_one = more_stuff.pop()
print "Adding: ", next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff)
print "There we go: ", stuff
print "Let's do some things with stuff."
print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool!
print '#'.join(stuff[3:5]) # super stellar! outputs:stuff[3]#stuff[4]
~~~
' '.join(things)?reads as, "Join things with ' ' between them." Meanwhile,?join(' ', things)?means, "Call join with ' ' and things。返回一個string.
輸出
~~~
Wait there's not 10 things in that list, let's fix that.
Adding: Boy
There's 7 items now.
Adding: Girl
There's 8 items now.
Adding: Banana
There's 9 items now.
Adding: Corn
There's 10 items now.
There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']
Let's do some things with stuff.
Oranges
Corn
Banana
['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Corn']
Apples Oranges Crows Telephone Light Sugar Boy Girl Corn
Telephone#Light#Sugar
~~~
實驗一些list的方法:
n1=[1,2,3,4]
print nl.count(5)?????? # 計數,看總共有多少個5
print nl.index(3)?????? # 查詢 nl 的第一個3的下標
nl.append(6)??????????? # 在 nl 的最后增添一個新元素6
nl.sort()?????????????? # 對nl的元素排序
print nl.pop()????????? # 從nl中去除最后一個元素,并將該元素返回。
nl.remove(2)??????????? # 從nl中去除第一個2
nl.insert(0,9)????????? # 在下標為0的位置插入9