简体   繁体   English

字典python中的整数列表

[英]List of integers in dictionary python

i am learning python currently with the Python Crash Course text. 我目前正在使用Python Crash Course文本学习python。 I am at the part about having lists in dictionaries specifically the try it yourself. 我的意思是在字典中列出列表,尤其是自己尝试一下。 The text explains how to access values that are in list form in python when they are strings but not integers. 文字说明了当它们是字符串而不是整数时,如何访问python中列表形式的值。 I have gotten my code to work so far but it prints the favorite numbers list twice in list form. 到目前为止,我的代码已经可以使用,但是它以列表形式两次打印了喜欢的数字列表。 How do i fix this so it prints it only once, and plainly without the brackets? 如何解决此问题,使其仅打印一次,并且没有括号?

here is my code so far. 到目前为止,这是我的代码。

favorite_numbers = {
    'king': ['7','10'],
    'jake': ['29','77'],
    'mr robot': ['234','1337'],
    'elliot': ['1234' ,'89'],
}

for name, number in favorite_numbers.items():
    print(
    "Hey, " 
    + name.title() + 
    " your favorite numbers are: "
    )
    for numbers in number:
        print(number)

Help? 救命? please and thank you. 谢谢,麻烦您了。

I think your naming confused you a little. 我认为您的命名让您有些困惑。 Look at your use of number and numbers . 看看你使用的numbernumbers Fixed version below. 下面的固定版本。 Also check out `print( "foo", end = "" ) to have multiple print statements that don't terminate in a new line, if you want to clean up the appearance of the output a little. 如果您想稍微清理输出的外观,还可以检查`print(“ foo”,end =“”)以拥有不以新行结尾的多个print语句。

favorite_numbers = {
    'king': ['7','10'],
    'jake': ['29','77'],
    'mr robot': ['234','1337'],
    'elliot': ['1234' ,'89'],
}

for name, numbers in favorite_numbers.items():
    print(
    "Hey, " 
    + name.title() + 
    " your favorite numbers are: "
    )
    for number in numbers:
        print(number)

This will fix your current code: 这将修复您的当前代码:

for numbers in number:
    print(numbers)

But it's kind of a backward way to name the variables. 但这是一种命名变量的落后方法。 How about this instead? 怎么样呢?

for name, numbers in favorite_numbers.items():
    print(
    "Hey, " 
    + name.title() + 
    " your favorite numbers are: "
    )
    for number in numbers:
        print(number)

You are confused because you have similarly-named variables. 您很困惑,因为您拥有名称相似的变量。 In your case, this line: 在您的情况下,此行:

for name, number in favorite_numbers.items():

binds number to successive values in the dictionary. number绑定到字典中的连续值。 In this example, they are specifically lists containing strings. 在此示例中,它们特别是包含字符串的列表。

This line: 这行:

for numbers in number:

binds numbers to successive strings in the list. numbers绑定到列表中的后续字符串。

This line: 这行:

print(number)

prints the list , not the string . 打印列表 ,而不是字符串

Try this: 尝试这个:

for name, list_of_numbers in favorite_numbers.items():
    print(
    "Hey, " 
    + name.title() + 
    " your favorite numbers are: "
    )
    for number in list_of_numbers:
        print(number)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM