关于List的学习:
"""More on Lists? Using Lists as Stacks.""" fruits = [‘orange‘, ‘apple‘, ‘pear‘, ‘banana‘, ‘kiwi‘, ‘apple‘, ‘banana‘] #Return the number of times x appears in the listprint(fruits.count(‘apple‘))print(fruits.count(‘tangerine‘)) #Return zero-based index in the list of the first item whose value is x.#list.index(x[, start[, end]])print(fruits.index(‘kiwi‘, 4)) #Reverse the elements of the list in place.print("Before reversing ---------------------------------")print(fruits)fruits.reverse()print("After reversing ---------------------------------")print(fruits) #Add an item to the end of the listprint("Before appendding ---------------------------------")print(fruits)fruits.append(‘grape‘)print("After appendding ---------------------------------")print(fruits) #sort(key=None, reverse=False)fruits.sort()print("After sorting ---------------------------------")print(fruits) #Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns# the last item in the list.fruits.pop()print("After poping ---------------------------------")print(fruits) if ‘apple‘ in fruits: print("in range") 打印结果:
D:\Python3.6.1\python.exe F:/python_workspace/tutorial/Lists.py
2
0
4
Before reversing ---------------------------------
[‘orange‘, ‘apple‘, ‘pear‘, ‘banana‘, ‘kiwi‘, ‘apple‘, ‘banana‘]
After reversing ---------------------------------
[‘banana‘, ‘apple‘, ‘kiwi‘, ‘banana‘, ‘pear‘, ‘apple‘, ‘orange‘]
Before appendding ---------------------------------
[‘banana‘, ‘apple‘, ‘kiwi‘, ‘banana‘, ‘pear‘, ‘apple‘, ‘orange‘]
After appendding ---------------------------------
[‘banana‘, ‘apple‘, ‘kiwi‘, ‘banana‘, ‘pear‘, ‘apple‘, ‘orange‘, ‘grape‘]
After sorting ---------------------------------
[‘apple‘, ‘apple‘, ‘banana‘, ‘banana‘, ‘grape‘, ‘kiwi‘, ‘orange‘, ‘pear‘]
After poping ---------------------------------
[‘apple‘, ‘apple‘, ‘banana‘, ‘banana‘, ‘grape‘, ‘kiwi‘, ‘orange‘]
in range