Reputation: 37
I apologize if my questions seem trivial. I would rather ask this in a chat room; however, my reputation is too low at the moment, so I am unable to ask anything in the Python chat room. I am currently learning Python for a class and the teacher gave us some practice problems to work to get us spun up. The function I am building now takes a number list and converts it to a string. The issue that I am having is that my if statement never evaluates to true. I have tried a few ways to work with the variables and have added many print statements to see if they should ever be equal, but to no avail. Thanks again in advance. I promise I am only asking after researching and trying many ways, but am now at a loss...Here is my code:
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
Upvotes: 1
Views: 1126
Reputation: 46628
The answer above properly addresses the question, but as a general tip: when reducing a list of values to a single value, use the reduce
function. I realise of course that this is a learning exercise, but it can be useful to know the relevant inbuilt function for the job. That makes your function much shorter:
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)
For example, the output from:
l = [7,4,11,11,14]
print nlist2string(l)
is the string "hello"
.
The reduce function takes two arguments, a function that will be used to collapse the list into a single value, and a list.
As a more simple example of what reduce
does:
function add(x,y):
return x + y
print reduce(add, [1, 4, 3, 10, 5])
//Output: 23
Upvotes: 1
Reputation: 19406
You have a list of strings, of numbers 0-25.
In Python, a string is never equal to a number, So, num1 == num2
is always False.
So, it should be
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]
is more appropriate(and will work).
Or even better
numbers = range(26)
And if you don't want to edit numbers
's value, use this conditional:
if num1 == int(num2):
This will convert num2 into an integer, which is what you want to do.
Also, in that case, you can use map (Built-in Function), for more readability like so:
numbers = map(str, range(26))
Upvotes: 1
Reputation: 640
In Python, string literals and numbers are separate types of object, and do not compare equal.
1 == "1"
returns False.
You're iterating through your list of strings of numerals, but it's not a list of actual numbers.
You can use the range() function (it's a python built-in) to generate a number list instead of typing it out by hand.
Upvotes: 0
Reputation: 208405
You are trying to compare integers to strings, try changing your definition for numbers
to the following:
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]
Or alternatively, numbers = range(26)
.
Currently when comparing num1
and num2
, you will be doing comparisons like 4 == '4'
, which will never be true:
>>> 4 == '4'
False
As an alternative to changing how you create your numbers
list, you could convert num2
to an integer or num1
to a string before the comparison, so either num1 == int(num2)
or str(num1) == num2
.
Upvotes: 5