#/usr/bin/env python
#coding=utf-8 #有汉字的时候加上
if 1+2: # 非零非空为真 在if语句中,只会执行一个分支,其他不执行,一个if语句中只有一个else
>>> 10/3
3
>>> 10/3.0
3.3333333333333335
>>> 10//3
3
>>> 10//3.0
3.0
>>> 10%3
1
>>> 2**3
8
>>> 2<<3
16
>>> 2>>3
0
>>> 3&2
2
>>> 3|2
3
>>> 3^2
1
不支持多目运算符 (? : )
#{1..100 + 从1加到100
#$ seq -s + 1 99 | bc }
$python #交互式
>>> range(1,10)
[1,2,3,4,5,6,7,8,9]
>>>range(1,10,2)
[1,3,5,7,9]
>>>range(10,2,2)
[]
>>>range(10,2,-1):
[10,9,8,7,6,5,4,3]
>>>li = [] >>>li = list()
>>>li = [1,2,3,4,5[‘a‘,‘b‘]]
>>>li[2]
3
>>>li[5][0]
‘a‘
>>>li[6]
indexEoor: list out of range
>>>len(li)
5
>>>for i in li:
print i
1
2
3
4
5
[‘a‘,‘b‘]
>>>2 in li
ture
>>>del li[0]
>>>li
[2,3,4,5[‘a‘,‘b‘]]
#(莫有返回值时,就是在原有的列表的改变,如果有返回值,想使用必须先赋值才能用,
#原值不改变)
处理一组有序项目的数据结构
列表是可变类型的数据
使用[]表示列表,包含多个以逗号分隔的数字或字串
列表操作: 内存储存空间相同
索引和切片
添加
删除
修改
查找
>>> li = [1,2,3,4]
>>>li.append(‘a‘)
>>>li
[1,2,3,4,‘a‘]
>>>li.append([5,6])
>>li
[1,2,3,4,‘a‘,[5,6]]
>>>li2 = [‘hello‘,‘world‘]
>>>li2.extend(li)
li bubian
>>>li2
[‘hello‘,‘world‘,1,2,3,4,‘a‘,[5,6]]
>>>li = [1,2,3]
>>>li2 = [4,5,6]
>>>li+li2
[1,2,3,4,5,6]
>>>li.insert(1,100)
>>>li
[1,100,2,3]
>>>li.insert(-1,10)
>>>li # 从后边插入
[1,100,2,10,3]
>>>li.insert(100,10) == li.insert(len(li),10) #规范
[1,100,2,3,10]
>>>li.insert(1,[1,2,3])
>>>li
[1,[1,2,3],100,2,3,10]
>>>li.remove(100)
>>>li
[1,[1,2,3],2,3,10]#只删除一个索引,并且先删除第一个
>>>li.pop()
>>>li
[1,[1,2,3],2,3]
>>>li.count(1)#统计1出现的次数
1
>>>li.index(1) # 统计1的索引
>>>li = [2,1,4,3,6,5]
>>>li.sort(li)
>>>li
[1,2,3,4,5,6] #排序
>>>li.reverse()
>>>li
[6,5,4,3,2,1]
>>>li2 = li[2:4]
[4,3]
>>>li[:3]
[6,5,4]
>>>li[3:]
[3,2,1]
>>>li[:3:2]
[1,3]
>>li[2:4:-1]
[]
>>>li[4:2:-1]
>>>li = [1,2,3,4]
>>>li2 = li
>>>id(li) == id(li2)
>>> li.append(1)
>>>li
[1,2,3,4,1]
>>>li2
[1,2,3,4,1]
python 列表赋值是引用赋值,相当在内存中引用2个索引,改变其中一个,另一个也改变
>>>li = [1,2,3,4]
>>>li2 = li[:]
id(li) != id(li2)
可以用切片解决
列表里面的内容可以改变 t[0]=100
支持下标索引的度是有顺序的
元组
使用()圆括号中用逗号分割的项目定义
元组的值不可改变
内存存储空间是不同的
>>>t = (1,2,3) #元组里面的值 t[0]=**不改变 支持分片 用于固定ip 固定端口
>>> t [1:3]
(2,3)
>>>t = (1,2,3,[1,2,3,4])
>>>t[3][0] = 100
>>> t
(1,2,3,[100,2,3,4])
集合 s = {1,2,3}里面必须要有内容
集合支持去重复的值,根据 集合里面的值的hash值,所以集合里面的值都是可以被哈希的
集合是无需的,所以不能用下标索引查找,不支持切片
>>>s = set()
>>>type(s)
<type ‘set‘>
>>>s = {} #不能这样定义
>>>type(s)
<type ‘dict‘> #字典
>>>s = {1,1,1,2,3,4,5}
>>>s
set([1,2,3,4,5]) # 集合的元素不重复
>>s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘set‘ object does not support indexing
>>>
>>> s = {1,2,3,[1,2]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: ‘list‘
>>>
>>> s = {1,2,3,(1,2)}
>>> s
set([(1, 2), 1, 2, 3])
>>>
>>> li = [1,2,3,4,5]
>>> s = set(li)
>>> s
set([1, 2, 3, 4, 5])
>>> type s
File "<stdin>", line 1
type s
^
SyntaxError: invalid syntax
>>> type(s)
<type ‘set‘>
>>> #list 变 set
>>> li2 = list (s)# set 变 list
>>> li2
[1, 2, 3, 4, 5]
>>>
>>> li = list(set([1,2,3,4]))
>>> li
[1, 2, 3, 4]
>>>
>>> s
set([1, 2, 3, 4, 5])
>>> s.add(100)
>>> s1 = [‘huli‘,4]
>>> s.update(s1)
>>> s
set([1, 2, 3, 4, 5, ‘huli‘, 100])
>>>
>>> s2 = {1,2,7}
>>> s.update(s2)
>>> s
set([1, 2, 3, 4, 5, 7, ‘huli‘, 100])
>>> s4 = (8,9)
>>> s.update(s4)
>>> s
set([1, 2, 3, 4, 5, 7, 8, 9, ‘huli‘, 100])
>>>s.remove(1)
>>>s
set([2,3,4,5,7,8,9,‘huli‘,100])
>>>s.discard(3)
>>>s
set([2,4,5,7,8,9,‘huli‘,100])
>>>s.pop()
>>>s
set([4,5,7,8,9,‘huli‘,100])
>>>s.clear()
>>>s
set([])
python一切皆对象
>>>s={1,2,3,4,5}
>>>s1={2,4,5,6}
>>> s1.difference(s) # == >>>s1-s
set([6])
>>> s1
set([2, 3, 5, 6])
>>> s
set([1, 2, 3, 4, 5])
>>>
>>> s1.intersection(s) # == >>>s1&s
set([2, 3, 5])
>>> s1.difference_update(s)
>>> s1
set([6])
>>>s = {1,2,3}
>>>s1 = {2,3,4}
>>>s.isdisjoint(s1)
False
>>>s.issubset(s1)
False
交集 intersection &
并集 | 都不修改原内容
判断是不是有交集 s.isdisjoint(s1) 莫有ture
s1是不是s的子集 s1.issubset(s)
字典
是 python 中唯一的映射类型(哈希表)无序的
字典对象是可变的,但是字典的键必须是不可变对象,并且一个字典中可以使用不
同类型的键值
keys()返回键列表
values()返回值列表
items()返回包含键值对的元组
>>> type(d)
<type ‘dict‘>
>>> d = {‘name‘:‘tom‘,‘age‘:10}
>>> d2 = {‘name‘:‘xpg‘,‘name‘:‘qq‘}
>>> d2
{‘name‘: ‘qq‘}
>>> d2 = {‘name‘:‘xpg‘,{1,2}:‘qq‘}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: ‘set‘
>>> d2 = {‘name‘:‘xpg‘,[1,2]:‘qq‘}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: ‘list‘
>>> d2 = {‘name‘:‘xpg‘,(1,2):‘qq‘}
>>>
>>> d2
{(1, 2): ‘qq‘, ‘name‘: ‘xpg‘}
>>> d2.keys()
[(1, 2), ‘name‘]
>>>
>>> d2.iterkeys()
<dictionary-keyiterator object at 0x17720a8>
>>> t = d2.iterkeys()
>>> t.next()
(1, 2)
>>> t.next()
‘name‘
>>> t.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> d2.values()
[‘qq‘, ‘xpg‘]
>>> d2
{(1, 2): ‘qq‘, ‘name‘: ‘xpg‘}
>>>
>>> for i in d2.iterkeys():
... print i
...
(1, 2)
name
>>>
>>> d2.items()
[((1, 2), ‘qq‘), (‘name‘, ‘xpg‘)]
>>> for x,y in d2.items():
... print x,y
...
(1, 2) qq
name xpg
>>>
>>> d[‘name‘]
‘tom‘
>>> for i in d.keys():
... print i,d[i]
...
age 10
name tom
>>>
>>> d2.get(‘name‘)
‘xpg‘
>>> d2.get(‘num‘,9)
9
>>> d2
{(1, 2): ‘qq‘, ‘name‘: ‘xpg‘}
>>>
>>> d2.has_key(‘name‘)
True
>>> d2.has_key(‘name2‘)
False
>>>
>>> d
{‘age‘: 10, ‘name‘: ‘tom‘}
>>> d[‘phone‘] = 111
>>> d
{‘phone‘: 111, ‘age‘: 10, ‘name‘: ‘tom‘}
>>> d[‘name‘] = ‘jerry‘
>>> d
{‘phone‘: 111, ‘age‘: 10, ‘name‘: ‘jerry‘}
>>>
>>> d2 = {‘c‘:2,‘s‘:3}
>>> d3 = d2
>>> id(d3)
23222480
>>> id(d2)
23222480
>>> d3
{‘c‘: 2, ‘s‘: 3}
>>> d2
{‘x‘: 4, ‘c‘: 2, ‘s‘: 3}
>>> d4 = d2.copy()
>>> d4
{‘x‘: 4, ‘c‘: 2, ‘s‘: 3}
>>> d2
{‘x‘: 4, ‘c‘: 2, ‘s‘: 3}
>>> id(d4)
23213856
>>> id(d2)
23222480
>>>
>>> li
[1, 2, 3]
>>> j = 0
>>> for i in li:
... li[j] = i+1
... j += 1
...
>>> li
[2, 3, 4]
>>> li
[2, 3, 4]
>>> li2 = [i for i in li]
>>> li2
[2, 3, 4]
>>> id(li)
24512560
>>> id(li2)
24512632
>>> li
[2, 3, 4]
>>> li2 = [i+1 for i in li]
>>> li2
[3, 4, 5]
>>> id(li2)
24580968
>>> id(li)
24512560
>>> li3 = []
>>> for i in li:
... li3.append(i+1)
...
>>> li3
[3, 4, 5]
迭代器 特点 可以直接执行 .net 每执行一次有一个结果
for i in 迭代器:遇到stop退出
迭代器性能高于for 循环
for 循环作用就是自动执行迭代器
生成器 it = (i+1 for i in li)不执行 当执行.net时才执行 相当于迭代器
>>> li
[2, 3, 4]
>>> it = (i+1 for i in li)
>>>
>>> it
<generator object <genexpr> at 0x176d730>
>>> it.next()
3
>>> it.next()
4
>>> it.next()
5
>>> it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
列表是一次性全给你
当数据量大时,生成器性能好
列表解析性能远高于for循环
迭代器(惰性)性能远高于与列表解析
>>> li
[2, 3, 4]
>>> li2 = [i+1 for i in li if i%2 ==0 ]
>>> li2
[3, 5]
>>> li3 = []
>>> for i in li:
... if i %2 == 0:
... li3.append(i+1)
...
>>> li3
[3, 5]
>>>
本文出自 “11716212” 博客,请务必保留此出处http://11726212.blog.51cto.com/11716212/1789071
原文地址:http://11726212.blog.51cto.com/11716212/1789071