增
append
增加
In [1]: hero = ['1','2']
In [2]: hero.append('3')
In [3]: hero
Out[3]: ['1', '2', '3']
extend
多个增加
In [3]: hero
Out[3]: ['1', '2', '3']
In [4]: hero.extend(['4','5','6'])
In [5]: hero
Out[5]: ['1', '2', '3', '4', '5', '6']
len(s):
通过长度增加
In [6]: s = [1, 2, 3, 4, 5]
In [7]: s[len(s):] = [6]
In [8]: s
Out[8]: [1, 2, 3, 4, 5, 6]
In [9]: s[len(s):] = [7,8,9]
In [10]: s
Out[10]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
insert
增加对应的元素(在1的后面加上逗号后面的2)
In [11]: s = [1,3,4,5]
In [12]: s.insert(1,2)
In [13]: s
Out[13]: [1, 2, 3, 4, 5]
删
remove
删除指定元素
In [5]: hero
Out[5]: ['1', '2', '3', '4', '5', '6']
In [14]: hero.remove('1')
In [15]: hero
Out[15]: ['2', '3', '4', '5', '6']
pop
删除下标对应元素
In [17]: hero
Out[17]: ['2', '3', '4', '5', '6']
In [18]: hero.pop(2)
Out[18]: '4'
In [19]: hero
Out[19]: ['2', '3', '5', '6']
clear
清空
In [19]: hero
Out[19]: ['2', '3', '5', '6']
In [20]: hero.clear()
In [21]: hero
Out[21]: []
改
[ ] = " "
替换
In [22]: hero = ['1','2','3','4','5','6']
In [23]: hero[4] = "55"
In [24]: hero
Out[24]: ['1', '2', '3', '4', '55', '6']
[ :] = " "
批量替换
In [24]: hero
Out[24]: ['1', '2', '3', '4', '55', '6']
In [25]: hero [3:] = ['44','55','66']
In [26]: hero
Out[26]: ['1', '2', '3', '44', '55', '66']
sort
正序排序
In [27]: nums = [3,1,9,6,8,3,5,3]
In [28]: nums.sort()
In [29]: nums
Out[29]: [1, 3, 3, 3, 5, 6, 8, 9]
reverse
倒叙排序
In [29]: nums
Out[29]: [1, 3, 3, 3, 5, 6, 8, 9]
In [30]: nums.reverse()
In [31]: nums
Out[31]: [9, 8, 6, 5, 3, 3, 3, 1]
In [33]: nums.sort(reverse=True)
In [34]: nums
Out[34]: [9, 8, 6, 5, 3, 3, 3, 1]
copy
复制数组(浅拷贝)
In [47]: nums
Out[47]: [3, 1, 9, 6, 8, 3, 5, 3]
In [48]: nums_copy1 = nums.copy()
In [49]: nums_copy1
Out[49]: [3, 1, 9, 6, 8, 3, 5, 3]
copy
用切片来指定所有,效果是一样的(浅拷贝)
In [49]: nums_copy1
Out[49]: [3, 1, 9, 6, 8, 3, 5, 3]
In [50]: nums_copy2 = nums[:]
In [51]: nums_copy2
Out[51]: [3, 1, 9, 6, 8, 3, 5, 3]
查
count
查找目标元素出现次数
In [34]: nums
Out[34]: [9, 8, 6, 5, 3, 3, 3, 1]
In [35]: nums.count(3)
Out[35]: 3
index
查找目标元素的索引值
In [37]: heros = ['riven','yasuo','zark','nadalee','leesin','draven']
In [38]: heros.index("leesin")
Out[38]: 4
通过index
查询更改(这里leesin就是=4)
In [39]: heros
Out[39]: ['riven', 'yasuo', 'zark', 'nadalee', 'leesin', 'draven']
In [40]: heros[heros.index("leesin")] = "ashe"
In [41]: heros
Out[41]: ['riven', 'yasuo', 'zark', 'nadalee', 'ashe', 'draven']
通过index
查询目标的第一个索引值是多少
In [43]: nums = [3,1,9,6,8,3,5,3]
In [44]: nums.index(3)
Out[44]: 0
通过index
查询目标的第一个索引值是多少(指定范围)
In [45]: nums
Out[45]: [3, 1, 9, 6, 8, 3, 5, 3]
In [46]: nums.index(3,1,7)
Out[46]: 5
标签:index,hero,nums,Python,改查,55,curd,heros,Out
From: https://www.cnblogs.com/2ich4n/p/18104870