如何防止嵌套for循环导致多次返回相同的值?

时间:2021-07-02 14:29:51

I am trying to write a program that goes through items in a list. Takes those items apart piece by piece and then assigns a value to them. In this case I am using numbers as my items. I want to assign a value of either prime, even, or other. (Represented as 'p', 'e', or 'o'.) Example code:

我正在尝试编写一个遍历列表中项目的程序。将这些项目逐个分开,然后为它们分配值。在这种情况下,我使用数字作为我的项目。我想分配一个素数,偶数或其他值。 (表示为'p','e'或'o'。)示例代码:

testCode = ''
testCodeList = []
prime = ['2','3','5','7']
even = ['4','6','8']
other = ['1','9']
list = ['76']
for item in list:
    for num in item:
        for eve in even:
            if num == eve:
                testCode = testCode + 'e'
            else:
                for pri in prime:
                    if num == pri:
                        testCode = testCode + 'p'
                    else:
                        for othello in other:
                            if num == othello:
                                testCode = testCode + 'o'
                            else:
                                pass
print testCode

And here is an example of the output:

以下是输出的示例:

pppe

How do I make it only give me 'pe' like I want it too? Thanks for your help!

我怎么做它只给我'pe'就像我也想要它?谢谢你的帮助!

2 个解决方案

#1


1  

The three inner for loops where you go over the primes, evens and others is where the problem rests. You should do this instead:

三个内部for循环,你越过素数,均衡和其他问题的地方。你应该这样做:

testCode = ''
testCodeList = []
prime = ['2','3','5','7']
even = ['4','6','8']
other = ['1','9']
list = ['76']
for item in list:
    for num in item:
        if num in even:
            testCode = testCode + 'e'
        elif num in prime:
            testCode = testCode + 'p'
        elif num in other:
            testCode = testCode + 'o'
        else:
            pass

Also depending on your objective of this code, you may want to replace the declaration of the even, prime and other lists with functions/commands to check to see if the number is in fact even, prime or other. In other words, for example for the even part, you could check to see if the number is divisible by two instead of checking to see if the number exists in the even list. This can be done by replacing if num in even: with if int(num) % 2 == 0:.

此外,根据您对此代码的目的,您可能希望用函数/命令替换even,prime和其他列表的声明,以检查该数字是否实际上是偶数,素数或其他。换句话说,例如对于偶数部分,您可以检查该数字是否可被2整除,而不是检查该数字是否存在于偶数列表中。这可以通过替换if中的num来实现:if if int(num)%2 == 0:。

#2


0  

There is no reason to use a bunch of nested loops if you just want to check for membership. Use the in operator.

如果您只想检查成员身份,则没有理由使用一堆嵌套循环。使用in运算符。

testCode = ''
prime = ['2','3','5','7']
even = ['4','6','8']
other = ['1','9']
lst = ['76']
for item in lst:
    for num in item:
        if num in even:
            testCode += 'e'
        if num in prime:
            testCode += 'p'
        if num in other:
            testCode += 'o'

This results in testCode being 'pe' as desired. Note that repeatedly adding to a string is probably not the best way to do things. Consider appending to the list you never used, testCodeList, instead. Also, naming something list means you can't use the built-in function list() without extra effort.

这导致testCode根据需要为“pe”。请注意,重复添加到字符串可能不是最好的方法。考虑附加到您从未使用过的列表testCodeList。此外,命名一些列表意味着您无需额外的努力就无法使用内置函数列表()。

#1


1  

The three inner for loops where you go over the primes, evens and others is where the problem rests. You should do this instead:

三个内部for循环,你越过素数,均衡和其他问题的地方。你应该这样做:

testCode = ''
testCodeList = []
prime = ['2','3','5','7']
even = ['4','6','8']
other = ['1','9']
list = ['76']
for item in list:
    for num in item:
        if num in even:
            testCode = testCode + 'e'
        elif num in prime:
            testCode = testCode + 'p'
        elif num in other:
            testCode = testCode + 'o'
        else:
            pass

Also depending on your objective of this code, you may want to replace the declaration of the even, prime and other lists with functions/commands to check to see if the number is in fact even, prime or other. In other words, for example for the even part, you could check to see if the number is divisible by two instead of checking to see if the number exists in the even list. This can be done by replacing if num in even: with if int(num) % 2 == 0:.

此外,根据您对此代码的目的,您可能希望用函数/命令替换even,prime和其他列表的声明,以检查该数字是否实际上是偶数,素数或其他。换句话说,例如对于偶数部分,您可以检查该数字是否可被2整除,而不是检查该数字是否存在于偶数列表中。这可以通过替换if中的num来实现:if if int(num)%2 == 0:。

#2


0  

There is no reason to use a bunch of nested loops if you just want to check for membership. Use the in operator.

如果您只想检查成员身份,则没有理由使用一堆嵌套循环。使用in运算符。

testCode = ''
prime = ['2','3','5','7']
even = ['4','6','8']
other = ['1','9']
lst = ['76']
for item in lst:
    for num in item:
        if num in even:
            testCode += 'e'
        if num in prime:
            testCode += 'p'
        if num in other:
            testCode += 'o'

This results in testCode being 'pe' as desired. Note that repeatedly adding to a string is probably not the best way to do things. Consider appending to the list you never used, testCodeList, instead. Also, naming something list means you can't use the built-in function list() without extra effort.

这导致testCode根据需要为“pe”。请注意,重复添加到字符串可能不是最好的方法。考虑附加到您从未使用过的列表testCodeList。此外,命名一些列表意味着您无需额外的努力就无法使用内置函数列表()。