列表增删改查

Posted lv_vl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表增删改查相关的知识,希望对你有一定的参考价值。

1,列表中添加一个元素

>>> name= ["jim","tom","luch","jon","jia"]

>>> name.append("key")

>>> name
[‘jim‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

2,列表中查找元素的索引

>>> name

[‘jim‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]
>>> name.index(‘jim‘)
0
>>> name.index(‘ka‘)
1
3,通过索引在ka的前面添加kaka

>>> name

[‘jim‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

>>> name.insert(name.index(‘ka‘),‘kaka‘)
>>> name
[‘jim‘, ‘kaka‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

4,直接删除列表中的元素

>>> name
[‘jim‘, ‘kaka‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]
>>> name.remove(‘jim‘)
>>> name
[‘kaka‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

5,通过索引删除

>>> name
[‘kaka‘, ‘ka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]
>>> del name[1]
>>> name
[‘kaka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

6,通过索引更改列表中的元素

>>> name
[‘kaka‘, ‘wen‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]
>>> name[1]="kaka"
>>> name
[‘kaka‘, ‘kaka‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]

7,统计列表中相同元素的个数

>>> name
[‘kaka‘, ‘kaka‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘]
>>> name.count(‘kaka‘)
2

8,列表元素正向排序

>>> name
[‘kaka‘, ‘kaka‘, ‘tom‘, ‘luch‘, ‘jon‘, ‘jia‘, ‘key‘, 1, 2, 30]
>>> name.sort()
>>> name
[1, 2, 30, ‘jia‘, ‘jon‘, ‘kaka‘, ‘kaka‘, ‘key‘, ‘luch‘, ‘tom‘]

9.列表元素反向排序

>>> name
[1, 2, 30, ‘jia‘, ‘jon‘, ‘kaka‘, ‘kaka‘, ‘key‘, ‘luch‘, ‘tom‘]
>>> name.reverse()
>>> name
[‘tom‘, ‘luch‘, ‘key‘, ‘kaka‘, ‘kaka‘, ‘jon‘, ‘jia‘, 30, 2, 1]

 

以上是关于列表增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Laravel框架——增删改查

库表的增删改查

mysql数据库增删改查

mysql增删改查

django配置模型增删改查

MySQL基本操作--库表增删改查