python序列(列表,元组,字典)的增删改查

列表


操作


列表


方法


示例


增加


list.append(obj)

增加元素到末尾


eg.

>>> list1=[‘hello‘,‘world‘,‘how‘,‘are‘,‘you‘]

>>> list1.append(‘!‘)

>>> list1

[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘, ‘!‘]


list.insert(index, obj)

增加元素到指定位置

index:索引位置

obj:内容


eg.

>>> list1

[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘, ‘!‘]

>>> list1.insert(1,‘,‘)

>>> list1

[‘hello‘, ‘,‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘, ‘!‘]


list.extend(list_i)

将list_i列表中的元素增加到list中


eg.

>>> list

[‘hello‘, ‘how‘, ‘are‘, ‘you‘]

>>> list.extend([‘good‘,‘girl‘])

>>> list

[‘hello‘, ‘how‘, ‘are‘, ‘you‘, ‘good‘, ‘girl‘]


删除


list.pop():

默认删除list末尾的元素

list.pop(index)

删除指定位置的元素,index是索引


eg.

>>> list1

[‘hello‘, ‘,‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘, ‘!‘]

>>> list1.pop()

‘!‘

>>> list1.pop(1)

‘,‘


del list[index]

删除指定位置的元素,index是索引

del list

删除整个列表


eg.

>>> list1

[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> del list1[1]

>>> list1

[‘hello‘, ‘how‘, ‘are‘, ‘you‘]

>>> list1

[‘hello‘, ‘how‘, ‘are‘, ‘you‘]

>>> del list1

>>> list1

Traceback (most recent call last):

File "<stdin>", line
1, in <module>

NameError: name ‘list1‘ is not defined


list.remove(obj)

移除列表第一个与obj相等的元素


eg.

>>> list=[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list.remove(‘world‘)

>>> list

[‘hello‘, ‘how‘, ‘are‘, ‘you‘]


list.clear()

清空列表全部内容


eg.

>>> list=[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list.clear()

>>> list

[]


修改


list[index]=obj

修改指定位置的元素


eg.

>>> list1

[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list1[0]=‘hi‘

>>> list1

[‘hi‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]


查询


list[index]

通过下标索引,从0开始


eg.

>>> list=[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list[2]

‘how‘


list[a:b]

切片,顾头不顾尾


eg.

>>> list=[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list[0:3]

[‘hello‘, ‘world‘, ‘how‘]

>>> list[1:]

[‘world‘, ‘how‘, ‘are‘, ‘you‘]

>>> list[:3]

[‘hello‘, ‘world‘, ‘how‘]

>>> list[:]

[‘hello‘, ‘world‘, ‘how‘, ‘are‘, ‘you‘]

元组


操作


元组


方法


示例


增加


tup=tup1+tup2

元组不支持修改,但可以通过连接组合的方式进行增加


eg.

>>> tup1=(1,2,3)

>>> tup2=(4,5,6)

>>> tup=tup1+tup2

>>> tup

(1, 2, 3, 4, 5, 6)


删除


del tup

元组不支持单个元素删除,但可以删除整个元组


eg.

>>> tup

(1, 2, 3, 4, 5, 6)

>>> del tup

>>> tup

Traceback (most recent call last):

File "<stdin>", line
1, in <module>

NameError: name ‘tup‘ is not defined


修改


tup=tup[index1],tup1[index2], ...

tup=tup[index1:index2]

元组是不可变类型,不能修改元组的元素。可通过现有的字符串拼接构造一个新元组


eg.

>>> tup=(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘)

>>> tup=tup[1],tup[2],tup[4]

>>> tup

(‘b‘, ‘c‘, ‘e‘)

>>> tup=(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘)

>>> tup=tup[1:3]

>>> tup

(‘b‘, ‘c‘)


查询


tup[index]

通过下标索引,从0开始


eg.

>>> tup=(1,2,3,4)

>>> tup[2]

3


tup[a:b]

切片,顾头不顾尾


eg.

>>> tup=(1,2,3,4)

>>> tup[1:3]

(2, 3)

>>> tup[1:]

(2, 3, 4)

>>> tup[:3]

(1, 2, 3)

>>> tup[:]

(1, 2, 3, 4)

字典


操作


字典


方法


示例


增加


dict[key]=value

通过赋值的方法增加元素


eg.

>>> dict={‘name‘:‘li‘,‘age‘:1}

>>> dict[‘class‘]=‘first‘

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘}


dict.update(dict_i)

把新的字典dict_i的键/值对更新到dict里(适用dict_i中包含与dict不同的key)


eg.

>>> dict={‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘}

>>> dict.update(school=‘wawo‘)

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘: ‘wawo‘}


删除


del dict[key]

删除单一元素,通过key来指定删除

del dict

删除字典


eg.

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘}

>>> del dict[‘class‘]

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1}

>>> dict1={‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘}

>>> del dict1

>>> dict1

Traceback (most recent call last):

File "<stdin>", line
1, in <module>

NameError: name ‘dict1‘ is not defined


dict.pop(key)

删除单一元素,通过key来指定删除


eg.

>>> dict={‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘}

>>> dict.pop(‘name‘)

‘li‘

>>> dict

{‘age‘: 1, ‘class‘: ‘first‘}


dict.clear()

清空全部内容


eg.

>>> dict

{‘age‘: 1, ‘class‘: ‘first‘}

>>> dict.clear()

>>> dict

{}


修改


dict[key]=value

通过对已有的key重新赋值的方法修改


eg.

>>> dict

{‘name‘: ‘pang‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘: ‘wawo‘}

>>> dict[‘name‘]=‘li‘

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘: ‘wawo‘}


dict.update(dict_i)

把字典dict_i的键/值对更新到dict里(适用dict_i中包含与dict相同的key)


eg.

>>> dict

{‘name‘: ‘li‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘: ‘wawo‘}

>>> dict.update(name=‘pang‘)

>>> dict

{‘name‘: ‘pang‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘: ‘wawo‘}


查询


dict[key]

通过key访问value值


eg.

>>> dict={‘name‘: ‘pang‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘:
‘wawo‘}

>>> dict[‘name‘]

‘pang‘


dict.items()

以列表返回可遍历的(键, 值) 元组数组


eg.

>>> dict={‘name‘: ‘pang‘, ‘age‘: 1, ‘class‘: ‘first‘, ‘school‘:
‘wawo‘}

>>> dict.items()

dict_items([(‘name‘, ‘pang‘), (‘age‘, 1), (‘class‘, ‘first‘), (‘school‘,
‘wawo‘)])


dict.keys()

以列表返回一个字典所有键值

dict.values()

以列表返回一个字典所有值


eg.

>>> dict.keys()

dict_keys([‘name‘, ‘age‘, ‘class‘, ‘school‘])

>>> dict.values()

dict_values([‘pang‘, 1, ‘first‘, ‘wawo‘])


dict.get(key)

返回指定key的对应字典值,没有返回none


eg.

>>> dict.get(‘age‘)

1

原文地址:https://www.cnblogs.com/lilip/p/9249379.html

时间: 2024-08-28 22:15:27

python序列(列表,元组,字典)的增删改查的相关文章

列表、字典的增删改查

一.列表: # 列表的作用:存多个值,可以修改 # hobby = ['play','eat','sleep'] # # 列表的具体操作 # 查看: # hobby = ['play','eat','sleep',['egon','ysb']] # f = hobby[2] # 查看hobbyt列表内第二个元素 # print(f) # # 增加(1): # hobby = ['play','eat','sleep',['egon','ysb']] # hobby.append(元素) # f

python 字典的增删改查

字典 # 1.存储数据比较大 # 2.字典的查找速度比列表快 # 3.字典都是通过键来操作的,且键必须是唯一的. # 4.# dic = {"键":"值","key":"value"} 字典的增删改查 字典增: dic = {} dic["键"]= 值 添加键值对 dic = {} dic.setdefault("键",值) 有则更改,无则添加 字典删: dic = {"111

字典 字典的增删改查

1)什么是字典(dict)dict. 以{}表示. 每一项用逗号隔开,内部元素用key:value的形式来保存数据{"jj":"林俊杰", "jay":"周杰伦"} 查询的效率非常高, 通过key来查找元素 内部使用key来计算一个内存地址(暂时),hash算法. key必须是不可变的数据类型(key 必须是可哈希的数据类型) 可哈希就是不可变 2)字典的增删改查 新增: dict["新key"] = &q

python中列表 元组 字典 集合的区别

列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复项的.如果我们扩大清单的范围,统计我们过去一周所有的花费情况,那么这也是一个清单,但这个清单里会有类别不同的项,比如我们买东西是一种花费,交水电费也是一种花费,这些项的类型是可以使不同的.pyt

Python学习---django之ORM的增删改查180125

模型常用的字段类型参数 <1> CharField        #字符串字段, 用于较短的字符串.        #CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的最大字符数.<2> IntegerField       #用于保存一个整数.<3> FloatField        # 一个浮点数. 必须 提供两个参数:    参数    描述        # max_digits    总位数(不

Python 模拟SQL对文件进行增删改查

1 #!/usr/bin/env python 2 # _*_ coding:UTF-8 _*_ 3 # __auth__: Dalhhin 4 # Python 3.5.2,Pycharm 2016.3.2 5 # 2017/05/15 6 7 import sys,os 8 9 def where(dbfile,where_list): #条件是一个list 10 11 def and_or_where(sub_where_list): 12 '''获取and或同时含有and.or关键字的条

字典相关函数(增删改查)

# ###字典的相关函数 (增删改查) # (1) 增 dictvar = {} dictvar['top'] = "凯" dictvar['middle'] = "妲己" dictvar["bottom"] = "鲁班七号" dictvar["jungle"] = "刘备" dictvar["support"] = "刘邦" print(dict

Python网络编程06----django数据库的增删改查

首先定义model如下. class UserInfo(models.Model): username=models.CharField(max_length=50,null=True,primary_key=False,db_index=True) #可以为空,不是主键,创建索引 password=models.CharField(max_length=50,error_messages={"invalid":"出错"}) #定义出错信息 gender=model

python之MySQL学习——简单的增删改查封装

1.增删改查封装类MysqlHelper.py 1 import pymysql as ps 2 3 class MysqlHelper: 4 def __init__(self, host, user, password, database, charset): 5 self.host = host 6 self.user = user 7 self.password = password 8 self.database = database 9 self.charset = charset

python基础入门---数列的一些增删改查操作

数列的一些增删改查操作 import copy names = ["aa","bb","cc","dd"] print(names) print(names[1],names[2]) print(names[0:2])#切片,要头不要尾 print(names[:2])#和上一个结果一样 print(names[-1])#切片,取倒数第一个 print(names[-2])#切片,取倒数第二个 print(names[-2:]