Python多图 [英] Python multimap
问题描述
最近我们需要一个C ++中的multimap容器。我现在需要
编写等效的Python代码。 Python如何处理这个?
k [''1''] =''汤''
k [''1''] =''鲍勃''
k [''1''] =''乔''
....
相同的钥匙,但价值观不同。没有覆盖......他们都必须将
插入容器中
谢谢,
布拉德
Recently had a need to us a multimap container in C++. I now need to
write equivalent Python code. How does Python handle this?
k[''1''] = ''Tom''
k[''1''] = ''Bob''
k[''1''] = ''Joe''
....
Same key, but different values. No overwrites either.... They all must
be inserted into the container
Thanks,
Brad
推荐答案
8月27日9:35 * am,brad< byte8b ... @ gmail.comwrote:
On Aug 27, 9:35*am, brad <byte8b...@gmail.comwrote:
最近我们需要一个C ++中的multimap容器。我现在需要
编写等效的Python代码。 Python如何处理这个?
k [''1''] =''汤''
k [''1''] =''鲍勃''
k [''1''] =''乔''
...
相同的钥匙,但是不同的价值观没有覆盖....他们都必须
插入容器
谢谢,
布拉德
Recently had a need to us a multimap container in C++. I now need to
write equivalent Python code. How does Python handle this?
k[''1''] = ''Tom''
k[''1''] = ''Bob''
k[''1''] = ''Joe''
...
Same key, but different values. No overwrites either.... They all must
be inserted into the container
Thanks,
Brad
Python 2.5.2(r252:60911,2008年7月31日,17:28:52)
[GCC 4.2.3(Ubuntu 4.2.3-) linux2上的2ubuntu7)]
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>> k = {}
k [''1' '] = []
k [''1'']。追加(''汤'')
k [''1'']。追加('''Bob'')
k [''1'']。追加(''乔'')
k [''1'']
>>k = {}
k[''1''] = []
k[''1''].append(''Tom'')
k[''1''].append(''Bob'')
k[''1''].append(''Joe'')
k[''1'']
['''汤'',''鲍勃'',''乔'']
[''Tom'', ''Bob'', ''Joe'']
>>>
>>>
brad写道:
brad wrote:
最近有需要给我们一个C ++中的multimap容器。我现在需要
编写等效的Python代码。 Python如何处理这个?
k [''1''] =''汤''
k [''1''] =''鲍勃''
k [''1''] =''乔''
...
相同的钥匙,但是不同的价值观没有覆盖....他们都必须
插入容器
谢谢,
布拉德
Recently had a need to us a multimap container in C++. I now need to
write equivalent Python code. How does Python handle this?
k[''1''] = ''Tom''
k[''1''] = ''Bob''
k[''1''] = ''Joe''
...
Same key, but different values. No overwrites either.... They all must
be inserted into the container
Thanks,
Brad
我不知道这是否完全相同,但是如何使用这样的
defaultdict呢?
I don''t know if this is exactly equivalent, but what about using a
defaultdict like this?
>>来自集合导入defaultdict
k = defaultdict(list)
k [''1 ''] .append('''汤'')
k [''1'']。追加('''鲍勃'')
k [''1'']。追加(''' Joe'')
k [''1'']
>>from collections import defaultdict
k = defaultdict(list)
k[''1''].append(''Tom'')
k[''1''].append(''Bob'')
k[''1''].append(''Joe'')
k[''1'']
[''Tom'',''Bob '',''乔'']
-
[''Tom'', ''Bob'', ''Joe'']
--
brad写道:
brad wrote:
最近我们需要一个C ++中的multimap容器。我现在需要
编写等效的Python代码。 Python如何处理这个?
k [''1''] =''汤''
k [''1''] =''鲍勃''
k [''1''] =''乔''
....
相同的钥匙,但价值观不同。没有覆盖......他们都必须将
插入容器中
Recently had a need to us a multimap container in C++. I now need to
write equivalent Python code. How does Python handle this?
k[''1''] = ''Tom''
k[''1''] = ''Bob''
k[''1''] = ''Joe''
....
Same key, but different values. No overwrites either.... They all must
be inserted into the container
对内置字典进行子类化?
class d(dict):
def __setitem __(self,item,value):
如果不是self中的项目:super(d,self) .__ setitem __(item,[])
self [item] .append(value)
Subclassing the builtin dict?
class d(dict):
def __setitem__(self, item, value):
if not item in self: super(d, self).__setitem__(item, [])
self[item].append(value)
>> D = d()
D [1] =" Hello"
D [1] =" World!"
D [ 1]
>>D = d()
D[1] = "Hello"
D[1] = "World!"
D[1]
[''Hello'',''World!'']
[''Hello'', ''World!'']
谢谢,
布拉德
Thanks,
Brad
Michele
Michele
这篇关于Python多图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!