字典dict:javascript
1. 什麼是字典:java
1.1 字典就是咱們在其餘緣由中用到的key:value形式的一種表達形式,例如,在java中有map,javascript的json, redis中的hash等等這些形式,都是換湯不換藥。python
字典能夠存儲任意的對象,也能夠是不通的數據類型,可是java中的map就不能夠。{「name」:「ling」,「age」: 25}, java中通常用類來實現這樣的需求。redis
1.2 http://www.json.cnexpress
[{"name": "gavin",json
"age": 28,vim
"address": "河南"函數
}, {"name": "zhang",ui
"age": 20,spa
"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 expression:
statements(s)
注:Python使用縮進做爲其語句分組的方法,建議使用4個空格
1.1 邏輯值(bool)包含了兩個值:
1.1.1 true的表示:
表示非空的量(好比:string,tuple,list,set,dictonary),全部非零數。
vim if.py
#! /usr/bin/python
if 1:
print 'hello world'
print 'true'
1.1.2 false的表示:
表示0,None,空的量等。
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]: '