day3之笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day3之笔记相关的知识,希望对你有一定的参考价值。
list方法:
append添加,
以下分别插入了不同的元素,从屁股的位置开始插入:
name=[‘alex‘,‘eric‘,‘tony‘] name.append(‘Hanmeimie‘) nam_2=[‘Jim‘,‘Tom‘] name.append(nam_2) name.append(1) name_dic={‘k1‘:‘Lilei‘} name.append(name_dic) for i in range(10): name.append(i) name
index 获取某个值的索引:
前面三个都会返回正确的index,后面三个都会pop up error,注意最小单位,顺序及大小写,都是严格匹配的容不得半点儿马虎;
另外如果有超过两个相同的最小单位,index只会返回最左边的第一个
name=[‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] name.index(‘alex‘) name.index(1) name.index([‘Jim‘,‘Tom‘]) name.index(‘Tom‘,‘JIm‘) name.index(‘Tom‘,‘Jim‘)
name.index(‘Tom‘)
>>> name
[‘Alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, ‘T_bag‘]
>>> name.append(‘alex‘)
>>> name.index(‘alex‘)
1
insert插入索引值+veriable:
都是在索引值的前面插入元素
name=[‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] name.insert(0,‘Alex‘) #result [‘Alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] name.insert((len(name)-1), ‘T_bag‘) # result ‘Alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, ‘T_bag‘, 9]
pop,切掉尾部的那个,并赋值给另外一个,remove+ assign;
[‘Alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, ‘T_bag‘, 9] >>> adbd=name.pop() >>> adbd 9 >>> name [‘Alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, ‘T_bag‘] >>>
remove:name_list.remove("seven"),移除从左边找到的第一个,重复的
>>> name [‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> name.insert(0,‘alex‘) >>> name [‘alex‘, ‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> name.remove(‘alex‘) >>> name [‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse():反向
>>> name [‘alex‘, ‘eric‘, ‘tony‘, [‘Jim‘, ‘Tom‘], 1, ‘Hanmeimie‘, {‘k1‘: ‘Lilei‘}, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> name.reverse() >>> name [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, {‘k1‘: ‘Lilei‘}, ‘Hanmeimie‘, 1, [‘Jim‘, ‘Tom‘], ‘tony‘, ‘eric‘, ‘alex‘] >>>
sort:排序 str + int + 汉字,会pop error,后面需要用算法来cover;
>>> name [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, {‘k1‘: ‘Lilei‘}, ‘Hanmeimie‘, 1, [‘Jim‘, ‘Tom‘], ‘tony‘, ‘eric‘, ‘alex‘] >>> name.sort() >>> name [0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, {‘k1‘: ‘Lilei‘}, [‘Jim‘, ‘Tom‘], ‘Hanmeimie‘, ‘alex‘, ‘eric‘, ‘tony‘]
acount 出现的次数
[0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, {‘k1‘: ‘Lilei‘}, [‘Jim‘, ‘Tom‘], ‘Hanmeimie‘, ‘alex‘, ‘eric‘, ‘tony‘] >>> name.count(‘alex‘) 1 >>> name.index(‘alex‘) 14 >>> name.insert(14,‘alex‘) >>> name.count(‘alex‘) 2
del name_list[1],删除第二个,删除指定所以位置,或切片位置所对应的值
>>> name [0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, {‘k1‘: ‘Lilei‘}, [‘Jim‘, ‘Tom‘], ‘Hanmeimie‘, ‘alex‘, ‘alex‘, ‘eric‘, ‘tony‘] >>> name.index(‘Hanmeimie‘) 13 >>> del name[13] >>> name.index(‘Hanmeimie‘) Traceback (most recent call last): File "<pyshell#107>", line 1, in <module> name.index(‘Hanmeimie‘) ValueError: ‘Hanmeimie‘ is not in list >>> name [0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, {‘k1‘: ‘Lilei‘}, [‘Jim‘, ‘Tom‘], ‘alex‘, ‘alex‘, ‘eric‘, ‘tony‘]
元组和列表几乎是一样的,列表是可以修改的元祖是不能修改的;
name_tumple=(‘alex‘,‘eric‘,‘tony‘)
以上是关于day3之笔记的主要内容,如果未能解决你的问题,请参考以下文章