发布于2020-03-18 14:14 阅读(1747) 评论(0) 点赞(14) 收藏(3)
列表 | 元组 |
---|---|
可修改 | 不可修改 |
[‘a’,‘b’,‘c’] | (‘a’,‘b’,‘c’) |
索引和切片
>>> name='Sudley'
>>> name[0]
'S'
>>> name[-1]
'y'
>>> name[0:]
'Sudley'
>>> name[:]
'Sudley'
>>> name[4:]
'ey'
>>> name[:-3]
'Sud'
>>>
上面的切片中默认步长为1,我们可以自定义步长,但是需要注意步长不能为0
>>> name[0::2]
'Sde'
>>> name[0:5:2]
'Sde'
>>> name[0:4:2]
'Sd'
>>> name[4:0:2]
''
>>> name[4:0:-2]
'ed'
>>>
序列相加、相乘
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> [1,2,3] + ['4','5','6'] #一般而言不建议拼接不同类型的序列
[1, 2, 3, '4', '5', '6']
>>>
成员资格
>>> 'Su' in 'Sudley'
True
>>> db = [
['Sudley','111111'],
['root','123456'],
]
>>> u='Sudley'
>>> p='123456'
>>> [u,p] in db
False
>>> p='111111'
>>> [u,p] in db
True
>>>
长度、最大值和最小值
>>> num = ['a','b','c']
>>> len(num)
3
>>> min(num)
'a'
>>> max(num)
'c'
>>>
基本操作
>>> x = [1,2] #列表赋值
>>> x[1] = 3
>>> x
[1, 3]
>>> name = list('Sudley') #列表和字符串之间的转换
>>> name
['S', 'u', 'd', 'l', 'e', 'y']
>>> str(name)
"['S', 'u', 'd', 'l', 'e', 'y']"
>>> for i in range(0,len(name)):
name1 = name1 + name[i]
>>> name1
'Sudley'
>>> name[5:] = list('eee') #切片赋值,可以实现插入或删除列表元素
>>> name
['S', 'u', 'd', 'l', 'e', 'e', 'e', 'e']
>>> name[0:5] = []
>>> name
['e', 'e', 'e']
>>> del name[1:]
>>> name
['e']
>>>
列表方法
>>> #列表末尾追加元素
>>> num = [1,2]
>>> num.append(3)
>>> num
[1, 2, 3]
>>> #列表拷贝
>>> num1 = num
>>> num2 = num.copy()
>>> num3 = num[:]
>>> num[1] = 'a'
>>> num
[1, 'a', 3]
>>> num1
[1, 'a', 3]
>>> num2
[1, 2, 3]
>>> num3
[1, 2, 3]
>>> #元素计数
>>> num = [[1,2],1,1,[2,1,[1,2]]]
>>> num.count(1)
2
>>> num.count([1,2])
1
>>> #列表扩展
>>> a = [1,2]
>>> b = [3,4]
>>> a + b
[1, 2, 3, 4]
>>> a
[1, 2]
>>> a.extend(b)
>>> a
[1, 2, 3, 4]
>>> a.append(b)
>>> a
[1, 2, 3, 4, [3, 4]]
>>> #索引,只查找第一个出现的索引
>>> a.index(3)
2
>>> a.index([3,4])
4
>>> a.append(3)
>>> a.index(3)
2
>>> a
[1, 2, 3, 4, [3, 4], 3]
>>>#插入对象
>>> a.insert(2,2.5)
>>> a
[1, 2, 2.5, 3, 4, [3, 4], 3]
>>> a.insert(4,3.5)
>>> a
[1, 2, 2.5, 3, 3.5, 4, [3, 4], 3]
>>>#删除元素
>>> a.pop()
3
>>> a.pop(0)
1
>>>
>>> a
[2, 2.5, 3, 3.5, 4, [3, 4]]
>>> a.append(a.pop())
>>> a
[2, 2.5, 3, 3.5, 4, [3, 4]]
>>> a[1] = 2
>>> a
[2, 2, 3, 3.5, 4, [3, 4]]
>>> a.remove(2)
>>> a
[2, 3, 3.5, 4, [3, 4]]
>>>#排序
>>> a = [1,3,2]
>>> a.reverse()
>>> a
[2, 3, 1]
>>> a.sort()
>>> a
[1, 2, 3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>>#常见错误
>>> b = a.sort()
>>> a
[1, 2, 3]
>>> b
>>> b = a.pop(1) #pop()是唯一即修改列表又返回非None值的列表方法
>>> b
2
>>>
元组最大的特点是不能修改,一般用作字典的键
>>> a = (1,2)
>>> a[0]
1
>>> a[0] = 2
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
a[0] = 2
TypeError: 'tuple' object does not support item assignment
>>> a
(1, 2)
>>> a = 1,2
>>> a
(1, 2)
>>> a = 1,
>>> a
(1,)
>>> a = (1) #元组的括号可以省略但是逗号不能省略
>>> a
1
>>> type(a)
<class 'int'>
>>> a = (1,)
>>> type(a)
<class 'tuple'>
>>> a
(1,)
>>>
作者:sjhjf0293
链接:https://www.pythonheidong.com/blog/article/265652/8f4a4ef4b9321f7554e0/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!