[code]
d = {
'a' : [1, 2, 3],
'b' : [4, 5]
}
e = {
'a' : {1, 2, 3},
'b' : {4, 5}
}
如何向上面一样一个键对应多个值呢?
# -*- coding: utf-8 -*- """ @author:songhao @file: c5.py @time: 2017/12/12 """ from <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/collections" title="View all posts in collections" target="_blank">collections</a></span> import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/defaultdict" title="View all posts in defaultdict" target="_blank">defaultdict</a></span> d = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/defaultdict" title="View all posts in defaultdict" target="_blank">defaultdict</a></span>(list) d['a'].append(1) d['a'].append(2) d['c'].append(2) print(d) print("*" * 100) d = defaultdict(set) d['a'].add(1) d['a'].add(2) d['b'].add(4) print(d) print("*" * 100) dd = dict() dd['a'] = [1, 2, 3] dd['c'] = [1, 2, 3] print(dd) # output /usr/local/bin/python3 "/Users/songhao/Desktop/Python3 /Python file/d12/c5.py" defaultdict(<class 'list'>, {'a': [1, 2], 'c': [2]}) **************************************************************************************************** defaultdict(<class 'set'>, {'a': {1, 2}, 'b': {4}}) **************************************************************************************************** {'a': [1, 2, 3], 'c': [1, 2, 3]}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# -*- coding: utf-8 -*-
"""
@author:songhao
@file: c5.py
@time: 2017/12/12
"""
from
collections
import
defaultdict
d
=
defaultdict
(
list
)
d
[
'a'
]
.
append
(
1
)
d
[
'a'
]
.
append
(
2
)
d
[
'c'
]
.
append
(
2
)
print
(
d
)
print
(
"*"
*
100
)
d
=
defaultdict
(
set
)
d
[
'a'
]
.
add
(
1
)
d
[
'a'
]
.
add
(
2
)
d
[
'b'
]
.
add
(
4
)
print
(
d
)
print
(
"*"
*
100
)
dd
=
dict
(
)
dd
[
'a'
]
=
[
1
,
2
,
3
]
dd
[
'c'
]
=
[
1
,
2
,
3
]
print
(
dd
)
# output
/
usr
/
local
/
bin
/
python3
"/Users/songhao/Desktop/Python3 /Python file/d12/c5.py"
defaultdict
(
<
class
'list'
>
,
{
'a'
:
[
1
,
2
]
,
'c'
:
[
2
]
}
)
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
defaultdict
(
<
class
'set'
>
,
{
'a'
:
{
1
,
2
}
,
'b'
:
{
4
}
}
)
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
{
'a'
:
[
1
,
2
,
3
]
,
'c'
:
[
1
,
2
,
3
]
}
|
需要注意的是, defaultdict 会自动为将要访问的键(就算目前字典中并不存在这样的键)创建映射实体。 如果你并不需要这样的特性,你可以在一个普通的字典上使用 setdefault() 方法来代替。比如:
d = {} # A regular dictionary d.setdefault('a', []).append(1) d.setdefault('a', []).append(2) d.setdefault('b', []).append(4) print(d) # {'a': [1, 2], 'b': [4]} # 应用 d = {} for key, value in pairs: if key not in d: d[key] = [] d[key].append(value) # 这种方式 是不是很棒 不用判断键是不是存在 这个字典中 d = defaultdict(list) for key, value in pairs: d[key].append(value)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
d
=
{
}
# A regular dictionary
d
.
setdefault
(
'a'
,
[
]
)
.
append
(
1
)
d
.
setdefault
(
'a'
,
[
]
)
.
append
(
2
)
d
.
setdefault
(
'b'
,
[
]
)
.
append
(
4
)
print
(
d
)
# {'a': [1, 2], 'b': [4]}
# 应用
d
=
{
}
for
key
,
value
in
pairs
:
if
key
not
in
d
:
d
[
key
]
=
[
]
d
[
key
]
.
append
(
value
)
# 这种方式 是不是很棒 不用判断键是不是存在 这个字典中
d
=
defaultdict
(
list
)
for
key
,
value
in
pairs
:
d
[
key
]
.
append
(
value
)
|
8771

被折叠的 条评论
为什么被折叠?



