Python 列表增删改查排序统计

2018-06-23 13:30:44来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

列表的查

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[1:4])
结果:
['tom', 'jack', 'james']

注意:列表的切片的下标以 0 为开始。即: 下标0 --> klvchen,下标1 --> tom,..... 下标5 --> lucy。

          列表的切换顾头不顾尾,例如:a [1:4],则不会取到列表下标为4的元素

 

从左到右,取到最后:

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[1:])

结果:
['tom', 'jack', 'james', 'lily', 'lucy']

 

从左到右,取到倒数第二个值:

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[1:-1])
结果:
['tom', 'jack', 'james', 'lily']

 

从左到右,隔一个去取:

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[1::2])
结果:
['tom', 'james', 'lucy']

 

从右到左,一个一个去取:

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[3::-1])
结果:
['james', 'jack', 'tom', 'klvchen']

 

从右到左去取

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a[-2:-4:-1])
结果:
['lily', 'james']

 

列表的增

append 方法默认插到最后一个位置

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
a.append("mary")
print(a)
结果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy', 'mary']

 

insert 方法将数据插入到任意一个位置

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
a.insert(1,"mary")
print(a)
结果:
['klvchen', 'mary', 'tom', 'jack', 'james', 'lily', 'lucy']

 

extend 方法可在列表的末尾一次性追加另一个序列中的多个值

a = ["klvchen", "tom", "jack"]
b = ["james", "lily", "lucy"]
a.extend(b)
print(a)
结果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy']

 

列表的改

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
a[1]="tom2"
print(a)
结果:
['klvchen', 'tom2', 'jack', 'james', 'lily', 'lucy']
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
a[1:3]=["tom2", "jack2"]
print(a)
结果:
['klvchen', 'tom2', 'jack2', 'james', 'lily', 'lucy']

 

列表的删

remove 方法

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
a.remove("tom")
print(a)
结果:
['klvchen', 'jack', 'james', 'lily', 'lucy']

 

pop 方法

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
b = a.pop(1)
print(a)
print(b)
结果:
['klvchen', 'jack', 'james', 'lily', 'lucy']
tom

注意 pop 方法会返回删除的元素

 

del 方法

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
del a[1]
print(a)
结果:
['klvchen', 'jack', 'james', 'lily', 'lucy']
a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
del a
print(a)
结果:
NameError: name 'a' is not defined

注意:del a 把列表a从内存删除了,所以打印 a 列表程序会报错

 

列表元素的统计

list = ['a', 'a', 'b', 'c']
res = list.count('a')
print(res)
结果:
2

 

列表的排序

sort 方法

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a)
a.sort()
print(a)
结果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy']
['jack', 'james', 'klvchen', 'lily', 'lucy', 'tom']

 

reverse 方法

a = ["klvchen", "tom", "jack", "james", "lily", "lucy"]
print(a)
a.reverse()
print(a)
结果:
['klvchen', 'tom', 'jack', 'james', 'lily', 'lucy']
['lucy', 'lily', 'james', 'jack', 'tom', 'klvchen']

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:机器学习classification_report方法及precision精确率和recall召

下一篇:11-面向对象基础