[英]How can i store values in my dictionary
袋子包含:
['Item','item','item']
硬幣包含:
['Item','Item','Item']
coin_to_bag = {}
print('Here are your bags:\n')
print(bags)
print('\n')
print('Here are your coin types:\n')
print (coins)
我的嵌套循環
bags = ['small', 'medium','large']
coins = ['quarter', 'dime', 'nickel', 'penny']
'''
what Im trying to do:
foreach bag in bags:
print: enter in a coin index. e.g. 1 for quarter out of ['quarter', 'dime', 'nickel', 'penny']
prompt:: how many of this type? 15 (thus will store '15' representing, there are 15 quarters, storing '15')
for index_position in coins:
assign index, type = bag create a dictionary like ["quarter"] = {"1", "15"}}
print(coin_to_bag)
'''
for bag in bags:
coin_to_bag = coin_to_bag[bag] # set quarter as a key in coin_to_bag = {["quarter"]}
coin_type = input('Number of this type? e.g. type 1 for Quarter "["Quarter", "Nickel"]" ') #e.g. 0.25 * 2
coin_amount = input('Number of this type? e.g. 15') #e.g. 0.25 * 2
for coin in coins:
#set values for key: quarter like {["quarter"] = ["1", "15"]}
coin_to_bag[bag] = coin_to_bag[coin_type], coin_to_bag[coin_amount]
print(coin_to_bag)
我似乎無法弄清楚如何使用dictionary
和lists(coin,bags)
最終,我試圖將coin_to_bag
存儲:
coin_to_bag = {"small": {"1", "15"}, "medium": {"2", "5"} }
值通過以下方式存儲到字典中:
some_dict = {}
some_dict[key] = value
或一行為: some_dict = {key: value}
。
因此,大概您想要:
coin_to_bag[bag] = [coin_type, coin_amount]
例如,這可能意味着
coin_to_bag['small'] = ["1", "15"]
這樣做, coin_to_bag[bag]
意味着使用bag
給出的密鑰訪問coin_to_bag
字典的元素。 但是該鍵值在您進行設置之前不會存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.