2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -14,7 +14,7 @@
In [1]:
```
```py
a = {}
type(a)
@@ -22,13 +22,13 @@ type(a)
Out[1]:
```
```py
dict
```
In [2]:
```
```py
a = dict()
type(a)
@@ -36,7 +36,7 @@ type(a)
Out[2]:
```
```py
dict
```
@@ -46,7 +46,7 @@ dict
In [3]:
```
```py
a["one"] = "this is number 1"
a["two"] = "this is number 2"
a
@@ -55,7 +55,7 @@ a
Out[3]:
```
```py
{'one': 'this is number 1', 'two': 'this is number 2'}
```
@@ -63,14 +63,14 @@ Out[3]:
In [4]:
```
```py
a['one']
```
Out[4]:
```
```py
'this is number 1'
```
@@ -78,7 +78,7 @@ Out[4]:
In [5]:
```
```py
a["one"] = "this is number 1, too"
a
@@ -86,7 +86,7 @@ a
Out[5]:
```
```py
{'one': 'this is number 1, too', 'two': 'this is number 2'}
```
@@ -96,7 +96,7 @@ Out[5]:
In [6]:
```
```py
b = {'one': 'this is number 1', 'two': 'this is number 2'}
b['one']
@@ -104,7 +104,7 @@ b['one']
Out[6]:
```
```py
'this is number 1'
```
@@ -114,24 +114,24 @@ Out[6]:
In [7]:
```
```py
print a
```
```
```py
{'two': 'this is number 2', 'one': 'this is number 1, too'}
```
In [8]:
```
```py
print b
```
```
```py
{'two': 'this is number 2', 'one': 'this is number 1'}
```
@@ -140,13 +140,13 @@ print b
In [9]:
```
```py
# 会报错
a[0]
```
```
```py
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-9-cc39af2a359c> in <module>()
@@ -164,7 +164,7 @@ KeyError: 0
In [10]:
```
```py
synonyms = {}
synonyms['mutable'] = ['changeable', 'variable', 'varying', 'fluctuating',
'shifting', 'inconsistent', 'unpredictable', 'inconstant',
@@ -177,7 +177,7 @@ synonyms
Out[10]:
```
```py
{'immutable': ['fixed',
'set',
'rigid',
@@ -203,7 +203,7 @@ Out[10]:
In [11]:
```
```py
# 定义四个字典
e1 = {'mag': 0.05, 'width': 20}
e2 = {'mag': 0.04, 'width': 25}
@@ -217,7 +217,7 @@ events
Out[11]:
```
```py
{500: {'mag': 0.05, 'width': 20},
760: {'mag': 0.04, 'width': 25},
3001: {'mag': 0.05, 'width': 80},
@@ -228,7 +228,7 @@ Out[11]:
In [13]:
```
```py
people = [
{'first': 'Sam', 'last': 'Malone', 'name': 35},
{'first': 'Woody', 'last': 'Boyd', 'name': 21},
@@ -241,7 +241,7 @@ people
Out[13]:
```
```py
[{'first': 'Sam', 'last': 'Malone', 'name': 35},
{'first': 'Woody', 'last': 'Boyd', 'name': 21},
{'first': 'Norm', 'last': 'Peterson', 'name': 34},
@@ -254,7 +254,7 @@ Out[13]:
In [14]:
```
```py
inventory = dict(
[('foozelator', 123),
('frombicator', 18),
@@ -267,7 +267,7 @@ inventory
Out[14]:
```
```py
{'foozelator': 123, 'frombicator': 18, 'snitzelhogen': 23, 'spatzleblock': 34}
```
@@ -275,7 +275,7 @@ Out[14]:
In [15]:
```
```py
inventory['frombicator'] += 1
inventory
@@ -283,7 +283,7 @@ inventory
Out[15]:
```
```py
{'foozelator': 123, 'frombicator': 19, 'snitzelhogen': 23, 'spatzleblock': 34}
```
@@ -293,7 +293,7 @@ Out[15]:
In [16]:
```
```py
data = {}
data[1.1 + 2.2] = 6.6
# 会报错
@@ -301,7 +301,7 @@ data[3.3]
```
```
```py
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-16-a48e87d01daa> in <module>()
@@ -316,14 +316,14 @@ KeyError: 3.3
In [17]:
```
```py
data
```
Out[17]:
```
```py
{3.3000000000000003: 6.6}
```
@@ -331,7 +331,7 @@ Out[17]:
In [19]:
```
```py
connections = {}
connections[('New York', 'Seattle')] = 100
connections[('Austin', 'New York')] = 200
@@ -343,13 +343,13 @@ connections[('New York', 'Austin')] = 400
In [20]:
```
```py
print connections[('Austin', 'New York')]
print connections[('New York', 'Austin')]
```
```
```py
200
400
@@ -361,7 +361,7 @@ print connections[('New York', 'Austin')]
之前已经见过用索引可以找到一个键对应的值但是当字典中没有这个键的时候Python会报错这时候可以使用字典的 `get` 方法来处理这种情况,其用法如下:
```
```py
`d.get(key, default = None)`
```
@@ -369,7 +369,7 @@ print connections[('New York', 'Austin')]
In [21]:
```
```py
a = {}
a["one"] = "this is number 1"
a["two"] = "this is number 2"
@@ -380,12 +380,12 @@ a["two"] = "this is number 2"
In [22]:
```
```py
a["three"]
```
```
```py
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-8a5f2913f00e> in <module>()
@@ -398,12 +398,12 @@ KeyError: 'three'
In [24]:
```
```py
print a.get("three")
```
```
```py
None
```
@@ -412,14 +412,14 @@ None
In [25]:
```
```py
a.get("three", "undefined")
```
Out[25]:
```
```py
'undefined'
```
@@ -427,7 +427,7 @@ Out[25]:
`pop` 方法可以用来弹出字典中某个键对应的值,同时也可以指定默认参数:
```
```py
`d.pop(key, default = None)`
```
@@ -435,14 +435,14 @@ Out[25]:
In [26]:
```
```py
a
```
Out[26]:
```
```py
{'one': 'this is number 1', 'two': 'this is number 2'}
```
@@ -450,27 +450,27 @@ Out[26]:
In [27]:
```
```py
a.pop("two")
```
Out[27]:
```
```py
'this is number 2'
```
In [28]:
```
```py
a
```
Out[28]:
```
```py
{'one': 'this is number 1'}
```
@@ -478,14 +478,14 @@ Out[28]:
In [29]:
```
```py
a.pop("two", 'not exist')
```
Out[29]:
```
```py
'not exist'
```
@@ -493,7 +493,7 @@ Out[29]:
In [30]:
```
```py
del a["one"]
a
@@ -501,7 +501,7 @@ a
Out[30]:
```
```py
{}
```
@@ -509,7 +509,7 @@ Out[30]:
之前已经知道,可以通过索引来插入、修改单个键值对,但是如果想对多个键值对进行操作,这种方法就显得比较麻烦,好在有 `update` 方法:
```
```py
`d.update(newd)`
```
@@ -517,7 +517,7 @@ Out[30]:
In [31]:
```
```py
person = {}
person['first'] = "Jmes"
person['last'] = "Maxwell"
@@ -526,7 +526,7 @@ print person
```
```
```py
{'born': 1831, 'last': 'Maxwell', 'first': 'Jmes'}
```
@@ -535,14 +535,14 @@ print person
In [32]:
```
```py
person_modifications = {'first': 'James', 'middle': 'Clerk'}
person.update(person_modifications)
print person
```
```
```py
{'middle': 'Clerk', 'born': 1831, 'last': 'Maxwell', 'first': 'James'}
```
@@ -551,7 +551,7 @@ print person
In [33]:
```
```py
barn = {'cows': 1, 'dogs': 5, 'cats': 3}
```
@@ -560,45 +560,45 @@ barn = {'cows': 1, 'dogs': 5, 'cats': 3}
In [35]:
```
```py
'chickens' in barn
```
Out[35]:
```
```py
False
```
In [34]:
```
```py
'cows' in barn
```
Out[34]:
```
```py
True
```
### `keys` 方法,`values` 方法和`items` 方法
```
```py
`d.keys()`
```
返回一个由所有键组成的列表;
```
```py
`d.values()`
```
返回一个由所有值组成的列表;
```
```py
`d.items()`
```
@@ -606,39 +606,39 @@ True
In [36]:
```
```py
barn.keys()
```
Out[36]:
```
```py
['cows', 'cats', 'dogs']
```
In [37]:
```
```py
barn.values()
```
Out[37]:
```
```py
[1, 3, 5]
```
In [38]:
```
```py
barn.items()
```
Out[38]:
```
```py
[('cows', 1), ('cats', 3), ('dogs', 5)]
```