Python If语句从不计算为Tru

2024-10-01 15:44:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我的问题看起来微不足道,我道歉。我宁愿在聊天室里问这个问题;但是,我现在的名声太低了,所以我不能在Python聊天室问任何事情。我目前正在为一节课学习Python,老师给了我们一些练习题,让我们开始练习。我正在构建的函数现在接受一个数字列表并将其转换为字符串。我现在的问题是,我的if语句的计算结果从来都不是真的。我尝试了几种处理变量的方法,并添加了许多print语句,看看它们是否应该相等,但都没有效果。再次提前感谢。我保证我只是在研究和尝试了很多方法之后才问的,但现在我不知所措…以下是我的代码:

def nlist2string(nlist):
    characters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    numbers = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25']
    newList = []
    nListLen = len(nlist)         # var msgLen will be an integer of the length

    print 'Number list before conversion: ', nlist

    index = 0
    while index < nListLen:
        print 'Index at: ', nlist[index]
        num = nlist[index]
        print 'Is num equal to nlist indexed? ', num
        newNum = num % 26

        i = 0
        while i < 26:
            num1 = newNum
            num2 = numbers[i]
            print 'num1 = ', num1
            print 'num2 = ', num2
            if (num1 == num2):
                newList.append(characters[i])
                print 'Here is the current newList: ', newList
            else:
                print 'They never equal each other.'
            i = i + 1
        index = index + 1
    return newList

    numMessage = [28, 0, 33]
    convertedNumMsg = nlist2string(numMessage)
    print 'Number list after conversion: ', convertedNumMsg

Tags: the方法indexif语句numprintnumbers
3条回答

您有一个字符串列表,数字为0-25。
在Python中,字符串永远不等于数字,因此,num1 == num2总是False。在

所以,应该是的

numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

更合适(而且会起作用)。在

甚至更好

^{pr2}$

如果您不想编辑numbers的值,请使用以下条件:

if num1 == int(num2):

这将把num2转换成一个整数,这正是您要做的。
另外,在这种情况下,您可以使用map (Built-in Function),以获得更高的可读性,如下所示:

numbers = map(str, range(26))

上面的答案正确地解决了这个问题,但是作为一个一般提示:当将一个值列表缩减为一个值时,使用reduce函数。我当然意识到这是一种学习练习,但了解工作的相关内在功能可能会很有用。这使得你的功能大大缩短:

def nlist2string(nlist):

    def convert_to_alpha(s):
        if isinstance(s,str): //check if s is already a string
            return s          //if it is, return it unchanged
        else:
            return str(unichr(s+97)) //otherwise, get the corresponding alphabet
                                     //and return that
    def reduce_func(x,y):
        //convert the numbers to alphabets
        //and join the two together
        return convert_to_alpha(x) + convert_to_alpha(y) 

    return reduce(reduce_func, nlist)

例如,输出来自:

^{pr2}$

是字符串"hello"。在

reduce函数接受两个参数,一个用于将列表折叠为单个值的函数和一个列表。在

作为reduce的一个更简单的例子:

function add(x,y):
    return x + y

print reduce(add, [1, 4, 3, 10, 5])
//Output: 23

您正在尝试将整数与字符串进行比较,请尝试将numbers的定义更改为以下内容:

numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

或者,numbers = range(26)。在

目前,当比较num1num2时,您将进行类似4 == '4'的比较,这永远不会是真的:

^{pr2}$

除了更改创建numbers列表的方式之外,还可以在比较之前将num2转换为整数或{}转换为字符串,因此num1 == int(num2)或{}。在

相关问题 更多 >

    热门问题