0

Currently I have a list of strings, lets say:

list1 = [1,2,3,4]

I want to convert this list of numbers into their corresponding letters, using:

output = [chr(ord((str(x+96)))) for x in list1]

When I do this, I get an error saying the program expects a string length of one, but in my code it gives a string length of 3 because of the '' around each number after it is converted into a string. The conversion into a string is necessary because it has to be in the string format in order for ord to work.

So my question to you guys is how can I fix this and/or get rid of these quotes?

In case anyone was wondering, it is supposed to come out as

output = [a,b,c,d]
2
  • 1
    list1 is a list of integers, not a list of strings. Commented Jun 1, 2015 at 3:07
  • What you are expecting to be output is not possible. 'a' is a character, where as a wihout quotes is 'some object'. That object is not defined in this particular case. Would you please explain 'why' you want the output to be in the format the way you want it? Commented Jun 1, 2015 at 3:09

5 Answers 5

2

You have an integer, add 96 to it, the result should just be:

output = [chr(x+96) for x in list1]

You can ditch ord and str and entirely.

list1 = [1,2,3,4]

output = [chr(x+96) for x in list1]

print output #Prints: ['a', 'b', 'c', 'd']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That makes sense now… Your code seems to work, however, how can I remove the quotes from the result?
2

Your Answer will be like:

 output = [chr((x+96)) for x in list1]

ord takes a character and return the integer ordinal of the character. In your code, you are doing:

ord((str(x+96)))

This is like,

ord((str(1+96)))
ord('97')

So you are getting the error. ord's argument will be a one character string. like:

>>> ord('a')
>>> 97

But to get your expected output you don't need to use ord.

2 Comments

Thank you! This seems to work, expect when I print the result each letter comes out with quotes around them
In your code the letters are string inside a list. In python strings are represented inside quotes. So these quotes can not be eliminated.
1

Printing a list prints the string representation of its elements which includes the single quotes around the string elements inside your list. Instead just format the string yourself:

>>> '[{}]'.format(','.join([chr((x+96)) for x in list1]))
'[a,b,c,d]'

Or in printed form:

>>> print '[{}]'.format(','.join([chr((x+96)) for x in list1]))
[a,b,c,d]

The format method allows you to format a string. In this case, the curly braces are used as a placeholder for the value to include in the final string. The square brackets are part of the format to provide the actual array-like output. The join method takes an array of objects and joins them together to form a single combined string. In this case, I've joined several objects using a literal comma. This produces a comma separated string output. The inner list comprehension is essentially the code that you had already provided.

6 Comments

@Flo: My answer gives the exact output that you asked for.
Ah, it does work. I was a little hesitant about it because I did not know how it worked, so can you explain it? I am still a novice at programming…Thanks though!
Are you using Python 2 or Python 3?
Not sure… SageMath's Python Worksheet
Use import sys; print (sys.version) to find out. If you are currently using parenthesis in your print statements then you're probably using Python 3. If not, then it is probably Python 2. I've given links to the Python 2 docs, but will update my answer if you specify a different version.
|
0

Can't you just do it the sane way which is

string = " abcdefghijklmnopqrstuvwxyz"
newlist = []
for num in nums:
    newlist.append(string[num])

Comments

0

Assuming you only want a string to print:

>>> list1 = [1, 2, 3, 4]
>>> pretty_string = ""
>>> # strings are immutable so need to
>>> # create new and assign to old variable name.
>>> for i in list1:
>>>     pretty_string = pretty_string + str(i) + ", "
>>> pretty_string = pretty_string[:-2] # remove trailing comma and space
>>> print(pretty_string)
1, 2, 3, 4

The str(i) method converts i to type string

Or, to print what you asked verbatim:

>>> print("output = [" + pretty_string + "]")
output = [1, 2, 3, 4]

If however you want a list of character representations of your integer list elements then:

>>> list1 = [1, 2, 3, 4] # integer values
>>> character_reps = []
>>> for i in list1:
>>>     character_reps.append(str(i))
>>> for i in character_reps:
>>>     print(i) # no need to convert as members already string types
1
2
3
4

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.