繁体   English   中英

提高dict初始化的效率

[英]Improving efficiency of dict initialization

我在以下代码中将 python dict所有值初始化为0

ecoPos = ["AAC refrig","advanced insulation","advanced-insulation","fiber insulation","high insulation thickness","high R-value","high R value","alternative energy","hydro-electric","hydroelectric","hydro-powered","hydro powered","hydropowered","solar powered","solar-powered","wind powered","wind-powered","applicator-free","applicator free","bamboo","beyond-meat","beyond meat","plant-based","plant based","vegan","vegetarian","bike","e-bike","ebike","small scooter","biochar","compost","Biodegradable","bioplastic","Bisphenol A-free","Bisphenol A free","BPA-free","BPA free","chicken","duck","cultured meat","eggs","geese","goose","pork","quail","turkey","climate-friendly","climate friendly","low GWP","low global warming potential","closed-loop process","closed loop process","compact fluorescent lamp","CFL","cork","corrugated cardboard","corrugated-cardboard","cotton","cruelty-free","cruelty free","PETA approved","PETA-approved","drip sprinkler","Dual Fuel","Dual-Fuel","fuel cell","hydrogen fuel","electric dryer","electric mower","energy efficient","energy saving","energy-saving","high efficiency","highly efficient","less energy","low-impact","low impact","Ethylene vinyl acetate","EVA","flax","linen","Forest Stewardship Council","FSC","programme for the Endorsement of Forest Certification","PEFC","sustainable forestry initiative","SFI","heat pump dryer","hemp","HFO-emitting","HFO emitting","HFO-blowing","HFO blowing","hydrofluoroether","HFE","hydrofluoropolyether","HFPE","incandescent ","jute","kenaf","sunhemp","lead-acid","lead acid","liquefied natural gas","LNG","Liquefied petroleum gas","LPG","Microwave","mulching flower bed","natural fiber","natural fibre","organic","ozone-free","ozone free","Polyethylene terephthalate","PET","polyethylene","PP","Rechargeable","reclaimed","Recyclable","Repurposed","Re-purposed","reusable","reused","up-cycled","Upcycled","removes pollutants","renewable","seep hose","silicone","silk","sock","tshirt","t-shirt","soy","teak","thermoplastic starch","TPS","tree-free","tree free","wood","wool"]
ecoNeg = ["ammonia","BAU","applicator ","bathtub","shower","sink","washing machine","washing-machine","beef","lamb","mutton","big scooter","moped","motorcycle","biomass","Bisphenol A","BPA","xenobiotic endocrine disrupting chemical","XEDC","butter","cheese","fresh cream","ice cream","meat","milk","sour cream","yogurt","cadmium","copper","lead","zinc","carbon dioxide","C02","charcoal grill","chloride","chlorine","Chlorofluorocarbon","CFC","Hydrochloroflourocarbon","HCFC","Hydrofluorocarbon","HFC","perfluorocarbon","PFC","clothes-dryer","clothes dryer","formal trouser","formal wear","regenerated cellulose","skirt","suit","coal","gas","oil","condom","detergent","Dichlorodiphenyltrichloroethane","DDT","dishwasher","refrigerator","television","TV","Dry cleaned","dry-cleaned","fossil fuel-based","fossil fuel based","fungicide","insecticide","pesticide","herbicide","glass fiber","glass fibre","HFC","Hydrofluorocarbon","methane","CH4","nitrous oxide","N20","Perfluorocarbon","PFC","sulfur hexafluoride","SF6","high energy cost","inefficient","low-efficiency","low efficiency","high global warming potential","high GWP","high-global warming potential","high-GWP","hose","inorganic fertilizer","lawn mower","low insulation thickness","low-R Value","low R value","maxi pad","maxi-pad","sanitary pad","sanitary-pad","melamine coffee cup","non-compostable","noncompostable","non-recyc","nonrecyc","non-reusable","nonreusable","nonrenewable","non-renewable","oven","ozone emitting","ozone-emitting","ozone generator","ozone machine","ozone washer","paper","pencil","notebook","tissue","Petroleum","phosphate fertilizer","phosphorus fertilizer","plasma generator","UV bulb","plastic","pollute","pollution","polyester","polyhydroxyalkanoate","PHA","styrofoam","sugar beet","sugarbeet","sugar cane","sugarcane","tampon","toxic","vinyl","volatile organic compound","VOC"]
for i in ecoPos:
    ecoPosCnt[i] = 0
for i in ecoNeg:
    ecoNegCnt[i] = 0

我怎样才能更有效地做到这一点?

如果您只想从默认值0的列表中初始化字典:

ecoPosCnt = dict.fromkeys(ecoPos, 0)
ecoNegCnt = dict.fromkeys(ecoNeg, 0)

print(ecoPosCnt)
print(ecoNegCnt)

使用defaultdict ,这只会在您第一次真正想要使用它时将值设置为0

from collections import defaultdict

ecoPosCnt = defaultdict(int)
ecoNegCnt = defaultdict(int)

或者,由于我猜您正在计算元素的出现次数,请使用Counter ,它将为您完成所有工作:

from collections import Counter

ecoPosCnt = Counter(ecoPos)
ecoNegCnt = Counter(ecoNeg)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM