Python3 字典一个键对应多个值

Python字典多值管理
部署运行你感兴趣的模型镜像

[code]
d = {
'a' : [1, 2, 3],
'b' : [4, 5]
}
e = {
'a' : {1, 2, 3},
'b' : {4, 5}
}

如何向上面一样一个键对应多个值呢?

Python
# -*- 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() 方法来代替。比如:

Python
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 )



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值