我有一个for循环,它给出了包含2个值的多个列表。这是for循环,数据从excel文件中提取。当我想在字典中添加一个键已经存在的值时,就会出现问题
obj={}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail', '
Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']
for i in test:
arr = i.lstrip(' ').rstrip(' ').split(' - ')
if arr[2] == 'noemail':
arr = arr[:2]
print(arr)
# gives multiple arrays [['Tonya Baker', 'Designer'], ['Jess Huang', 'Designer']]
obj[arr[1]] = arr[0]
print(obj) # {'Designer': ['Jess Huang']}但相反,我想让它像:
{'Designer': ['Tonya Baker' ,'Jess Huang']}发布于 2021-08-04 07:03:31
使用defaultdict。
>>> from collections import defaultdict
>>> test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail']
>>> d = defaultdict(list)
>>> for s in test:
... name, role, *_ = s.split(' - ')
... d[role.strip()].append(name.strip())
...
>>> d
defaultdict(list,
{'Designer': ['Tonya Baker', 'Jess Huang'],
'Bizdev': ['Denise Leung']})发布于 2021-08-04 07:02:35
你可以试试这个:
obj={}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail', 'Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']
for i in test:
arr = i.lstrip(' ').rstrip(' ').split(' - ')
if arr[2] == 'noemail':
arr = arr[:2]
if arr[1] not in obj:
obj[arr[1]] = []
obj[arr[1]].append(arr[0])
print(obj)
"""
{'Designer': ['Tonya Baker', 'Jess Huang'],
'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'],
'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz'],
'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'],
'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'],
'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'],
'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'],
'Director': ['Melissa Grant', 'Scott Halcomb'],
'Manager': ['Victoria McCulloh', 'Shawn Warehouse', 'Shawn Norwood', 'Alex Bronson', 'Noah Solnick'],
'Csuite': ['Natalie Gordon']}
"""我们每次都只是检查我们现在在obj(dict)中获得的密钥是否已经存在,如果不是我们正在创建它,那么我们将追加该列表。
发布于 2021-08-04 07:02:32
试试这个:
obj = {}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail',
'Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']
for i in test:
arr = i.lstrip(' ').rstrip(' ').split(' - ')
if arr[2] == 'noemail':
arr = arr[:2]
print(arr)
if arr[1] not in obj:
obj[arr[1]] = []
obj[arr[1]] += [arr[0]]
print(obj) 输出:
['Tonya Baker', 'Designer']
['Jess Huang', 'Designer']
['Denise Leung', 'Bizdev']
['Kristin Saulsbury', 'Bizdev']
['Molly Murphy', 'Bizdev']
['Angela Wood', 'Support']
['Heather Bond', 'Support']
['Natalie Ruiz', 'Support']
['Ivan Torres', 'Software']
['Max Gordon', 'Software']
['Jon Skulski', 'Software']
['Peggy Lin', 'Engineering']
['Andy Mai', 'Engineering']
['Alex Battaglino', 'Engineering']
['Evan Marks', 'Other']
['August Flanagan', 'Other']
['Helen Chi', 'Other']
['Ian Pearce', 'Other']
['Lisa Lamb', 'Marketing']
['Kate Levy', 'Marketing']
['Queen Tuba', 'Marketing']
['Crystal Baik', 'Marketing']
['Melissa Grant', 'Director']
['Scott Halcomb', 'Director']
['Victoria McCulloh', 'Manager']
['Shawn Warehouse', 'Manager']
['Shawn Norwood', 'Manager']
['Alex Bronson', 'Manager']
['Noah Solnick', 'Manager']
['Natalie Gordon', 'Csuite']
{'Designer': ['Tonya Baker', 'Jess Huang'], 'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'], 'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz'], 'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'], 'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'], 'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'], 'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'], 'Director': ['Melissa Grant', 'Scott Halcomb'], 'Manager': ['Victoria McCulloh', 'Shawn Warehouse', 'Shawn Norwood', 'Alex Bronson', 'Noah Solnick'], 'Csuite': ['Natalie Gordon']}我看到测试值是在不久前增加的,所以我编辑了我的答案。
https://stackoverflow.com/questions/68646561
复制相似问题