通过拆分python列表中的每个项目来填充Dict Key和Value。

问题描述 投票:-1回答:1

我试图将这个列表分割成两个列表,然后将其填充到一个键和项的字典中。

我有一个每个行业的报表列表,我想把这个列表分成两个列表。一个是报表,一个是行业。然后,行业将是字典的键,声明是值。

['“Significant shortages of personal protective equipment PPE, chemical reagents, test swabs and other basic medical supplies persist. Extreme sourcing measures are required to procure necessary supplies for basic operations. Distributor allocations continue across the board.” Health Care & Social Assistance', '“Severe impact to operations as a result of COVID-19. Major challenges in obtaining needed supplies for first responders, including N95 masks, gowns, disinfecting products and medical supplies. As a local government, we are experiencing a significant increase in activity due to emergency-response efforts. Starting to experience inappropriate price increases for short-supply items.” Public Administration', '“Significant demand disruption caused by the coronavirus.” Accommodation & Food Services', '“The coronavirus is having an impact, but not as much as we thought it would at this point. All sectors are staying busy. Although there are many customer concerns, we are finding work-arounds and adapting to the ever-changing situation.” Construction', '“COVID-19 shelter-in-place order in effect. Offices closed except for essential personnel.” Educational Services', '“Like most businesses, we cannot fully project how the coronavirus will impact us. By displaying prudence and avoiding panic, we are trying to navigate this crisis. As human capital is our greatest expense, protecting that capital is job one. Supply chains are overstressed and will normalize only when the panic subsides.” Information', '“COVID-19 has greatly impacted daily operations. All staff personnel are telecommuting, and customer concerns have shifted from normal activities to preventative measures.” Management of Companies & Support Services', '“We are experiencing no real issues from a business perspective, although COVID-19 has forced us to reconsider elements of how our workforce gets things done.” Mining', '“As expected for many industries whether manufacturing or non-manufacturing, purchasing has slowed as we evaluate the economic climate and prepare for long-term effects.” Retail Trade', '“The coronavirus is effecting every aspect of business.” Real Estate, Rental & Leasing']

理想的结果是得到这样的两个列表。

New List 1 = [“Significant shortages of personal protective equipment PPE, chemical reagents, test swabs and other basic medical supplies persist. Extreme sourcing measures are required to procure necessary supplies for basic operations. Distributor allocations continue across the board.”, ....., “The coronavirus is effecting every aspect of business.”]

New List 2 = [Health Care & Social Assistance, ....., Real Estate, Rental & Leasing]

列表2将是字典的关键 而列表1将是存储的值。

dictionary = dict(zip(List2,List1))

谢谢您的帮助。

python list split dictionary-comprehension
1个回答
0
投票

你可以通过搜索来分离报表和行业 索引。如果您的原始清单名为 L:

industries = [s[s.index("”")+2:] for s in L]
statements = [s[1:s.index("”")] for s in L]
dictionary = dict(zip(industries, statements))

但你也可以直接用dict理解来构建你的dict。

dictionary = {e[e.index("”")+2:]: e[1:e.index("”")] for e in L}

0
投票
  1. 不是 New List 1New List 2list1list2. (无空格,无大写字母--见 PEP 8 - 函数和变量名称.)

  2. New List 2 = [Health Care & Social Assistance, ....., Real Estate, Rental & Leasing]
    

    不过

    list2 = ["Health Care & Social Assistance", ....., "Real Estate", "Rental & Leasing"]
    

    (字符串用引号表示)。

  3. 不是 dictionary = dict(zip(List2,List1))dictionary = dict(zip(list2, list1)) 与1.一致)。

© www.soinside.com 2019 - 2024. All rights reserved.