简体   繁体   English

如何从存储在列表中的字典中正确访问字典键?

[英]How to properly access dictionary keys from dictionaries stored in list?

I am a complete beginner reading Crash Course Python by Eric Matthes. 我是Eric Matthes撰写的Crash Course Python的完整入门者。 Here is my code trying to access a dictionary key, with the dictionaries stored within the list shirts . 这里是我的代码试图访问一个辞典键,用字典存储在列表中shirts

red_shirt = {'shirt_color': 'red', 'shirt_price': 20}
blue_shirt = {'shirt_color': 'blue', 'shirt_price': 25}
green_shirt = {'shirt_color': 'green', 'shirt_price': 30}

shirts = [red_shirt, blue_shirt, green_shirt]

user_input = input('Which shirt would you like to purchase?\n:')
for shirt in shirts:

So far I believe I have an issue with the for loop... Should I be using a second for loop to access the keys within the dictionary? 到目前为止,我相信我的for循环有问题...是否应该使用第二个for循环来访问字典中的键?

    if shirt['shirt_color'] == 'red':
        #shirt_color = 'red'
        print('You bought the ' + shirt['shirt_color'] + ' shirt for $' + str(shirt['shirt_price']))

Opposed to using k['v'] to access the values of the dictionary, I have also tried using .format . 与使用k['v']访问字典的值.format ,我也尝试使用.format

    elif shirt['shirt_color'] == 'blue':
        #shirt_color = 'blue'
        print('You bought the ' + shirt['shirt_color'] + ' shirt for $' + str(shirt['shirt_price']))
    elif shirt['shirt_color'] == 'green':
        #shirt_color = 'green'
        print('You bought the ' + shirt['shirt_color'] + ' shirt for $' + str(shirt['shirt_price']))
    else:
        print('It seems we do not have the shirt you are looking for, please try again')

I think you almost got what you wanted, you just forgot that you needed to actually reference the user input you requested. 我认为您几乎可以满足您的要求,而只是忘记了需要实际引用您所请求的用户输入。

red_shirt = {'shirt_color': 'red', 'shirt_price': 20}
blue_shirt = {'shirt_color': 'blue', 'shirt_price': 25}
green_shirt = {'shirt_color': 'green', 'shirt_price': 30}

shirts = [red_shirt, blue_shirt, green_shirt]

user_input = input('Which shirt would you like to purchase?\n:')
for shirt in shirts:
    if shirt['shirt_color'] == user_input:
        print('You bought the ' + shirt['shirt_color'] + ' shirt for $' + str(shirt['shirt_price']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字典中过滤某些键的最快方法,这些字典存储在字典列表中,而字典列表又存储在字典中 - Fastest way to filter for certain keys from dictionaries which are stored in a list of dictionaries which is in turn stored in a dictionary 如何从字典列表中访问嵌套字典 - How to access nested dictionary from a list of dictionaries 带有年份键的列表列表中的词典词典 - Dictionary of dictionaries from list of lists with year keys 从模板字典和键生成字典列表 - generate list of dictionaries from template dictionary and keys 如何从 Python 中的字典列表中访问某些键 - How to access certain keys from a list of dictionaries in Python 如何从列表访问相同键的多个字典值? - How to access multiple dictionaries values of the same keys from a list? 通过字典列表,字典键列创建熊猫数据框 - Creating a pandas Dataframe from a list of Dictionaries, dictionary keys as columns 找出一个字典中的键是否存在于字典列表中 - find out if keys from one dictionary are present in list of dictionaries 如何使用字典列表作为字典列表中的值创建字典 - how to create a dictionary with list of dictionaries as values from a list of dictionaries 如何从字典列表中创建三个单独的值列表,其中每个字典都有三个键 - How to create three separate lists of values from a list of dictionaries where each dictionary has three keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM