简体   繁体   English

我的 for 循环有问题 Python

[英]I have a problem with the for loop Python

I have a problem when using the for loop, I don't know why my loop is not working as expected.我在使用 for 循环时遇到问题,我不知道为什么我的循环没有按预期工作。

CODE:代码:

class Solution:
    def searchInsert(nums, target):
        pos = 0
        for i in nums:
            if nums[pos] != target:
                pos = pos + 1
                
            print(pos)
            break

Solution.searchInsert([1,3,5,6], 5)

This program receives an array of integers and another integer which I call target, the script has to give me back the position in the array in which we have the number of the target.该程序接收一个整数数组和另一个我称之为目标的 integer,脚本必须返回数组中的 position,我们在其中包含目标的编号。

In this case my array "nums" contains [1,3,5,6] and my target is 5, so the result must be 2, because the number of the target (5) is in the position "2" of the array.在这种情况下,我的数组“nums”包含 [1,3,5,6],而我的目标是 5,所以结果必须是 2,因为目标 (5) 的编号在数组的 position“2”中.

The problem comes when I run the script.当我运行脚本时,问题就来了。 Instead of a 2 the script gives me a 1脚本给了我 1 而不是 2

If someone catches the error in the code please tell me.如果有人发现代码中的错误,请告诉我。

You are closing the loop after the first iteration: you need to add an else: block so it can keep going until the condition is met...您在第一次迭代后关闭循环:您需要添加一个else:块,以便它可以继续运行直到满足条件...

class Solution:
    def searchInsert(nums, target):
        pos = 0
        for i in nums:
            if nums[pos] != target:
                pos = pos + 1
            else:    
                print(pos)
                break
>>> Solution.searchInsert([1,3,5,6],5)
2

I am posting here just fixed code but as others pointed out, its a strange way to deal with it.我在这里发布的只是固定代码,但正如其他人指出的那样,这是一种奇怪的处理方式。 But I understand, if you learn Python and come from other language (Visual Basic?), one would go this way.但我明白,如果你学习 Python 并且来自其他语言(Visual Basic?),那么你会这样学习 go。 In python indent is crucial.在 python 缩进是至关重要的。 In your code, you need to increase pos everytime code goes through cycle, not only when you find/did not find the value you want.在您的代码中,您需要在每次代码经过循环时增加 pos,而不仅仅是当您找到/没有找到您想要的值时。

class Solution:
    def searchInsert(nums, target):
        pos = 0
        for i in nums:
            if nums[pos] == target:
                print(pos) #replace with return pos ?
                break
            pos = pos + 1 #increase outside above condition

Solution.searchInsert([1,3,5,6], 5)

To make this code easier, one liner, investigate other users comments.为了使这段代码更容易,一个班轮,调查其他用户的意见。 Or research array index, length and later pandas module.或者研究数组索引,长度和后来的pandas模块。

you could use enumerate which gives you the index and value你可以使用 enumerate 给你索引和值

class Solution:
    def searchInsert(nums, target):
        for index, value in enumerate(nums):
            if value == target:
                return index 
             
print(Solution.searchInsert([1,3,5,6], 5))

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

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