列表for赋值
>>> vara=[id for id in list(range(1,100,2))] ---1-99间隔为2
>>> print (vara)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
列表计算
>>> max(vara)
99
>>> min(vara)
1
>>> sum(vara)
2500
列表分片,分割
>>> varb=vara[1:10] ----列表分割从1-9个元素
>>> print(varb)
[3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> varb=vara[-2:]
>>> print(varb)
[97, 99]
>>>
>>> varc=varb
>>> print(varc)
[97, 99]
>>>
>>> varc=varb[:]
>>> print(varc)
[97, 99]
>>>
-bash-4.1$ vi fot.py
vara=[id for id in list(range(1,100,2))]
for id1 in vara[1:10]:
print(id1**2)
-bash-4.1$ python fot.py
9
25
49
81
121
169
225
289
361
bicycles=['trek','cannonale','redline','specaaized']
bicycles=[]
print(bicycles[0].upper())
>>> bicycles=['trek','cannonale','redline','specaaized']
>>> print(bicycles[0].upper())
TREK
>>> print(bicycles[1].upper())
CANNONALE
>>> print(bicycles[2].upper())
REDLINE
>>> print(bicycles[3].upper())
SPECAAIZED
>>> print(bicycles[-1].upper())
SPECAAIZED
范例:
bicycles=['trek','cannonale','redline','specaaized']
message='My bicycle is '+bicycles[-1].title()+"......... NICE"
print(message)
>>> bicycles=['trek','cannonale','redline','specaaized']
>>> message='My bicycle is '+bicycles[-1].title()+"......... NICE"
>>> print(message)
My bicycle is Specaaized......... NICE
bicycles=[]
bicycles.append('trek') --表示在末尾添加值。
bicycles.append('aaaannonale')
bicycles.append('redline')
bicycles.append('specaaized')
print(bicycles.upper())
>>> print(bicycles.lower())
Traceback (most recent call last):
File "
AttributeError: 'list' object has no attribute 'lower'
>>> print(bicycles[0].lower())
trek
>>> print(bicycles)
['trek', 'cannonale', 'redline', 'specaaized']
bicycles.insert(2,'zhuzhu')
>>> print(bicycles)
['clg', 'cannonale', 'zhuzhu', 'redline', 'specaaized']
>>> bicycles[0]='clg' --修改0号位置的值
>>> print(bicycles)
['clg', 'cannonale', 'redline', 'specaaized']
>>>
>>> del bicycles[3]
>>> print(bicycles)
['clg', 'cannonale', 'zhuzhu', 'specaaized']
>>>
pop()方法 弹出末尾元素供使用
可删除列表末尾的元素,并让你直接能够使用它。
>>> print(bicycles)
['clg', 'cannonale', 'zhuzhu', 'specaaized']
>>> one_best=bicycles.pop()
>>> print(bicycles)
['clg', 'cannonale', 'zhuzhu']
>>> print(one_best)
specaaized
>>>
pop(n) 弹出第n个元素供使用
>>> print(bicycles)
['clg', 'cannonale', 'zhuzhu']
>>> two_best=bicycles.pop(2)
>>> print(bicycles)
['clg', 'cannonale']
>>> print(two_best)
zhuzhu
>>>
remove('XXX') 根据值删除元素(对于重复值如何处理????)
>>> print(bicycles)
['clg', 'cannonale']
>>> bicycles.remove('clg')
>>> print(bicycles)
['cannonale']
>>>
sort()方法
>>> bicycles.append('aaaannonale')
>>> bicycles.sort() ---永久性修改排列顺序
>>> print(bicycles)
['aaaannonale', 'cannonale', 'cannonale', 'redline', 'specaaized']
反向排序
>>> bicycles.sort(reverse=True)
>>> print(bicycles)
['specaaized', 'redline', 'cannonale', 'cannonale', 'aaaannonale']
临时排序函数输出
>>> print (sorted(bicycles))
['aaaannonale', 'cannonale', 'cannonale', 'redline', 'specaaized']
>>> print (bicycles)
['specaaized', 'redline', 'cannonale', 'cannonale', 'aaaannonale']
>>>
反转列表变量
name=[]
name.append('123')
name.insert(1,'zj')
name.reverse
print(name)
列表长度函数
len(name)