Python学习之list

2018-06-09  本文已影响0人  brave_wu

测试代码

#list
l = list()
l = [1,2,2,2,'3',None]
print('查询0和-1:')
a = l[0]
print(a)
a = l[-1]
print(a)
print('查询元素2靠近0和-3的下标:')
a = l.index(2,0)
b = l.index(2,-3)
print(a)
print(b)
print('查询2的个数:')
a = l.count(2)
print(a)

print('增添q:')
l.append('q')
print(l)
print('插入w到2:')
l.insert(2,'w')
print(l)
print('乘法:')
l = l * 2
print(l)
print('加法:')
l = l + [666,888]
print(l)
print('加法2接续:')
l.extend([666,888])
print(l)
print('删除w:')
l.remove('w')
print(l)
print('剔除第0个:')
a = l.pop(0)#默认最后一个
print(a)
print(l)
print('反转:')
l.reverse()
print(l)
print('清空:')
l.clear()
print(l)
l = [1,2,3,6,5,4]
print('倒序:')
l.sort(reverse=True)#默认123456
print(l)

运行结果

查询0和-1:
1
None
查询元素2靠近0和-3的下标:
1
3
查询2的个数:
3
增添q:
[1, 2, 2, 2, '3', None, 'q']
插入w到2:
[1, 2, 'w', 2, 2, '3', None, 'q']
乘法:
[1, 2, 'w', 2, 2, '3', None, 'q', 1, 2, 'w', 2, 2, '3', None, 'q']
加法:
[1, 2, 'w', 2, 2, '3', None, 'q', 1, 2, 'w', 2, 2, '3', None, 'q', 666, 888]
加法2接续:
[1, 2, 'w', 2, 2, '3', None, 'q', 1, 2, 'w', 2, 2, '3', None, 'q', 666, 888, 666, 888]
删除w:
[1, 2, 2, 2, '3', None, 'q', 1, 2, 'w', 2, 2, '3', None, 'q', 666, 888, 666, 888]
剔除第0个:
1
[2, 2, 2, '3', None, 'q', 1, 2, 'w', 2, 2, '3', None, 'q', 666, 888, 666, 888]
反转:
[888, 666, 888, 666, 'q', None, '3', 2, 2, 'w', 2, 1, 'q', None, '3', 2, 2, 2]
清空:
[]
倒序:
[6, 5, 4, 3, 2, 1]

总结

python中list的方法:
index,count,append,insert,extend,remove,pop,reverse,clear,sort....

上一篇下一篇

猜你喜欢

热点阅读