[英]Combining two lists into dictionary
I'm having issues trying to combine two lists that I have, Username and Account into a dictionary(username being the key and account being the value).我在尝试将我拥有的两个列表(用户名和帐户)合并到字典中时遇到问题(用户名是键,帐户是值)。 My issue is I want any value with the same key to be added together(ie the Brandon would only show up once with a value of 115.5).
我的问题是我希望将任何具有相同键的值加在一起(即布兰登只会出现一次,值为 115.5)。 I would also like to skip the key/value pair for any blank or non-number values.
我还想跳过任何空白或非数字值的键/值对。 Any help would be appreciated.
任何帮助,将不胜感激。
username = ['Brandon', 'Patrick', 'Brandon', 'Jack', '', 'Sarah', 'Jack', 'Brandon', 'James', 'James', 'Sarah', '', 'Brandon']
account = ['5', '18.9', 'xyz', '', '', '825', '45', '10', '3.25', '125.62', '2.43', '', '100.5']
You can zip together corresponding elements.您可以将 zip 放在一起对应的元素。 Skip invalid values by catching errors on conversion to float, and use the falsiness of empty strings to skip empty keys
通过捕获转换为浮点数时的错误来跳过无效值,并利用空字符串的虚假性来跳过空键
names = {}
for k, v in zip(username, account):
# check for empty keys
if not k:
continue
# an error on float conversion is due to a non
# numeric string (float or int)
try:
v = float(v)
except:
continue
# if the key is not in names, then .get will return
# 0, otherwise the last value set
names[k] = names.get(k, 0) + v
names
{'Brandon': 115.5, 'Patrick': 18.9, 'Sarah': 827.43, 'Jack': 45.0, 'James': 128.87}
How about this?这个怎么样?
The more advanced things you may want to look up are: defaultdict and zip您可能想要查找的更高级的内容是:defaultdict 和 zip
from collections import defaultdict
username = ['Brandon', 'Patrick', 'Brandon', 'Jack', '', 'Sarah', 'Jack', 'Brandon', 'James', 'James', 'Sarah', '', 'Brandon']
account = ['5', '18.9', 'xyz', '', '', '825', '45', '10', '3.25', '125.62', '2.43', '', '100.5']
result = defaultdict(list)
for u, a in zip(username, account):
if a != '':
result[u].append(a)
You can use zip
.您可以使用
zip
。 all
with check the conditions if not ''
and value is not numeric
.如果
not ''
并且value is not numeric
,则all
检查条件。
from collections import defaultdict
d = defaultdict(float)
for i in zip(username, account):
if all([*i, i[1].replace('.','').isdigit()]):
d[i[0]] += float(i[1])
{'Brandon': 115.5,
'Patrick': 18.9,
'Sarah': 827.43,
'Jack': 45.0,
'James': 128.87}
from collections import defaultdict def isfloat(value): try: float(value) return True except ValueError: return False username = ['Brandon', 'Patrick', 'Brandon', 'Jack', '', 'Sarah', 'Jack', 'Brandon', 'James', 'James', 'Sarah', '', 'Brandon'] account = ['5', '18.9', 'xyz', '', '', '825', '45', '10', '3.25', '125.62', '2.43', '', '100.5'] result = {} for u, a in zip(username, account): if isfloat(a): if u not in result.keys(): result[u] = float(a) else: result[u] += float(a)
This will help, try part will automatically skip any non-digit values这会有所帮助,尝试部分会自动跳过任何非数字值
username = ['Brandon', 'Patrick', 'Brandon', 'Jack', '', 'Sarah', 'Jack', 'Brandon', 'James', 'James', 'Sarah', '', 'Brandon']
account = ['5', '18.9', 'xyz', '', '', '825', '45', '10', '3.25', '125.62', '2.43', '', '100.5']
new_dict = {}
for key, value in zip(username, account):
try:
new_dict[key]=new_dict.get(key, 0.0) + float(value)
except:
pass
print(new_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.