如果您的列表是字典,您只需要合并字典:
>>> 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 效率,因为对于大型数据集追加将非常慢。