0

我编辑了它。我把它们列在字典里。如果 a 和 b 是 2 个字典:

a = {'UK':'http://www.uk.com', 'COM':['http://www.uk.com','http://www.michaeljackson.com']}



bb = {'Australia': 'http://www.australia.com', 'COM':['http://www.Australia.com', 'http://www.rafaelnadal.com','http://www.rogerfederer.com']}

我想联合他们

{'Australia': ['http://www.australia.com'], 'COM': ['http://www.uk.com', 'http://www.michaeljackson.com', 'http://www.Australia.com', 'http://www.rafaelnadal.com', 'http://www.rogerfederer.com'], 'UK': ['http://www.uk.com']}

我想联合他们,即

**How to do it in Python without overwwriting and replacing any value?**
4

4 回答 4

2

我认为也许 adefaultdict在这里可能会有所帮助:

from collections import defaultdict

a = [['UK', ['http://www.uk.com']], ['COM', ['http://www.uk.com'],['http://www.michaeljackson.com']]]

b = [['Australia', ['http://www.australia.com']], ['COM', ['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']]]

d = defaultdict(list)
d.update((v[0],v[1:]) for v in a)
for v in b:
    country_or_com = v[0]
    urls = v[1:]
    d[country_or_com].extend(urls) 

这不完全是您要求的数据结构,但它非常接近(我认为更可取)。

如果您真的想要它的格式(尽管顺序不同):

c = []
for k,v in d.items():
   out = [k]
   out.extend(v)
   c.append(out)

结果是:

[['Australia', ['http://www.australia.com']],
 ['COM', ['http://www.uk.com'], ['http://www.michaeljackson.com'],['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']],
 ['UK', ['http://www.uk.com']]]

如预期的。

于 2012-09-12T13:39:21.540 回答
1

如果您的列表是字典,您只需要合并字典:

>>> a = {'UK':'http://www.uk.com', 'COM':['http://www.uk.com','http://www.michaeljackson.com']}
>>> bb = {'Australia': 'http://www.australia.com', 'COM':['http://www.Australia.com', 'http://www.rafaelnadal.com','http://www.rogerfederer.com']}
>>> dict(a.items()+bb.items())
{'Australia': 'http://www.australia.com', 'COM': ['http://www.Australia.com', 'http://www.rafaelnadal.com', 'http://www.rogerfederer.com'], 'UK': 'http://www.uk.com'}

更新

到目前为止,我的回答是这样的:

>>> sk = list(set(bb.keys()+a.keys()))
>>> sk
['Australia', 'COM', 'UK']
>>> nd
{}

>>> for i in sk: 
...     if i in a.keys():
...        nd[i]=a[i]
... 
>>> nd
{'COM': ['http://www.uk.com', 'http://www.michaeljackson.com'], 'UK': 'http://www.uk.com'}
>>> for i in sk: 
...     if i in bb.keys():
...        nd[i]=bb[i]
... 
>>> nd
{'Australia': 'http://www.australia.com', 'COM': ['http://www.Australia.com', 'http://www.rafaelnadal.com', 'http://www.rogerfederer.com'], 'UK': 'http://www.uk.com'}

我怀疑人们仍然应该使用字典,这是一种 CPU 效率不高的*方法:

a = {'UK':'http://www.uk.com', 'COM':['http://www.uk.com','http://www.michaeljackson.com']}
bb = {'Australia': 'http://www.australia.com', 'COM':['http://www.Australia.com', 'http://www.rafaelnadal.com','http://www.rogerfederer.com']}

sk = list(set(bb.keys()+a.keys()))

nd = {}

for i in sk:
    plholder=[]
    if i in a.keys():
        print i
        print isinstance(a[i], str)
        if isinstance(a[i], str):
            plholder.append(a[i])
        else:
            if i in a.keys():
                for v in a[i]: plholder.append(v)
    if i in bb.keys():
        if isinstance(bb[i], str): plholder.append(bb[i])
        else:
            if i in a.keys():
                for v in bb[i]: plholder.append(v)
    nd[i]=plholder
print nd
{'Australia': ['http://www.australia.com'], 'COM': ['http://www.uk.com', 'http://www.michaeljackson.com', 'http://www.Australia.com', 'http://www.rafaelnadal.com', 'http://www.rogerfederer.com'], 'UK': ['http://www.uk.com']}

*非 cpu 效率,因为对于大型数据集追加将非常慢。

于 2012-09-12T13:44:27.490 回答
1

奇怪的数据结构通常适合潜伏在下面的一些奇怪的恶魔库。为了显而易见,我建议将其转换为适当的数据结构,使用这些数据结构,然后将其转换回来。您可以在奇怪的结构等上使用手工编织联合算法避免实现问题。

首先,使数据体面:

a_ = { x[0]: set(e[0] for e in x[1:]) for x in a }
b_ = { x[0]: set(e[0] for e in x[1:]) for x in b }

然后使用它:

c_ = defaultdict(set)
for k, v in a_.iteritems():
    c_[k] |= v
for k, v in b_.iteritems():
    c_[k] |= v

然后将其转换回您的奇怪结构:

return [[k] + [[e] for e in v] for k, v in c_.iteritems()]

这样我认为真正做了什么是清楚的。

假设:我假设(1)排序顺序并不重要,(2)值只出现一次。如果其中一些不正确,请说明。你的问题太简短了,我不得不做一些解释。

对于不熟悉sets独特捷径的人: c_[k] |= v,可以写成:
c_[k] = c_[k].union(v).

于 2012-09-12T14:32:29.890 回答
1

考虑使用不同的数据结构,例如集合字典。

a = [['UK', ['http://www.uk.com']], ['COM', ['http://www.uk.com'],['http://www.michaeljackson.com']]]
b = [['Australia', ['http://www.australia.com']], ['COM', ['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']]]

# convert these to dictionaries with set values
a = {item[0]:set(s[0] for s in item[1:]) for item in a}
b = {item[0]:set(s[0] for s in item[1:]) for item in b}

# define a function to update our dictionary-of-sets data structure
def union_update_setdict(D, *setdicts):
    """Update dictionary D (with `'key':set(value)` items) with items from setdicts.

    If a new key is added to D from setdicts, a shallow copy of the value
    is added to D.
    """
    for setdict in setdicts:
        for k,v in setdict.items():
            try:
                D[k].update(v)
            except KeyError:
                D[k] = v.copy()

union_update_setdict(a, b)



# Now let's test that the code works

expected = [['UK', ['http://www.uk.com']], ['COM', ['http://www.uk.com'], ['http://www.michaeljackson.com'], ['http://www.Australia.com'], ['http://www.rafaelnadal.com'], ['http://www.rogerfederer.com']], ['Australia', ['http://www.australia.com']]]

# put the "expected" results in our new data structure for comparison
expected = {item[0]:set(s[0] for s in item[1:]) for item in expected}

print a
assert expected == a

如果你真的需要继续使用你糟糕的数据结构,你可以在完成后将其转换回来:

terribledatastruct = [[k]+[[item] for item in v] for k,v in a.items()]
print terribledatastruct
于 2012-09-12T14:18:18.907 回答