Python之列表&元组&字典
今天学习了Python的基本数据类型,做以下笔记,以备查用。
一、列表
列表的常用方法:
1、append()方法
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -- append object to end """
pass
append()方法可以在列表尾部追加一个元素,该方法每次只能接受一个参数,并且该参数可以是任何数据类型。
e:
>>> a = [1,2,3,4,5,6]
>>> a.append(7)
>>> a
[1, 2, 3, 4, 5, 6, 7]
2、extend()方法
def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -- extend list by appending elements from the iterable """
pass
extend()方法的参数是一个列表,并将该列表中的每个元素追加到列表中。
e:
>>> b = [1,2,3,4]
>>> b.extend([5,6,7])
>>> b
[1, 2, 3, 4, 5, 6, 7]
3、index()方法
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
index()方法返回其参数在列表中的位置,(元素索引从0开始)
e:
>>> a = [1,4,7,9,3,6]
>>> a.index(3)
4
4、insert()方法
def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" L.insert(index, object) -- insert object before index """
pass
insert()方法向列表中插入一个值,接受两个参数,第一个参数指定新插入值的位置,第二个参数为插入的值。
e:
>>> a = [1,3,5,8,0]
>>> a.insert(2,22)
>>> a
[1, 3, 22, 5, 8, 0]
5、pop()方法
def pop(self, index=None): # real signature unknown; restored from __doc__
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
pop()方法接受一个参数,删除列表中以该参数为索引的值,然后返回被删除的值。若不带参数,默认删除列表中最后一个元素。
e:
>>> a = [1,2,3,4,5]
>>> a.pop(3)
4
>>> a
[1, 2, 3, 5]
6、remove()方法
def remove(self, value): # real signature unknown; restored from __doc__
"""
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
remove()方法接受一个参数,删除列表中对应的值。
e:
>>> a
[1, 2, 3, 5]
>>> a.remove(5)
>>> a
[1, 2, 3]
7、count()方法
def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0
count()方法接受一个参数,返回该参数在列表中的个数。
e:
>>> a = [1,3,5,7,3,2,4,5]
>>> a.count(3)
2
>>> a.count(5)
2
8、reverse()方法
def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass
reverse()方法没有参数,对列表中的元素方向排序
e:
>>> a = [1,2,3,4,5]
>>> a.reverse()
>>> a
[5, 4, 3, 2, 1]
9、sort()方法
def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
"""
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
"""
pass
sort()方法对列表中的元素进行排序
e:
>>> a = [2,5,8,3,4,9,1]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 8, 9]
二、元组
元组里的元素不可修改,元素的元组可以修改。
e:
元素不可修改:
>>> a = (1,2,3,[1,2,3])
>>> a[0]=4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
元素包含的元组可以修改:
>>> a = (1,2,3,[1,2,3])
>>> a[3]
[1, 2, 3]
>>> a[3][0]=4
>>> a
(1, 2, 3, [4, 2, 3])
元组的常用方法:
1、count()方法
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0
count()方法接受一个参数,返回该参数在元组里的个数
e:
>>> a = (1,2,3,4,5,2,5,3,4,2,6,)
>>> a.count(2)
3
>>> a.count(5)
2
2、index()方法
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
index()方法接受一个参数,返回该参数在元组第一次出现时的位置(索引)
e:
>>> a = (1,2,3,4,5,2,5,3,4,2,6,)
>>> a.index(4)
3
三、字典
字典的常用方法:
1、clear()方法
def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
clear()方法清空字典里的所有元素。
e:
>>> d = {1:'ahaii',2:'nancy',3:'tom'}
>>> d.clear()
>>> d
{}
2、copy()方法
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
pass
copy()方法对字典进行浅复制。
e:
>>> d = {1:'ahaii',2:'nancy',3:'tom'}>>> a = d.copy()
>>> a
{1: 'ahaii', 2: 'nancy', 3: 'tom'}
3、get()方法
def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
get()方法返回指定键的值,若改键不存在,则返回默认值或空。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
>>> a.get(2)
'nancy'
>>> a.get(4)
>>>
4、has_key()方法
def has_key(self, k): # real signature unknown; restored from __doc__
""" D.has_key(k) -> True if D has a key k, else False """
return False
has_key()方法判断参数是否包含在字典的key中,若存在,返回True,若不存在,返回False。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}>>> a.has_key(1)
True
>>> a.has_key(4)
False
5、items()方法
def items(self): # real signature unknown; restored from __doc__
""" D.items() -> list of D's (key, value) pairs, as 2-tuples """
return []
items()方法返回一个列表,列表中的每个元素是字典的一个键值对组成的元组。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
>>> a.items()
[(1, 'ahaii'), (2, 'nancy'), (3, 'tom')]
6、keys()方法
def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> list of D's keys """
return []
keys()方法返回一个列表,该列表包含字典所有的key。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
>>> a.keys()
[1, 2, 3]
7、pop()方法
def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass
pop()方法必须有一个参数(注意与列表中pop()方法的区别),并删除该参数在字典中对应的值。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
>>> a.pop(1)
'ahaii'
>>> a
{2: 'nancy', 3: 'tom'}
8、values()方法
def values(self): # real signature unknown; restored from __doc__
""" D.values() -> list of D's values """
return []
values()方法返回包含字典所有值的列表。
e:
>>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
>>> a.values()
['ahaii', 'nancy', 'tom']
9、itmes()方法
使用items()方法可以取字典中的键值对。
e:
dic = {'':'qwe','':'uyd','':'uhd'}
for k,v in dic.items():
print k,v
Python之列表&元组&字典的更多相关文章
- python中列表 元组 字典 集合的区别
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...
- **python中列表 元组 字典 集合
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. 1.列表 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔. 列表的特 ...
- Python学习-列表元组字典操作
一.列表 列表是Python的基本数据类型之一,它是以 [] 括起来的,内部成员用逗号隔开.里面可以存放各种数据类型. # 例如: list2 = ['jason', 2, (1, 3), ['war ...
- python字符串/列表/元组/字典之间的相互转换(5)
一.字符串str与列表list 1.字符串转列表 字符串转为列表list,可以使用str.split()方法,split方法是在字符串中对指定字符进行切片,并返回一个列表,示例代码如下: # !usr ...
- python 中列表 元组 字典 集合的区别
先看图片解释 (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复 ...
- python字符串 列表 元组 字典相关操作函数总结
1.字符串操作函数 find 在字符串中查找子串,找到首次出现的位置,返回下标,找不到返回-1 rfind 从右边查找 join 连接字符串数组 replace 用指定内容替换指定内容,可以指定次数 ...
- python基础-列表元组字典
1.列表和元组 列表可以对数据实现最方便的存储.修改等操作 names=["Alex","tenglan","Eric","Rai ...
- 【python】列表&&元组&&字典
列表:用“[]”包裹,可对值增删改. 列表遍历: 方法一: alist=["a","b","c","d","e ...
- python的列表 元组 字典
列表和元组都是序列,是数据元素的集合,数据元素可以是数值.字符串,布尔值.对象等. 一.列表:用方括号定义[] 空列表 names = [] 带值的列表 names = ["bill&quo ...
随机推荐
- mariadb 设置远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; flush privileges; ...
- CSS3 六边形绘制
把一个104px的div放在它们之间,设置一个背景颜色: width: 0; border-bottom: 30px solid #6C6; border-left: 52px solid trans ...
- git add -f
git add -f 添加已被 .gitignore 忽略的文件/文件夹
- 链表中LinkList L与LinkList *L 借鉴
链表中LinkList L与LinkList *L的区别以及(*L).elem,L.elem L->next,(*L)->next的区别typedef struct Node{int el ...
- Apache Storm简介
Apache Storm简介 Storm是一个分布式的,可靠的,容错的数据流处理系统.Storm集群的输入流由一个被称作spout的组件管理,spout把数据传递给bolt, bolt要么把数据保存到 ...
- Gentoo双网卡同时启用上内外网
引言:本文配置网络通过 OpenRC/netifrc 方法(net.*scritps)配置. 外网网卡:enp3s4 内网网卡:enp2s0 外网地址(通过路由器) IP: 192.168.1.10 ...
- windows下寻找端口
netstat -aon|findstr "5037" 找到占用5037的进程号: 根据进程号杀死进程 taskkill /pid 5136 /f tasklist|findstr ...
- matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)”
matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)” 解决办法:traindata ...
- sql第二天
--基本格式 select * from tblclass --对于列进行限制 --格式一:取指定列 select cid,cname from TblClass select cname from ...
- The certificate that was used has a trust chain that cannot be verified问题
今天调用wcf程序的时候发现证书有问题.报的错误如下 The certificate that was used has a trust chain that cannot be verified. ...