回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个for循环,它给了我多个包含2个值的列表。这是for循环,数据取自excel文件。
当我想在字典中添加一个键已经存在的值时,问题就出现了</p>
<pre class="lang-py prettyprint-override"><code>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']}
</code></pre>
<p>但我希望它是这样的:</p>
<pre class="lang-py prettyprint-override"><code>{'Designer': ['Tonya Baker' ,'Jess Huang']}
</code></pre>