Python列表附加True布尔值

问题描述:

下面的[incomplete]代码旨在接收长度为 xn n x 个数字,并返回下一个全数字.该代码标识作为函数参数传递的数字中缺少 n x 中的哪个数字,并返回(暂时,直到该函数进一步扩展为止).已开发),两个列表,原始数字本身(其中各个数字作为列表的成员)和一个列表,其中数字 n x 作为成员,并且这些数字出现在原始长度为 xn 的数目中,被布尔值 True 取代.

The following [incomplete] code is designed to take in an n to x number, of length x-n, and return the value of the next pandigital number. The code identifies which number between n and x is missing from the number passed in as an argument to the function, and returns (for the time being, until the function is further developed), two lists, the original number itself with its individual digits as members of a list, and a list, with the numbers n to x as members, with those numbers which are present in the original number of length x-n being replaced by the Boolean value True.

def nextPandigital(n,lower,upper):
    digits = []
    requiredDigits = []

    #Completed loop
    for digit in str(n):
    digits.append(int(digit))

    #Completed loop
    for num in range(lower,upper+1):
    requiredDigits.append(num)

    for number in requiredDigits:
        if str(number) in str(digits):
            x = requiredDigits.index(number)
            #requiredDigits[x] = 'True'
            requiredDigits[x] = True
    return digits, requiredDigits

尽管,对于Enthought Canopy中 nextPandigital(1023456789,0,9) 的输入参数,返回的第二个列表应为 [True,True,True,True,True,True,True,True,True,True] ,返回的第二个列表的值为:实际上是 [True,1,True,True,True,True,True,True,True,True] ,原始 requiredDigits 列表中的1不会被 True 取代.

Although, for the input parameters of nextPandigital(1023456789,0,9) in Enthought Canopy, the second list returned should read [True,True,True,True,True,True,True,True,True,True], the value of the second list returned is, in fact [True,1,True,True,True,True,True,True,True,True], with the 1 from the original requiredDigits list not being replaced by True.

我知道循环或代码的一般流程没有问题,因为当注释 requiredDigits[x] = True 行代码,并且当前注释的代码未注释时,代码按预期工作, requiredDigits 中的所有数字都由字符串值'True.'

I know that there is no issue with the loop, or the general flow of the code, for when the requiredDigits[x] = True line of code is commented, and the currently commented code is uncommented, the code works as it is intended to, with all digits in requiredDigits being replaced by the String value 'True.'

我已尝试解决此问题.但是,我无法查明其来源.我考虑到 True == 1 返回True的事实.但是,当将值True替换为False时,在 requiredDigits[x] = True 行中,代码仍将按预期工作.

I have attempted to resolve this issue. However, I am not able to pinpoint its source. I have considered to fact that True == 1 returns True. However, when the value True is replaced by False, in the line requiredDigits[x] = True, the code still works as it is intended to.

任何有关此问题的答案/帮助/建议/建议将不胜感激.预先谢谢您.

问题在于使用index查找要分配到的位置.由于True等于1,您错误地认为为0输入的True是您接下来要替换的1.

The issue is with using index to find where to assign to. Since True is equal to 1, you're mistakenly thinking that the True you entered for 0 is the 1 you want to replace next.

一种解决方案是在迭代列表时使用enumerate获取索引,而不是以后需要使用index来找到它们:

A solution would be to use enumerate to get indexes as you iterate over your list, rather than needing to find them later using index:

for x, number in enumerate(requiredDigits):
    if str(number) in str(digits):
        requiredDigits[x] = True

通常,更好的解决方案是使用列表理解一次创建列表,而不是先从数字开始然后在以后替换其中一些:

A better solution in general would be to use a list comprehension to create the list in one go, rather than starting with numbers and replacing some of them later:

requiredDigits = [True if num in digits else num for num in range(lower,upper+1)]

在针对digits的成员资格测试中,我还摆脱了对str的不必要调用.您正在进行子字符串测试,而不是测试数字本身是否在列表中.那可能不会造成错误,因为您关心的数字都是一个数字长,并且列表的字符串表示形式没有任何多余的数字.但总的来说,在不需要时使用字符串操作不是一个好主意.

I'm also getting rid of the unnecessary calls to str in the membership test against digits. You were doing substring testing, rather than testing if the numbers themselves were in the list. That probably wasn't going to cause errors, since the numbers you care about are all one digit long, and the string representation of a list doesn't have any extraneous digits. But in general, its not a good idea to use string operations when you don't need to.