码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础学习(三)四

时间:2017-07-03 22:23:58      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:returns   tac   hose   key   index   sts   for   .exe   range   

关于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 list
print(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 list
print("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

Python基础学习(三)四

标签:returns   tac   hose   key   index   sts   for   .exe   range   

原文地址:http://www.cnblogs.com/cathyj/p/7113143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!