1

我有以下两个列表:

ISO3166_CountryCodes_NO = [["NO","Norge"],["SE","Sverige"],["GR","Hellas"]]
ISO3166_CountryCodes_EN = [["NO","Norway"],["SE","Sweden"],["GR","Greece"]]

如您所见,国家代码始终相同,但国家名称不同(不同的翻译)如何创建这样的列表:

ISO3166_CountryCodes = [["NO","Norge","Norway"],["SE","Sverige","Sweden"],["GR","Hellas","Greece"]]

我可以在第一个列表中使用 for 循环来做到这一点,对于每个元素,我可以搜索第二个元素以找到常见的国家代码。然后将翻译附加到一个新列表中,但我觉得这种方式有点笨拙。

有没有更好的方法在 Python 中实现这一点?例如,在我更熟悉的 Perl 中,我会使用哈希表。

4

4 回答 4

4

在 python 中,字典是一个哈希表。首先,创建两个字典:

NO_dict = {x[0]: x[1] for x in ISO3166_CountryCodes_NO}
EN_dict = {x[0]: x[1] for x in ISO3166_CountryCodes_EN}

这给了你:

{'GR': 'Hellas', 'NO': 'Norge', 'SE': 'Sverige'}
{'GR': 'Greece', 'NO': 'Norway', 'SE': 'Sweden'}

然后,您可以像这样创建一个列表:

final_list = [[k, NO_dict[k], EN_dict[k]] for k in NO_dict]

给你:

[['GR', 'Hellas', 'Greece'],
 ['SE', 'Sverige', 'Sweden'],
 ['NO', 'Norge', 'Norway']]

稍后您可能会发现将数据保存在字典中更容易,名称存储在元组中,例如:

final_dict = {k:(NO_dict[k], EN_dict[k]) for k in NO_dict}

这样您就可以使用缩写作为键来获取项目,例如final_dict['NO']会产生('Norge', 'Norway')

编辑:OrderedDict

如果您的 python >= 2.7,并且您担心顺序,您仍然可以通过 using 使用字典OrderedDict,例如:

from collections import OrderedDict

# A list of lists can be used as input for an OrderedDict, so don't need to loop
NO_dict = OrderedDict(ISO3166_CountryCodes_NO)
EN_dict = OrderedDict(ISO3166_CountryCodes_EN)

# Assumes you want the result in the same order as the Norwegian list
# Iterate over the English list if it has a preferred order

final_dict = OrderedDict([(k, (NO_dict[k], EN_dict[k])) for k in NO_dict])

(有关另一种实现,请参见 AshwiniChaudhary 的回答)

于 2012-11-17T21:58:28.880 回答
2

像这样的东西,使用unique_everseen来自itertools 食谱chain()

In [26]: from itertools import *

In [27]: lis1=[["NO","Norge"],["SE","Sverige"],["GR","Hellas"]]

In [28]: lis2=[["NO","Norway"],["SE","Sweden"],["GR","Greece"]]

In [29]: from itertools import *

In [30]: def unique_everseen(iterable, key=None):
        seen = set()
        seen_add = seen.add
        if key is None:
                for element in ifilterfalse(seen.__contains__, iterable):
                        seen_add(element)
                        yield element
                else:
                        for element in iterable:
                                k = key(element)
                                if k not in seen:
                                        seen_add(k)
                                        yield element
   ....:                         

In [31]: [list(unique_everseen(chain(*x))) for x in izip(lis1,lis2)]
Out[31]: 
[['NO', 'Norge', 'Norway'],
 ['SE', 'Sverige', 'Sweden'],
 ['GR', 'Hellas', 'Greece']]

或:您可以groupby从 itertools 中使用,并结合operator.itemgetter()

In [42]: from operator import *

In [43]: [[k]+list(map(itemgetter(1),g)) for x in zip(lis1,lis2) for k,g in groupby(x,itemgetter(0))]
Out[43]: 
[['NO', 'Norge', 'Norway'],
 ['SE', 'Sverige', 'Sweden'],
 ['GR', 'Hellas', 'Greece']]

或 using collections.OrderedDict,它是一个子类,dict并且也保持顺序:

In [47]: from collections import OrderedDict

In [48]: dic=OrderedDict()

In [49]: for x in lis1:
   ....:     dic.setdefault(x[0],[]).append(x[1])
   ....:     

In [50]: for x in lis2:
    dic.setdefault(x[0],[]).append(x[1])
   ....:     

In [51]: dic
Out[51]: OrderedDict([('NO', ['Norge', 'Norway']), ('SE', ['Sverige', 'Sweden']), ('GR', ['Hellas', 'Greece'])])

In [52]: [[x]+y for x,y in dic.items()]
Out[52]: 
[['NO', 'Norge', 'Norway'],
 ['SE', 'Sverige', 'Sweden'],
 ['GR', 'Hellas', 'Greece']]

#or directly access the names using the short-name
In [53]: dic['NO']
Out[53]: ['Norge', 'Norway']

In [54]: dic['GR']
Out[54]: ['Hellas', 'Greece']
于 2012-11-17T21:50:00.690 回答
1

您可以使用列表理解:

>>> [[s]+
     [n for (c,n) in ISO3166_CountryCodes_NO if c==s]+
     [n for (c,n) in ISO3166_CountryCodes_EN if c==s]
     for s in set([c for (c,n) in ISO3166_CountryCodes_NO] + 
                  [c for (c,n) in ISO3166_CountryCodes_EN])]

[['GR', 'Hellas', 'Greece'], ['SE', 'Sverige', 'Sweden'], ['NO', 'Norge', 'Norway']]
于 2012-11-17T21:53:42.897 回答
1

使用 Python 3.2。

第一种方式:

[[i[0],i[1],v[1]] for i in list1 for v in list2  if i[0]==v[0]]

第二种方式:

res=[]
for i,v in list(zip(list1,list2):
    tem=[i[0]]
    if i[0]==v[0]: tem.extend([i[1],v[1]])
res.append(tem)
于 2012-11-18T15:39:55.387 回答