字典dict:
1. 什么是字典:
1.1 字典就是我们在其他原因中用到的key:value形式的一种表达形式,例如,在java中有map,javascript的json, redis中的hash等等这些形式,都是换汤不换药。
字典可以存储任意的对象,也可以是不通的数据类型,但是java中的map就不可以。{“name”:“ling”,“age”: 25}, java中一般用类来实现这样的需求。
[{"name": "gavin",
"age": 28,
"address": "河南"
}, {"name": "zhang",
"age": 20,
"address": "上海"
}]
2. 字典的定义方法:
2.1 a = dict ()
a = dict()
print(a)
>>>{}
2.2 x = dict(a=1, b=2)
x = dict(a=1,b=2)
print(x)
>>>{'a': 1, 'b': 2}
2.3 y = {“name”: “lingjing”}
y ={"name":"gavin"}
print(y)
>>>{'name': 'gavin'}
2.4 A = dict([(“name”, “gavin”), (“age”, 28)])
A = dict([("name","gavin"),("age",28)])
print(A)
>>>{'name': 'gavin', 'age': 28}
3. dict常用的方法
3.1 get(k)
返回key所对应的value
x ={"name": "gavin","age": 28}
print(x.get("name"))
print(x.get("age"))
>>>gavin
>>>28
3.2 setdefault(k, v)
如果key有不存在,设置一个默认值v,并返回v 如果k存在,返回k所对应的value
x ={"name": "gavin","age": 28}
print(x.setdefault("name","zhang"))
print(x.setdefault("address","henan"))
print(x)
>>>gavin
>>>henan
>>>{'name': 'gavin', 'age': 28, 'address': 'henan'}
3.3 keys()
获得所有keys
x ={"name": "gavin","age": 28}
print(x.keys())
>>>dict_keys(['name', 'age'])
3.4 values()
获得所有values
x ={"name": "gavin","age": 28}
print(x.values())
>>>dict_values(['gavin', 28])
3.5 iteritems()
iteritems ()代表对象,python2 中使用,循环一次取一次
items是一次性把所有的key:values取出来
for i, j in d.iteritems():print(i,j)
3.6 update() 增加
update相当于+
m.update(n) m,n都是dict
x ={"name": "gavin","age": 28}
y ={"address":"henan"}
x.update(y)
print(x)
>>>{'name': 'gavin', 'age': 28, 'address': 'henan'}
3.7 pop(k)
删除k:v对应的元素
x ={"name": "gavin","age": 28}
print(x.pop("name"))
print(x)
>>>gavin
>>>{'age': 28}
3.8 zip(l1, l2)
高阶函数,dict(zip(l1, l2)) 把l1和l2转换成相对应的字典形式
l1 = ["a", "b", "c"]
l2 = [1, 2, 3]
print(dict(zip(l1,l2)))
>>>{'a': 1, 'b': 2, 'c': 3}
3.9 clear
3.10 mm = dict(a=1,b=10,c=3,d=9)
print sorted(mm.iteritems(), key = lambda d:d[1], reverse = True)
3.11 x = y y变时x也变
x ={"name": "gavin","age": 28}
y = x
print(x.pop("name"))
print(x)
print(y)
>>>gavin
>>>{'age': 28}
>>>{'age': 28}
3.12 x = y.copy() y变时x不变
x ={"name": "gavin","age": 28}
y = x.copy()
print(x.pop("name"))
print(x)
print(y)
>>>gavin
>>>{'age': 28}
>>>{'name': 'gavin', 'age': 28}
3.13 items()
a = dict(x=1,y=2)
for i in a.items():
print(i)
>>>('x', 1)
>>>('y', 2)
a = dict(x=1,y=2)
print(a.items())
>>>dict_items([('x', 1), ('y', 2)])
print(type (a.items))
>>> <class 'builtin_function_or_method'>
3.14 fromkeys(x, "hello")
用列表里的元素作为keys,指定相同的values
m = dict()
x = [1,2,3,4]
print(m.fromkeys(x, "hello"))
>>>{1: 'hello', 2: 'hello', 3: 'hello', 4: 'hello'}
if语句
if expression:
statements(s)
注:Python使用缩进作为其语句分组的方法,建议使用4个空格
1.1 逻辑值(bool)包含了两个值:
1.1.1 true的表示:
表示非空的量(比如:string,tuple,list,set,dictonary),所有非零数。
- 1:
- ‘a’:
- 1<2
- not 1>2
vim if.py
#! /usr/bin/python
if 1:
print 'hello world'
print 'true'
1.1.2 false的表示:
表示0,None,空的量等。
- 0:
- ‘ ’: 空字符串
- 1>2:
vim if.py
#! /usr/bin/python
if 0:
print 'hello world'
print 'true'
else:
print 'false'
1.2 示例
vim if.py
#! /usr/bin/python
score = int(raw_input("Please input a number: "))
if score >= 90:
print 'A'
print 'very good'
elif score >= 80:
print 'B'
print 'good'
elif score >= 60:
print 'C'
print 'pass'
else:
print 'D'
print 'no pass'
注:int('30') 把字符串数字变成数值
2. else语句:
if expression:
statement(s)
else:
statement(s)
3. elif语句:
if expression1:
statement1(s)
elif expression2:
statement2(s)
else:
statement2(s)
3.1 多个条件判断:
方法1:
#! /usr/bin/python
yn = raw_input("Please input [Yes/No]: ")
yn = yn.lower()
if yn == 'y' or yn == 'yes':
print "program is running..."
elif yn == 'no' or yn == 'n':
print "program is exit"
else:
print 'Please input [Yes/No]: '
方法2:
#! /usr/bin/python
yn = raw_input("Please input [Yes/No]: ")
yn = yn.lower()
yes = ('yes', 'y')
no = ('no', 'n')
if yn in yes:
print "program is running..."
elif yn in no:
print "program is exit"
else:
print 'Please input [Yes/No]: '