简体   繁体   中英

How to add strings to a list?

So i'm trying to add strings of binary to a list but instead, i'm adding each individual character. I'm a new to python so i'm not quite sure how to do this myself(also, when getting the strings I need to ignore the spaces in between each string of binary)

str_msg = "This is a message!"
final = ' '.join(format(ord(x), 'b') for x in str_msg)
print(final)

which gives:

1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1101101 1100101 1110011 1110011 1100001 1100111 1100101 100001

but I then try to create the list this way:

binlist = []
for value in final:
    binlist.append(value)
print(binlist)

I get:

 ['1', '0', '1', '0', '1', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '0', '0', '0', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '1', '1', '0', '1', ' ', '1', '1', '0', '0', '1', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '1', '0', '0', '0', '0', '1', ' ', '1', '1', '0', '0', '1', '1', '1', ' ', '1', '1', '0', '0', '1', '0', '1', ' ', '1', '0', '0', '0', '0', '1'] 

The expression:

' '.join(format(ord(x), 'b') for x in str_msg)

is using a generator expression to yield each character in the string: str_msg converted to its ASCII value, represented in binary which is then getting passed into the .join method of the string ' ' (a space). As a result, you get a space-separated string of the binary representation of that string.

From the sounds of it, you want a list of strings containing the binary representation of each character.

This can be achieved with a list-comprehension which has the same syntax as a generator expression, but uses square brackets:

[format(ord(x), 'b') for x in str_msg]

As said above:

str_msg = "This is a message!"
final = ' '.join(format(ord(x), 'b') for x in str_msg)
print(final.split())

output:

['1010100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101', '100001']

list comprehension

str_msg = [format(ord(ch),'b') for ch in "this is a message"]
print(str_msg)

output

['1110100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101']

map

str_msg = list(map(lambda x: format(ord(x), 'b'), 'this is a message'))
print(str_msg)

output

['1110100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101']

You just want to split the string by spaces. On your final variable,

'1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1101101 1100101 1110011 1110011 1100001 1100111 1100101 100001'

do, final = final.split() and you will get your list

['1010100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101', '100001']

So while all of the answers i've gotten are vaild, I've choosen to accept @Joe Iddon 's answer since it is the answer that gives me the most optimal code(ie the shortest).

Thanks to everybody who helped with the question!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM