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 ...
随机推荐
- 文档在线预览开源实现方案三:OpenOffice + PDFRenderer + js
之前的方案无法很好地解决异构平台及不同浏览器的兼容性问题,如方案一需要客户端浏览器支持flash而移动端浏览器无法支持这点,虽然移动端浏览器支持方案二,但是一些老版本的IE浏览器无法支持,例如IE8就 ...
- Kmplayer播放器 绿色免安装版 2016 中文版
软件名称: Kmplayer播放器 绿色免安装版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 42.8MB 图片预览: 软件简介: Kmplayer播放 ...
- python第五天
反射 hasattr,getattr class Foo: static_name = 'nba' def __init__(self): self.name = 'alex' def show(se ...
- 改造jQuery-Tagit 插件支持中文全角的逗号和空格
jQuery 的 tagit 插件效果还是不错的,今天用到该插件但发现不能自定义标签分隔符,只能是英文半角逗号或空格,于是想改造下 效果: 先研究了一番插件的代码,发现并不能通过插件自身的扩展方法来实 ...
- HDU 5895 Mathematician QSC
矩阵快速幂,欧拉定理. $g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂. #pragma comment( ...
- python向服务器发送邮件事例
import osimport sysimport re __author__ = 'xiaoming' import requestststr = '<div>\n<ul>\ ...
- KMP 算法 学习 整理
我自己整理的KMP算法的PDF文件:http://pan.baidu.com/s/1o8yKIi2提取密码:8291 别的就不多说啥了,感谢来自海子 博客园的 资料--
- spring定时任务的几种实现方式
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...
- 定时发布任务,在global.asax中获取文件的物理路径的方法
如果要把一个相对路径或者虚拟路径映射道服务器的物理路径,通常会使用Server.MapPath()函数,比如将根目录下的html目录映射为物理路径:Server.MapPath("html& ...
- sql中判断是否存在某个对象
If object_id(N'对象名',N'对象类型') is not null 例如:表是否存在 if object_id(N'tablename',N'U') is not null begi ...