简体   繁体   English

遍历 Python 中的列表

[英]Looping through a list in Python

Trying to loop through a list of numbers so that the output reads the result on a seperate line.尝试遍历数字列表,以便 output 在单独的行上读取结果。

Instructions Given: - Store numbers 1-9 in a list.给出的说明: - 将数字 1-9 存储在列表中。 - Loop through the list. - 遍历列表。 - Using if-elif-else chain inside the loop to print the appropriate ending for each number. - 在循环内使用 if-elif-else 链来打印每个数字的适当结尾。 The output should read "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th" with each result on a seperate line. output 应显示为“1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th”,每个结果单独一行。 Most ordinal numbers end in "th" except for 1st, 2nd, 3rd.大多数序数都以“th”结尾,除了 1st、2nd、3rd。

My Problem: I am having an issue with the individual loop code.我的问题:我对单个循环代码有疑问。

What is the correct way to write this?写这个的正确方法是什么?

numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

for numbers in numbers:
    if '1' in numbers:
        print(" + number +" "st.")
    elif '2' in numbers:
        print(" + number + " "nd.")
    elif '3' in numbers:
        print(" + number + " "rd.")
    else:
        print(" + number +" "th.")

When you say: 当你说:

if '1' in numbers:

You are checking if that item is in the list, which will be true for every single iteration, so every iteration is going to print '1st'. 您正在检查该项目是否在列表中,这对于每次迭代都是正确的,因此每次迭代都将打印'1st'.

What you need to do is check the individual value, which you've specified as numbers but should change to number for each iteration 您需要做的是检查单个值,该值已指定为数字,但每次迭代都应更改为数字

for number in numbers:
    if number == '1': print('{}st.'.format(number))

Hope that makes sense! 希望有道理!

Also I just noticed, print(" + number +" "st.") I'm assuming you're attempting to concatenate strings here. 另外我只是注意到, print(" + number +" "st.")我假设您正在尝试在此处连接字符串。 I'd suggest using format, as I've shown above. 我建议使用格式,如上所示。 However to concatenate this statement you'd want to say print(number + "st.") 但是,要连接此语句,您需要说出print(number + "st.")

You are close. 你近了 In your for loop, you want the enumerator to be different than the list you are enumerating. 在for循环中,您希望枚举数与要枚举的列表不同。 Then that enumerator contains the object you are comparing, so in the print you don't need the quotes around number 然后,该枚举器包含您要比较的对象,因此在打印中,您不需要在number前后加上引号

numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for number in numbers:
    if '1' in number:
        print( number +"st.")
    elif '2' in number:
        print(number + "nd.")
    elif '3' in number:
        print( number + "rd.")
    else:
        print(number +"th.")

To answer your last question, there is no one correct way to do it. 要回答您的最后一个问题,没有正确的方法。 Different people have different approaches depending on personal preference and level of knowledge of the language. 不同的人有不同的方法,具体取决于个人喜好和语言知识水平。

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]   

for number in numbers:
    if number == 1:
        print(str(number)  + "st.")
    elif number == 2:
        print(str(number)+ "nd.")   
    elif number == 3:
        print(str(number)+ "rd.")
    else:
        print(str(number)+ "th.")

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

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