列表

操作

列表

方法

示例

增加

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

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

  1. Python(三)字典的增删改查和遍历

    一.增加

  2. python中列表的常用操作增删改查

    1. 列表的概念,列表是一种存储大量数据的存储模型. 2. 列表的特点,列表具有索引的概念,可以通过索引操作列表中的数据.列表中的数据可以进行添加.删除.修改.查询等操作. 3. 列表的基本语法 创建 ...

  3. python中列表中元素的增删改查

    增: append : 默认添加到列表的最后一个位置 insert : 可以通过下标添加到列表的任意位置 extend: a.extend[b] --将b列表的元素全加入到列表b中 删; remove ...

  4. 2018.8.1 python中字典的增删改查及其它操作

    一.字典的简单介绍 1.dict 用{}来表示       键值对数据           {key:value} 唯一性 2.键都必须是可哈希,不可变的数据类型就可以当做字典中的键 值没有任何限制 ...

  5. python字典的增删改查

    字典dict 知识点: {}括起来,以键值对形式存储的容器性数据类型: 键-必须是不可变数据类型,且是唯一的: -值可以是任意数据类型.对象. 优点:关联性强,查询速度快. 缺点:以空间换时间. 字典 ...

  6. DAY5(PYTHON) 字典的增删改查和dict嵌套

    一.字典的增删改查 dic={'name':'hui','age':17,'weight':168} dict1={'height':180,'sex':'b','class':3,'age':16} ...

  7. python之路day05--字典的增删改查,嵌套

    字典dic 数据类型划分:可变数据类型,不可变数据类型 不可变数据类型:元组,bool,int str -->可哈希可变数据类型:list,dict,set --> 不可哈希 dict k ...

  8. 字典(dict),增删改查,嵌套

    一丶字典 dict 用{}来表示  键值对数据  {key:value}  唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 二丶字典的增删改查 1.增 dic[k ...

  9. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  10. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

随机推荐

  1. springBoot中实现自定义属性配置、实现异步调用、多环境配置

    springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...

  2. 解析ArcGis的标注(一)——先看看分数式、假分数式标注是怎样实现的

    该“标注”系列博文的标注引擎使用“标准标注引擎(standard label engine)”,这个概念如不知道,可不理会,ArcGis默认标注引擎就是它. ArcGis的标注表达式支持VBScrip ...

  3. 043、data-packed volume container (2019-03-06 周三)

    参考https://www.cnblogs.com/CloudMan6/p/7203285.html     volume container 的数据归根到底还是在host上,我们能不能把数据完全放到 ...

  4. [ASNI C] [常用宏定义] [define技巧]

    1. 打印变量名及其值 #define Inquire(var, mode) fprintf(stdout, #var" = "#mode". \n", var ...

  5. you have mixed tabs and spaces.Fix This屏蔽

    这个功能很影响vs的速度,解决办法(VS2010版本为例),将Fix Mixed Tabs改为OFF即可.

  6. Chrome 浏览器快捷键

    Ø  前言 记录下 Chrome 的快捷键,原文链接:http://www.cnblogs.com/mikalshao/archive/2010/11/03/1868568.html   1.   标 ...

  7. Nginx 关闭日志生成文件

    nginx 关闭日志:其实一种方法就是写入/dev/null 文件 或者设置关闭: nginx 日志有两个类型  access.log  http 记录访问日志. error.log   server ...

  8. 数据库设计理论与实践·<四>数据库基本术语及其概念

    一.关系模型 关系模型是最重要的一种数据模型.关系数据库模型系统采用关系模型作为数据的组织方式. 关系模型的数据结构: 关系:一张表 元组:一行记录. 属性:一列 [码(键,key)]:表中的某个属性 ...

  9. TensorFlow从入门到理解(四):你的第一个循环神经网络RNN(分类例子)

    运行代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # set rando ...

  10. 向dnsrecord.txt 中添加 配置

    #!/bin/bash Ip_addr=`cat /etc/dnsrecord.txt |awk '{print $1}' |head -1` Check_dns_url(){ grep $1 /et ...