簡體   English   中英

通過拆分 python 列表中的每個項目填充的字典鍵和值

[英]Dict Key and Value populated from splitting each item in a python list

我正在嘗試將此列表拆分為兩個列表,然后將其填充到鍵和項的字典中。

我有每個行業的聲明列表,並希望將此列表分成兩個列表。 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))

謝謝您的幫助。

您可以通過搜索索引”來區分報表和行業。 如果您的原始列表名為L

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

但是你也可以直接用字典理解來構造你的字典:

dictionary = {e[e.index("”")+2:]: e[1:e.index("”")] for e in L}
  1. 不是New List 1New List 2 ,而是list1list2
    (沒有空格,沒有大寫字母 - 請參閱PEP 8 - Function 和變量名稱。)

  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 一致)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM