我有一个带有以下值的数组'commit',并且有单独列表中的国家/地区列表-Self.Countries(美国,中国,德国)。
commit = [['UNITED_STATES-Consumer1_SymPercChg'],
['UNITED_STATES-Housing1_StndDev'],
['UNITED_STATES-Auto1_SymPercChg'],
['CHINA-Finance1_SymPercChg'],
['CHINA-Transport1_StndDev'],
['CHINA-Housing1_SymPercChg'],
['GERMANY-PMI1_StndDev'],
['GERMANY-Manufacturing1_SymPercChg'],
['GERMANY-Survey1_SymPercChg']
]
在基于国家/地区列表拆分数组“ commmit”时,我需要帮助。我尝试了下面的代码,但无法正常工作。它返回一个空列表:
for country in self.countries:
self.select_columns = [x for x in commit if country in x]
答案 0 :(得分:1)
尝试regex match。
import re
pattern = re.compile('(^[A-Z_]+)(-)(.+)')
country_data = {}
for c in commit:
match = pattern.match(c[0])
if match:
if match.group(1) in countries:
if match.group(1) in country_data.keys():
country_data[match.group(1)].append(match.string)
else:
country_data[match.group(1)] = [match.string]
print(country_data)
答案 1 :(得分:1)
假设您有多个国家/地区的国家名称,名称之间用空格隔开(在您的问题中),而不是_
。
您可以执行以下操作:
countries = ['UNITED STATES',
'CHINA',
'GERMANY'
]
commit = [['UNITED_STATES-Consumer1_SymPercChg'],
['UNITED_STATES-Housing1_StndDev'],
['UNITED_STATES-Auto1_SymPercChg'],
['CHINA-Finance1_SymPercChg'],
['CHINA-Transport1_StndDev'],
['CHINA-Housing1_SymPercChg'],
['GERMANY-PMI1_StndDev'],
['GERMANY-Manufacturing1_SymPercChg'],
['GERMANY-Survey1_SymPercChg']
]
for c in countries:
c = "_".join(c.split(" "))
print c, [x for x in commit if c in x[0]]
输出:
UNITED_STATES ['UNITED_STATES-Consumer1_SymPercChg', 'UNITED_STATES-Housing1_StndDev', 'UNITED_STATES-Auto1_SymPercChg']
CHINA ['CHINA-Finance1_SymPercChg', 'CHINA-Transport1_StndDev', 'CHINA-Housing1_SymPercChg']
GERMANY ['GERMANY-PMI1_StndDev', 'GERMANY-Manufacturing1_SymPercChg', 'GERMANY-Survey1_SymPercChg']