简体   繁体   English

如何使用索引遍历列表

[英]How to iterate through a list using index

I am making a function that counts from 0 to a certain number input I tried to make it using print and that worked but the output needs to be a return value so I tried making a list then iterating through it using a for loop and returning each value of the list back using the index of the list but that didn't work it only returns the last value of the list I also want the returned value to be a string, not a list.我正在制作一个 function,它从 0 计数到某个数字输入我尝试使用 print 制作它并且有效但是 output 需要是一个返回值所以我尝试制作一个列表然后使用 for 循环遍历它并返回每个使用列表的索引返回列表的值,但这不起作用它只返回列表的最后一个值我还希望返回的值是一个字符串,而不是一个列表。

this is my code这是我的代码

nums = []
def numbers_range(number):
  for i in range(number+1):
    numz = nums.append(i)
  for g in range(len(nums)):
   numb = nums[g]
  return numb

print(numbers_range(5))

result 5 the result i want 0 1 2 3 4 5 please help结果 5 我想要的结果 0 1 2 3 4 5 请帮忙

Your for loop containing variable 'g' is putting values 1 over next in same variable numb.包含变量“g”的 for 循环将值 1 放在同一个变量 numb 中的 next 之上。 Basically each value in numb is being overridden by new value from list.基本上 numb 中的每个值都被列表中的新值覆盖。

Do this instead.改为这样做。

def numbers_range(number):
  nums = []
  for i in range(number+1):
    nums.append(i)
  
  ans = "".join([str(no) for no in nums])
  return ans

print(numbers_range(5))

Here in code,在代码中,

  1. We are creating a new list called nums .我们正在创建一个名为nums的新列表。

  2. Then we are traversing from 0 to the number and appending each value into the list.然后我们从 0 遍历到数字并将每个值附加到列表中。

  3. Finally returning the list converted to string.最后返回转换为字符串的列表。

for i in range(number+1):
    numz = nums.append(i)

for g in range(len(nums)):
    numb = nums[g]

What you're doing here is iterating through a list and assign the items to variable one by one.您在这里所做的是遍历列表并将项目逐一分配给变量。 In each turn the variable is overwritten.在每一轮中,变量都会被覆盖。 So when the loop terminates only the value assigned is final value in the list.因此,当循环终止时,只有分配的值才是列表中的最终值。

In case you need to return a sequence of numbers you can implement your code with eval function. But the returning values are strings, not integers.如果您需要return一个数字序列,您可以使用eval function 来实现您的代码。但返回值是字符串,而不是整数。 As you're just returning them, it won't be an issue.因为您只是退货,所以这不是问题。

def numbers_range(number):
    x =0
    s =''
    while x<=number:
# if you don't need a new line replace '\n' with a space ' ' .
         s =(s+str(x)+"\n")
         x =x+1
    numb = eval("print(s)")
    return numb

when using the function,使用 function 时,

print(numbers_range(5))

output output

0
1
2
3
4
5

Hope this answer your question.希望这能回答你的问题。

one solution is一种解决方案是

def numbers_range(number):
            ans = [str(i) for i in range(number+1)]
            return " ".join(ans)
                
print(numbers_range(5))

another solution is另一个解决方案是

def numbers_range(number):
    ans = []
    for i in range(number+1):
        ans.append(str(i))
    return " ".join(ans)
    


print(numbers_range(5))

So I think the issue is in your use of range() .所以我认为问题出在您对range()的使用上。 In my experience, range() needs to be formatted like range(min, max) whereas you are formatting it like range(min) .根据我的经验, range()需要像range(min, max)一样格式化,而你正在像range(min)一样格式化它。

I think what you want would come from this code:我想你想要的会来自这段代码:

nums = []
def numbers_range(number):
  for i in range(0, number+1):
    numz = nums.append(i)
  for g in range(0, len(nums)):
   numb = nums[g]
  return numb

print(numbers_range(5))

Hope this works for you!希望这对你有用!

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

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