gpt4 book ai didi

python - 字典中一个键的多个值

转载 作者:行者123 更新时间:2023-12-01 07:34:29 24 4
gpt4 key购买 nike

我有一个包含 2 列的数据框 get_tables

get_tables= df[['Database Schema', 'Database Table']]
get_tables = get_tables.drop_duplicates(subset=None, keep='first', inplace=False)
print(get_tables)
dict_get_tables= dict(zip(get_tables['Database Schema'], get_tables['Database Table']))
print(dict_get_tables)

get_tables 将输出打印为

Database Schema Database Table
0 abc customer
3 abc cust
4 def Student

dict_get_tables 将输出打印为

{'abc': 'cust', 'def': 'Student'}

我的要求是,当我像 dict_get_tables.get('abc') 这样获取 dict_get_tables 时,我必须同时获得客户和顾客 dict_get_tables.get('def')应该给我Student

最佳答案

您所需要的只是为每个键保留一个列表:`

dict_table= dict() 

for l in list:
if l[0] in dict_table:
# append the new number to the existing array at this slot
dict_table[l[0]].append(l[1])
else:
# create a new array in this slot
dict_table[l[0]] = [l[1]]

您可以使用上面这样的代码。

关于python - 字典中一个键的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com