Python If语句的计算结果永远不会为True

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

如果我的问题看起来微不足道,我道歉。我宁愿在聊天室里问这个问题;然而,目前我的声誉太低,所以我无法在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
def NLIST2字符串(nlist):
字符=['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']
数字=['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']
新列表=[]
nListLen=len(nlist)#var msgLen将是长度的整数
打印“转换前编号列表:”,nlist
索引=0
当索引
如果要将整数与字符串进行比较,请尝试将
数字的定义更改为以下内容:

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]
或者,编号=范围(26)

当前,在比较
num1
num2
时,您将进行类似
4==“4”
的比较,这永远不会是真的:

>>> 4 == '4'
False

作为更改创建
numbers
列表方式的替代方法,您可以在比较之前将
num2
转换为整数,或将
num1
转换为字符串,以便尝试将整数与字符串进行比较,尝试将
数字的定义更改为以下内容:

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]
或者,编号=范围(26)

当前,在比较
num1
num2
时,您将进行类似
4==“4”
的比较,这永远不会是真的:

>>> 4 == '4'
False

作为更改创建
numbers
列表方式的替代方法,您可以在比较之前将
num2
转换为整数或
num1
转换为字符串,因此
num1==int(num2)
str(num1)==num2
在Python中,字符串文本和数字是不同类型的对象,不要把它们等同起来

1 == "1"
返回False

您正在遍历数字字符串列表,但它不是实际数字的列表


您可以使用range()函数(它是python内置的)生成一个数字列表,而不是手动键入。

在python中,字符串文字和数字是不同类型的对象,不进行相等的比较

1 == "1"
返回False

您正在遍历数字字符串列表,但它不是实际数字的列表


您可以使用range()函数(它是python内置的)生成一个数字列表,而不是手动键入。

您有一个数字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]
更合适(而且会起作用)

甚至更好

numbers = range(26)

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

if num1 == int(num2):
这将把num2转换成一个整数,这就是您想要做的。
此外,在这种情况下,为了提高可读性,您可以使用:

numbers = map(str, range(26))

您有一个数字为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]
更合适(而且会起作用)

甚至更好

numbers = range(26)

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

if num1 == int(num2):
这将把num2转换成一个整数,这就是您想要做的。
此外,在这种情况下,为了提高可读性,您可以使用:

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)
例如,输出来自:

l = [7,4,11,11,14]
print nlist2string(l)
字符串是“hello”

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

作为
reduce
功能的更简单示例:

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

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

上面的答案正确地回答了这个问题,但作为一般提示:当将值列表减少为单个值时,请使用
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)
例如,输出来自:

l = [7,4,11,11,14]
print nlist2string(l)
字符串是“hello”

reduce函数有两个参数,一个用于折叠t的函数