python列表成dict

时间:2016-05-23 19:15:08

标签: python list

这是我的问题。

我有一个这样的列表(在我已经分配给列表元素的.txt文件中):

sharenames = ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American Airlines Group Inc.', 'ROST:Ross Stores Inc.', 'VRTX:Vertex Pharmaceuticals Incorporated', 'WDC:Western Digital Corp', 'NCLH:Norwegian Cruise Line Holdings Ltd', 'SWKS:Skyworks Solutions Inc.', 'BBBY:Bed Bath & Beyond Inc.', 'BIDU:Baidu Inc (ADR)', 'ENDP:Endo International plc', '"FRA:DBK":Deutsche Bank AG', '"FRA:FME":Fresenius Medical Care AG & Co. KGaA', '"FRA:DAI":Daimler AG']

我想创建一个我可以访问的字典,如

wertA = sharenames["FRA:FME"]  # as an example

wertA = sharenames[ROST]  # as an example

经过几个小时的尝试和错误,我希望你能帮助我。

4 个答案:

答案 0 :(得分:0)

您可以从元组列表中创建映射:

data =  ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American Airlines Group Inc.', 'ROST:Ross Stores Inc.', 'VRTX:Vertex Pharmaceuticals Incorporated', 'WDC:Western Digital Corp', 'NCLH:Norwegian Cruise Line Holdings Ltd', 'SWKS:Skyworks Solutions Inc.', 'BBBY:Bed Bath & Beyond Inc.', 'BIDU:Baidu Inc (ADR)', 'ENDP:Endo International plc', '"FRA:DBK":Deutsche Bank AG', '"FRA:FME":Fresenius Medical Care AG & Co. KGaA', '"FRA:DAI":Daimler AG']
mapping = dict(entry.rsplit(':', 1) for entry in data)

注意我正在使用string.rsplit到最大的单个分割(第二个参数)。此外,这假设您的值中没有冒号。

映射中的项目:

ENDP -> Endo International plc
WDC -> Western Digital Corp
VIAB -> Viacom Inc.
AAL -> American Airlines Group Inc.
DLTR -> Dollar Tree Inc.
BBBY -> Bed Bath & Beyond Inc.
"FRA:DAI" -> Daimler AG
VRTX -> Vertex Pharmaceuticals Incorporated
"FRA:FME" -> Fresenius Medical Care AG & Co. KGaA
SWKS -> Skyworks Solutions Inc.
NCLH -> Norwegian Cruise Line Holdings Ltd
ROST -> Ross Stores Inc.
"FRA:DBK" -> Deutsche Bank AG
BIDU -> Baidu Inc (ADR)

要从双引号中清除键,您可以执行以下操作:

clean_mapping = {k.replace('"',''): v for k, v in mapping}

答案 1 :(得分:0)

您需要拆分“:”处的每个项目。以下代码生成所需的字典:

sharenames = {}
for item in lst:
    sharenames[":".join(item.split(":")[:-1])] = item.split(":")[-1]

示例:sharenames["ENDP"] >>> 'Endo International plc'

答案 2 :(得分:0)

这可能会做你想要的:

sharenames = ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American Airlines Group Inc.', 'ROST:Ross Stores Inc.', 'VRTX:Vertex Pharmaceuticals Incorporated', 'WDC:Western Digital Corp', 'NCLH:Norwegian Cruise Line Holdings Ltd', 'SWKS:Skyworks Solutions Inc.', 'BBBY:Bed Bath & Beyond Inc.', 'BIDU:Baidu Inc (ADR)', 'ENDP:Endo International plc', '"FRA:DBK":Deutsche Bank AG', '"FRA:FME":Fresenius Medical Care AG & Co. KGaA', '"FRA:DAI":Daimler AG']

# Conversions;
sharenames = (sharename.rsplit(':',1) for sharename in sharenames)
sharenames = ((sharename[0].strip('"'),sharename[1]) for sharename in sharenames)
sharenames = dict(sharenames)

print sharenames['ROST']
print sharenames["FRA:FME"]

答案 3 :(得分:0)

d = {}
for e in sharenames:
    t = tuple(string.rsplit(e,':', 1))
    d[re.sub('\"','',t[0])] = t[1]

for k,v in d.iteritems():
    print k,v

ENDP Endo International plc

WDC Western Digital Corp

VIAB Viacom Inc.

FRA:DBK Deutsche Bank AG

AAL美国航空集团公司

DLTR Dollar Tree Inc.

BBBY Bed Bath&超越公司。

VRTX Vertex Pharmaceuticals Incorporated

SWKS Skyworks Solutions Inc.

NCLH Norwegian Cruise Line Holdings Ltd

ROST Ross Stores Inc。

FRA:DAI Daimler AG

FRA:FME Fresenius Medical Care AG& Co. KGaA

BIDU百度公司(ADR)