标签:账号 count 使用 地址 cal names end 返回 str
一、列表
1. 定义列表:
变量 = [‘’,’’,’’,’’…….]
列表中可以包含列表,为子列表
2. 切片:取多个元素
>>> names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"] >>> names[1:4] #取下标1至下标4之间的数字,包括1,不包括4 [‘Tenglan‘, ‘Eric‘, ‘Rain‘] >>> names[1:-1] #取下标1至-1的值,不包括-1 [‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘] >>> names[0:3] [‘Alex‘, ‘Tenglan‘, ‘Eric‘] >>> names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样 [‘Alex‘, ‘Tenglan‘, ‘Eric‘] >>> names[3:] #如果想取最后一个,必须不能写-1,只能这么写 [‘Rain‘, ‘Tom‘, ‘Amy‘] >>> names[3:-1] #这样-1就不会被包含了 [‘Rain‘, ‘Tom‘] >>> names[0::2] #后面的2是代表,每隔一个元素,就取一个 [‘Alex‘, ‘Eric‘, ‘Tom‘] >>> names[::2] #和上句效果一样 [‘Alex‘, ‘Eric‘, ‘Tom‘]
3. 追加:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘] >>> names.append("我是新来的") >>> names [‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新来的‘]
4. 插入:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘] >>> names.insert(1,"新来的") >>> names [‘Alex‘, ‘新来的‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘]
5. 修改:
>>> names [‘Alex‘, ‘Tenglan‘, ‘强行从Eric前面插入‘, ‘Eric‘, ‘Rain‘ , ‘Tom‘, ‘Amy‘] >>> names[2] = "该换人了" >>> names [‘Alex‘, ‘Tenglan‘, ‘该换人了‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy ‘]
6. 删除:
1)
>>> del names[2] #删除下标为2的元素 >>> names [‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新来的‘]
2)
>>> names.remove("Eric") #删除指定元素 >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新来的‘]
3)
>>> names.pop() #默认删除列表最后一个值,输入下标等同于del names[] ‘我是新来的‘ >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘]
7. 扩展:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘] >>> b = [1,2,3] >>> names.extend(b) >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] del b#删除变量b
8. 拷贝:
a) 浅copy:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] 1) >>> name_copy = names.copy() >>> name_copy [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] 2) >>>names[3] = ‘向鹏’ #names[‘Tom‘] = ‘向鹏’ 报错,names[]必须写下标 >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘向鹏‘, ‘Amy‘, 1, 2, 3] >>> name_copy [‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, [‘Tom‘, ‘Amy‘],‘Tom‘, ‘Amy‘, 1, 2, 3] 3) >>> names[3][0] = ‘tom’ >>> names[4] = ‘向鹏’ [‘Alex‘, ‘Tenglan‘, ‘Rain‘, [‘tom‘, ‘Amy‘], ‘向鹏‘, ‘Amy‘, 1, 2, 3] >>> name_copy [‘Alex‘, ‘Tenglan‘, ‘Rain‘, [‘tom‘, ‘Amy‘],‘Tom‘, ‘Amy‘, 1, 2, 3]
浅copy,只copy第一层,下一层指向原内存地址,可理解为:第一层独立copy一份,不是指向的第一层的内存地址
第二个列表拷贝第一个列表,实际上第二个列表中的每个元素只是第一个列表每一个元素的引用
浅copy三种实现方式:
Import copy person = [‘name’,[‘saving’,100]] 1) P1 = copy.copy(person) 2) P2 = person[:] 3) P3 = list(person)
浅copy使用:
i. 创建联合账号
p1 = person[:] p2 = person[:] p1[0]=’alex’ p2[0]=’fengjie’ print(p1) ——>[‘alex’,[‘saving’,100]] print(p2) ——>[‘fengjie’,[‘saving’,100]] p1[1][1] = 50 print(p1) ——>[‘alex’,[‘saving’,50]] print(p2) ——>[‘fengjie’,[‘saving’,50]]
b) 深copy:
import copy name_copy = copy.deepcopy(names) >>> name_copy [‘Alex‘, ‘Tenglan‘, ‘Rain‘, [‘Tom‘, ‘Amy‘],‘Tom‘, ‘Amy‘, 1, 2, 3] >>> names [‘Alex‘, ‘Tenglan‘, ‘Rain‘, [‘tom‘, ‘Amy‘], ‘向鹏‘, ‘Amy‘, 1, 2, 3]
9. 统计:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] >>> names.count("Amy") 2
10. 排序&翻转:
排序:
>>> names [‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3] >>> names.sort() #排序 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() #3.0里不同数据类型不能放在一起排序 >>> names[-3] = ‘1‘ >>> names[-2] = ‘2‘ >>> names[-1] = ‘3‘ >>> names [‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘, ‘1‘, ‘2‘, ‘3‘] >>> names.sort() >>> names [‘1‘, ‘2‘, ‘3‘, ‘Alex‘, ‘Amy‘, ‘Amy‘, ‘Tenglan‘, ‘Tom‘]
翻转:
>>> names.reverse() #反转 >>> names [‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘]
11. 获取下标:
>>> names [‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘] >>> names.index("Amy") 2 #只返回找到的第一个下标 >>> names[names.index("Amy")] Amy
12.
1) 循环
for i in names: print(i)#打印names列表
2) 步长切片
print(names[0:-1:2]) print(names[::2])
二、元组
1. 元组一旦创建不能更改,只能查(切片),又叫只读列表
2. 语法:
names = ("alex","jack","eric")
它只有2个方法,一个是count,一个是index
标签:账号 count 使用 地址 cal names end 返回 str
原文地址:https://www.cnblogs.com/sleeping-cat/p/9224398.html