Python 列表追加真布尔值 [英] Python Lists Append True Boolean

查看:23
本文介绍了Python 列表追加真布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的[不完整]代码被设计为接收一个nx的数字,长度为xn,并返回下一个泛数字.该代码识别出作为参数传入函数的数字中缺少 nx 之间的哪个数字,并返回(暂时,直到函数进一步开发),两个列表,原始数字本身及其作为列表成员的单个数字,以及一个列表,将数字 nx 作为成员,这些数字存在于被布尔值 True 替换的原始长度 xn 中.

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.'

我已尝试解决此问题.但是,我无法查明其来源.我已经考虑过 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 输入的 True1你想更换下一个.

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.

这篇关于Python 列表追加真布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆