python之3内置容器
所谓内置容器,就是不需要第三方模块,就可以使用的.
>>> li = []
>>> type(li)
<type 'list'>
>>> li = [1,2,3]
>>> li
[1, 2, 3]
len()可以统计list的长度
>>> li
[1, 2, 3]
>>> li[2]
3
>>> len(li)
3
>>> for x in li:
print x
1
2
3
>>> 4 in li
False
>>> 1 in li
True
>>> del li[2]
>>> li
[1, 2]
>>> dir(li)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> li.append(3)
>>> li
[1, 2, 3]
>>> li.extend([4,5,6])
>>> li
[1, 2, 3, 4, 5, 6]
>>> li.extend([range(7,11)])
>>> li
[1, 2, 3, 4, 5, 6, [7, 8, 9, 10]]
>>> li.insert(0,0)
>>> li
[0, 1, 2, 3, 4, 5, 6]
>>> li
[0, 1, 2, 3, 4, 5, 6]
>>> li.index(3,0,2)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
li.index(3,0,2)
ValueError: 3 is not in list
>>> li.index(3,0,5)
3
>>> li.reverse()
>>> li
[6, 5, 4, 3, 2, 1, 0]
>>> li = [1,2,3,4,5]
>>> type(li)
<type 'list'>
>>> li[2:4]
[3, 4]
>>> li[3:]
[4, 5]
>>> li[:3]
[1, 2, 3]
>>> li[:]
[1, 2, 3, 4, 5]
>>> li[0:-2]
[1, 2, 3]
>>> li[1:3:2]
[2]
#取得奇数
>>> li[::2]
[1, 3, 5]
#取得偶数
>>> li[1::2]
[2, 4]
>>> t1 = ([1,2,3],['a','b','c'])
>>> type(t1)
<type 'tuple'>
>>> t1[0][0] = 100
>>> t1
([100, 2, 3], ['a', 'b', 'c'])
定义单元素元组的时候,元素后面需要加逗号,如果不加则会定义成对应的元素类型,例如:
>>> a = (0)
>>> type(a)
<type 'int'>
>>> a = (0,)
>>> type(a)
<type 'tuple'>
>>> hash(t1)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
hash(t1)
TypeError: unhashable type: 'list'
>>> s1 = {t1}
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
s1 = {t1}
TypeError: unhashable type: 'list'
>>> s1 = {1,2,3}
>>> s2 = {2,3,4}
>>> s1.difference (s2)
set([1])
>>> s2.difference (s1)
set([4])
>>> s2.symmetric_difference (s1)
set([1, 4])
>>>
>>> s1
set([1, 2, 3])
>>> tuple(s1)
(1, 2, 3)
>>> list(s1)
[1, 2, 3]
>>> li1=[1,1,2,2,3,3]
>>> li1=list(set(li1))
>>> li1
[1, 2, 3]
>>> li2=[1,2,3,4]
>>> li2=iter (li2)
>>> li2.next()
1
>>> li2.next()
2
>>> li2.next()
3
>>> li2.next()
4
>>> li2.next()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
li2.next()
StopIteration
for语句就是对next方法的调用,当捕获到StopIteration错误的时候,就停止了
>>> d1 = {}
>>> type (d1)
<type 'dict'>
>>> d1 = {'d2':2,'d1':1}
>>> d1
{'d2': 2, 'd1': 1}
>>> type (d1)
<type 'dict'>
>>> d1.keys()
['d2', 'd1']
>>> it = d1.iterkeys()
>>> it.next()
'd2'
>>> it.next()
'd1'
>>> d1.viewkeys()
dict_keys(['d2', 'd1'])
>>> d1.items()
[('d2', 2), ('d1', 1)]
>>> it1=d1.iteritems()
>>> it1.next()
('d2', 2)
>>> v11=d1.viewitems()
>>> v11
dict_items([('d2', 2), ('d1', 1)])
>>> for k,v in d1.items():
print "%s => %s" %(k,v)
d2 => 2
d1 => 1
>>> d1
{'d2': 2, 'd1': 1}
>>> d1.has_key('d3')
False
>>> d1.has_key('d1')
True
>>> d1.get('d1')
1
>>> g1=d1.get('d3')
>>> print g1
None
>>> d1.get('d3','false')
'false'
>>> d1['d3']=3
>>> d1
{'d2': 2, 'd3': 3, 'd1': 1}
>>> d1.update({'d4':4,'d5':5})
>>> d1
{'d4': 4, 'd5': 5, 'd2': 2, 'd3': 3, 'd1': 1}
>>> d1.update(d5=5,d6=6)
>>> d1
{'d5': 5, 'd6': 6, 'd4': 4, 'd5': 5, 'd2': 2, 'd3': 3, 'd1': 1}
>>> d1
{'d6': 6, 'd4': 4, 'd5': 5, 'd2': 2, 'd3': 3, 'd1': 1}
>>> type(d1)
<type 'dict'>
>>> d2=d1.copy()
>>> d2
{'d6': 6, 'd4': 4, 'd5': 5, 'd2': 2, 'd3': 3, 'd1': 1}
>>> l1=[1,2,3,4,5]
>>> [x for x in l1]
[1, 2, 3, 4, 5]
>>> [x+1 for x in l1]
[2, 3, 4, 5, 6]
>>> l1=[1,2,3,4,5]
>>> i1=(x+1 for x in l1)
>>> i1
<generator object <genexpr> at 0x029D29E0>
>>> i1.next()
2
>>> i1.next()
3
>>> i1.next()
4
>>> i1.next()
5
>>> i1.next()
6
>>> i1.next()
Traceback (most recent call last):
File "<pyshell#134>", line 1, in <module>
i1.next()
StopIteration
列表和迭代器的区别就在于,迭代器的输出是可控制的
>>> [x for x in l1 if x%2==0]
[2, 4]
>>> l1=[1,2]
>>> l2=[3,4]
>>> [(x,y) for x in l1 for y in l2]
[(1, 3), (1, 4), (2, 3), (2, 4)]
>>> [x*y for x in l1 for y in l2]
[3, 4, 6, 8]
l1=[1,2]
l2=[3,4]
ret=[]
for x in l1:
for y in l2:
ret.append(x*y)
#!/usr/bin/env python
# encoding:utf-8
#定义第一行列表
li = [1]
#定义总行号
num1 = int(raw_input("请输入行号:"))
#开始循环,row表示每次循环时的行号
for row in range(num1):
#输出每行前的空格,K表示每次输出的个数
for K in range(num1-1):
print ' ',
num1 = num1-1
#输出上次循环所获的行数据,n表示每行的各个元素
for n in iter(li):
print "%s " %n,
#打印每行末的换行符
print
#获取本次循环所得到的行数据,并更新li列表,便于下次循环前输出,zip()函数可以将两个列表,重组成[(x,y)]形式的列表.临时列表a>的目的是为了交错相加,实现杨辉三角的定律,构建下一行列表
a = [0]
a.extend(li)
li.append(0)
li=[x+y for x,y in zip(a,li)]
[root@bogon ~]# python test4.py
请输入行号:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

在交互式输入的时候,建议使用raw_input,而不是input.
来源: <http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html>
关于添加[0]元素这个,不建议用+号直接添加,即[0]+li,li+[0],因为如此以来,会返回两个新的列表,占用额外内存.python之3内置容器的更多相关文章
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- Python中的内置函数__init__()的理解
有点意思,本来我是学习java的.总所周知,java也有构造函数,而python在面向对象的概念中,也有构造函数.它就是 __init__(self) 方法. 其实类似于__init__()这种方法, ...
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
前言:本项目基于maven构建 spring-boot项目可以快速构建web应用,其内置的tomcat容器也十分方便我们的测试运行: spring-boot项目需要部署在外部容器中的时候,spring ...
- python字符串常用内置方法
python字符串常用内置方法 定义: 字符串是一个有序的字符的集合,用与存储和表示基本的文本信息. python中引号中间包含的就是字符串. # s1='hello world' # s2=&quo ...
- python学习交流 - 内置函数使用方法和应用举例
内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...
- python常用的内置函数哈哈
python常用的内置函数集合做一个归类用的时候可以查找 abs 返回数字x的绝对值或者x的摸 all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为true,就 ...
- python常用的内置函数
python常用的内置函数集合做一个归类用的时候可以查找- abs 返回数字x的绝对值或者x的摸 - all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为tru ...
- python字符串处理内置方法一览表
python字符串处理内置方法一览表 序号 方法及描述 1 capitalize()将字符串的第一个字符转换为大写 2 center(width, fillchar) 返回一个指定的宽度 widt ...
- 十六. Python基础(16)--内置函数-2
十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...
随机推荐
- Python操作 Memcache、Redis、RabbitMQ、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- Halloween party
https://www.hackerrank.com/challenges/halloween-party def main(): t = int(raw_input()) for _ in rang ...
- 最长公共子序列--nyoj36
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- Log4net从下载到使用例子
一.首先下载log4net.dll http://pan.baidu.com/s/1gdigrwJ 二.添加log4net引用 三.代码: using System; using System.C ...
- 树状数组(BIT)
i的二进制的最后一个1可以通过i&(-i)得到,时间复杂度o(logn).对于W*H的二维BIT只需要建立H个大小为x轴方向元素个数W的BIT,复杂度O(logW+logH).同样的方法可以扩 ...
- 【转】NAT路由器打洞原理
什么是打洞,为什么要打洞 由于Internet的快速发展 IPV4地址不够用,不能每个主机分到一个公网IP 所以使用NAT地址转换. 下面是我在网上找到的一副图 一般来说都是由私网内主机(例如上图中“ ...
- 一个sigaction的C++ wrap
在上一篇文章(http://www.cnblogs.com/coding-my-life/p/4220128.html)中,提到了libev提供了处理信号的C++ wrap.但我显然接受不了需要进入l ...
- NYOJ130 同样的雪花 【Hash】
同样的雪花 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 You may have heard that no two snowflakes are alike. ...
- iOS开发- 界面传值(1)-通知模式(广播)
之后的几篇博客, 记录下不同界面间传值的经常使用办法. 这篇文章记录广播的方式. iOS的设计模式中,通知模式也是当中重要的模式之中的一个,Notification直译为通知,事实上本人认为叫做广播模 ...
- 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service ...