列表(list)

赋值方法:

l = [11,45,67,34,89,23]

l = list()

列表的方法:

 #!/usr/bin/env python

 class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
"""
def append(self, p_object): # real signature unknown; restored from __doc__
'''在列表末尾添加一个新的对象'''
""" L.append(object) -> None -- append object to end """
pass def clear(self): # real signature unknown; restored from __doc__
'''清空列表中的所有对象'''
""" L.clear() -> None -- remove all items from L """
pass def copy(self): # real signature unknown; restored from __doc__
'''拷贝一个新的列表'''
""" L.copy() -> list -- a shallow copy of L """
return [] def count(self, value): # real signature unknown; restored from __doc__
'''某个元素在列表中出现的次数'''
""" L.count(value) -> integer -- return number of occurrences of value """
return 0 def extend(self, iterable): # real signature unknown; restored from __doc__
'''在列表的末尾追加另外一个列表的多个值'''
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass 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 def insert(self, index, p_object): # real signature unknown; restored from __doc__
'''指定位置插入元素'''
""" L.insert(index, object) -- insert object before index """
pass 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 def remove(self, value): # real signature unknown; restored from __doc__
'''移除列表中给定值的第一次出现的元素'''
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass def reverse(self): # real signature unknown; restored from __doc__
'''反转列表'''
""" L.reverse() -- reverse *IN PLACE* """
pass def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
'''对列表中的元素排序'''
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass

list

方法示例:

####
append()
>>> l = [11,45,67,34,89,23]
>>> l.append(44)
>>> l
[11, 45, 67, 34, 89, 23, 44]
####
>>> l
[1, 4, 7, 11, 23, 34, 34, 44, 44, 45, 67, 89]
>>> l.clear()
>>> l
[]
####
copy()
>>> l
[11, 45, 67, 34, 89, 23, 44]
>>> i = l.copy()
>>> i
[11, 45, 67, 34, 89, 23, 44]
####
count()
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34]
>>> l.count(44)
3
####
extend()
>>> i = [1,4,7,6]
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34]
>>> l.extend(i)
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7, 6]
####
indexi()
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7, 6]
>>> l.index(44)
6
>>> l.index(45)
1
####
pop()
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7, 6]
>>> l.pop()
6
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7]
####
remove()
>>> l
[11, 45, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7]
>>> l.remove(45)
>>> l
[11, 67, 34, 89, 23, 44, 44, 44, 45, 34, 1, 4, 7]
>>> l.remove(44)
>>> l
[11, 67, 34, 89, 23, 44, 44, 45, 34, 1, 4, 7]
####
reverse()
>>> l
[11, 67, 34, 89, 23, 44, 44, 45, 34, 1, 4, 7]
>>> l.reverse()
>>> l
[7, 4, 1, 34, 45, 44, 44, 23, 89, 34, 67, 11]
####
sort()
>>> l
[7, 4, 1, 34, 45, 44, 44, 23, 89, 34, 67, 11]
>>> l.sort()
>>> l
[1, 4, 7, 11, 23, 34, 34, 44, 44, 45, 67, 89]
####
 
 
元组:
 元组中的元素是不可以改变的。
赋值方法:
tup = 'a','b','c'
tup = ('a', 'b', 'c')

元组的方法:

 #!/usr/bin/env python
class tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
'''某个元素在元素中出现的次数'''
""" T.count(value) -> integer -- return number of occurrences of value """
return 0 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 def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass

tuple

方法示例:

####

count()

>>> tup = ('a','b','c','b')
>>> tup.count('b')
2

 ####
index()
>>> tup = ('a','b','c','b')

>>> tup.index('b')
1

 ###
 
 
 
 
 
字典:
 字典(dict):字典为一对键(key)和值(value)的对应关系,中间使用“:”分隔开。
key在字典中是唯一的,字典是无序的。
赋值字典:
dic = {'k1':'v1','k2':'v2','k3':'v3'}
 
字典的方法:
 #!/usr/bin/env python
class dict(object):
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
"""
def clear(self): # real signature unknown; restored from __doc__
'''清空字典'''
""" D.clear() -> None. Remove all items from D. """
pass def copy(self): # real signature unknown; restored from __doc__
'''拷贝字典,浅拷贝'''
""" D.copy() -> a shallow copy of D """
pass @staticmethod # known case
def fromkeys(*args, **kwargs): # real signature unknown
'''首先有一个列表,这个列表将作为一个字典的key,如果不给值则所有key的值为空,如果给值就将值设置为key的值'''
""" Returns a new dict with keys from iterable and values equal to value. """
pass def get(self, k, d=None): # real signature unknown; restored from __doc__
'''根据key取值,如果没有这个key,不返回值'''
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass def items(self): # real signature unknown; restored from __doc__
'''所有key和值组成列表的形式'''
""" D.items() -> a set-like object providing a view on D's items """
pass def keys(self): # real signature unknown; restored from __doc__
'''所有key组成列表的形式'''
""" D.keys() -> a set-like object providing a view on D's keys """
pass def pop(self, k, d=None): # real signature unknown; restored from __doc__
'''获取key的值,并从字典中删除'''
"""
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 def popitem(self): # real signature unknown; restored from __doc__
'''获取键值对,并在字典中删除,随机的'''
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
'''如果key不存在,则创建,如果key存在则返回key的值,不会修改key的值'''
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
pass def update(self, E=None, **F): # known special case of dict.update
'''更新'''
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
pass def values(self): # real signature unknown; restored from __doc__
'''所有值的列表形式'''
""" D.values() -> an object providing a view on D's values """
pass def __contains__(self, *args, **kwargs): # real signature unknown
""" True if D has a key k, else False. """
pass def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
"""
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
# (copied from class doc)
"""
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass def __sizeof__(self): # real signature unknown; restored from __doc__
""" D.__sizeof__() -> size of D in memory, in bytes """
pass __hash__ = None

dict

方法示例:

####
clear()
>>> dic
{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
>>> dic.clear()
>>> dic
{}
####
copy()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic2 = dic.copy()
>>> dic2
{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
####
>>> l = [2,3,5,6,7]
>>> d = dict.fromkeys(l)
>>> d
{2: None, 3: None, 5: None, 6: None, 7: None}
>>> d = dict.fromkeys(l,'a')
>>> d
{2: 'a', 3: 'a', 5: 'a', 6: 'a', 7: 'a'}
####
items()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic.items()
dict_items([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
####
keys()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic.keys()
dict_keys(['k1', 'k2', 'k3'])
####
pop()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic.pop('k2')
'v2'
>>> dic
{'k1': 'v1', 'k3': 'v3'}
####
popitme()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic.popitem()
('k2', 'v2')
>>> dic
{'k1':'v1','k3':'v3'}
####
setdefault()
>>> dic = {'k1':'v1','k2':'v2','k3':'v3'}
>>> dic.setdefault('k2')
'v2'
>>> dic.setdefault('k4','v4')
'v4'
>>> dic
{'k1': 'v1', 'k4': 'v4', 'k2': 'v2', 'k3': 'v3'}
####
update()
>>> dic
{'k1': 'v1', 'k4': 'v4', 'k2': 'v2', 'k3': 'v3'}
>>> dic2
{'k5': 'v5'}
>>> dic.update(dic2)
>>> dic
{'k1': 'v1', 'k5': 'v5', 'k4': 'v4', 'k2': 'v2', 'k3': 'v3'}
>>> dic2
{'k5': 'v5'}
####
values()
>>> dic
{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
>>> dic.values()
dict_values(['v1', 'v2', 'v3'])
####
 
 
 

python基础知识-列表,元组,字典的更多相关文章

  1. python的学习笔记01_4基础数据类型列表 元组 字典 集合 其他其他(for,enumerate,range)

    列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问 ...

  2. Python入门基础学习(列表/元组/字典/集合)

    Python基础学习笔记(二) 列表list---[ ](打了激素的数组,可以放入混合类型) list1 = [1,2,'请多指教',0.5] 公共的功能: len(list1) #/获取元素 lis ...

  3. Day2 - Python基础2 列表、字典、集合

    Python之路,Day2 - Python基础2   本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一, ...

  4. python基础之列表、字典、元祖等 (二)

    一.作用域 if 1==1: name = 'weibinf' print name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 result = 值1 ...

  5. python基础(五)列表,元组,集合

    列表 在python中是由数个有序的元素组成的数据结构,每一个元素对应一个index索引来隐式标注元素在列表中的位置.是python中最常用的一种数据类型.需要注意的是列表中可以有重复相同的数据. 列 ...

  6. Python基础知识之2——字典

    字典是什么? 字典是另外一个可变的数据结构,且可存储任意类型对象,比如字符串.数字.列表等.字典是由关键字和值两部分组成,也就是 key 和 value,中间用冒号分隔.这种结构类似于新华字典,字典中 ...

  7. Python基础 之列表、字典、元组、集合

    基础数据类型汇总 一.列表(list) 例如:删除索引为奇数的元素 lis=[11,22,33,44,55] #第一种: for i in range(len(lis)): if i%2==1: de ...

  8. python 基础知识 列表的 增删改查 以及迭代取值

    """ python 列表 通用方法 元组.数组.字典 取值方法 [] 列表中可以存储不同类型的数据 函数 封装了独立的功能可以直接调用 函数名(参数) 方法 和函数类似 ...

  9. Python 基础-python-列表-元组-字典-集合

    列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...

随机推荐

  1. Centos 6.X noVNC+websockify 实现webvnc

    文章参考:https://github.com/kanaka/noVNC http://www.cnblogs.com/yanghuahui/p/3574388.html 工作原理: noVNC 可以 ...

  2. BillBoardView自己定义控件广告板轮播

    BillBoardView自己定义控件广告板轮播 GitHub地址 compile 'com.march.billboardview:billboardview:2.0.6-beta4' BillBo ...

  3. 【转】windows下 ADT NDK开发环境配置

    前提: 下载好Ecplise ADT并配置好开发环境,不会配置环境可以参考这里: http://blog.csdn.net/danfengw/article/details/47111107 步骤: ...

  4. SERVICE_STATUS结构各成员解析

    在编写Windows服务的时候,需要调用API函数::SetServiceStatus()向服务控制管理器(SCM)提交更新当前服务的状态信息,其第2个参数为指向SERVICE_STATUS结构的指针 ...

  5. Unix环境高级编程第三版中实例代码如何在自己的linux上运行的问题

    学习Linux已经有2个月了,最近被期末考试把进度耽误了,前几天把Unix环境高级编程看了两章,感觉对Linux的整体有了一些思路,今天尝试着对第一章涉及到的一个简单的交互式shell编译运行一下,结 ...

  6. 九度OJ 1167:数组排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5395 解决:1715 题目描述: 输入一个数组的值,求出各个值从小到大排序后的次序. 输入: 输入有多组数据. 每组输入的第一个数为数组的 ...

  7. CentOS、乌班图设置固定静态IP

    CentOS.乌班图设置固定静态IP 一.centOS 1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2,在文件 ...

  8. Discuz!支持发布视频帖子设定 + 修改后台文件

    最近想做一个地方性论坛,果断在阿里巴巴的phpwind论坛程序与腾讯旗下的discuz论坛程序中选择,虽然phpwind大气,后面还是 决定选择了discuz程序用来构建这个平台,经过一番安装后,发现 ...

  9. Java拓展教程:文件DES加解密

    Java拓展教程:文件加解密 Java中的加密解密技术 加密技术根据一般可以分为对称加密技术和非对称加密技术.对称加密技术属于传统的加密技术,它的加密和解密的密钥是相同的,它的优点是:运算速度快,加密 ...

  10. Object.create用法

    用法: Object.create(object, [,propertiesObject]) 创建一个新对象,继承object的属性,可添加propertiesObject添加属性,并对属性作出详细解 ...