How to convert ASCII values from a string of strings
up vote
-2
down vote
favorite
Is it possible to convert a string of ASCII characters from a list of strings:
[['You '], ['have '], ['made '], ['my '], ['day.']]
I understand that the conversion is done using ord(i)
as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:
[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]
Thank you!
python tuples ascii
add a comment |
up vote
-2
down vote
favorite
Is it possible to convert a string of ASCII characters from a list of strings:
[['You '], ['have '], ['made '], ['my '], ['day.']]
I understand that the conversion is done using ord(i)
as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:
[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]
Thank you!
python tuples ascii
4
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
You don't needord
if you're using Python 3.
– PM 2Ring
Nov 19 at 16:13
1
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Is it possible to convert a string of ASCII characters from a list of strings:
[['You '], ['have '], ['made '], ['my '], ['day.']]
I understand that the conversion is done using ord(i)
as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:
[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]
Thank you!
python tuples ascii
Is it possible to convert a string of ASCII characters from a list of strings:
[['You '], ['have '], ['made '], ['my '], ['day.']]
I understand that the conversion is done using ord(i)
as explained here. I just can't seem to figure out how to retain the list of list structure after the conversion that reads something like this:
[[ 1, 2, 3 ], [ 1, 2, 3, 4 ], etc. ]
Thank you!
python tuples ascii
python tuples ascii
edited Nov 19 at 16:51
asked Nov 19 at 16:08
nagymusic
263
263
4
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
You don't needord
if you're using Python 3.
– PM 2Ring
Nov 19 at 16:13
1
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52
add a comment |
4
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
You don't needord
if you're using Python 3.
– PM 2Ring
Nov 19 at 16:13
1
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52
4
4
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
You don't need
ord
if you're using Python 3.– PM 2Ring
Nov 19 at 16:13
You don't need
ord
if you're using Python 3.– PM 2Ring
Nov 19 at 16:13
1
1
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
use list comprehension or maps to traverse into the inner elements and apply the ord
function:
example:
# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
add a comment |
up vote
1
down vote
You have two options:
Nest your list comprehension:
[[ord(c) for c in nested[0]] for nested in outerlist]
In Python 3, you can encode the string to a
bytes
object, and convert that to a list.bytes
objects are just sequences of integers, after all:
[list(nested[0].encode('ascii')) for nested in outerlist]
The Python 2 equivalent is to use the
bytearray()
type; forstr
(byte strings):
[list(bytearray(nested[0])) for nested in outerlist]
In both cases I assume that your nested list contain just a single element each, a string.
Demo on Python 3.7:
>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
use list comprehension or maps to traverse into the inner elements and apply the ord
function:
example:
# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
add a comment |
up vote
3
down vote
accepted
use list comprehension or maps to traverse into the inner elements and apply the ord
function:
example:
# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
use list comprehension or maps to traverse into the inner elements and apply the ord
function:
example:
# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]
use list comprehension or maps to traverse into the inner elements and apply the ord
function:
example:
# python 3 (py2 syntax slightly different)
x = [['You '], ['have '], ['made '], ['my '], ['day.']]
[list(map(ord, i[0])) for i in x]
# outputs
Out[41]:
[[89, 111, 117, 32],
[104, 97, 118, 101, 32],
[109, 97, 100, 101, 32],
[109, 121, 32],
[100, 97, 121, 46]]
answered Nov 19 at 16:12
Haleemur Ali
11.9k21637
11.9k21637
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
add a comment |
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
Thanks a lot! This is very helpful. Could you suggest how to implement this into a statement that would enable one to replace all values less than a certain value by None (32 => None), and then subtract the rest of the lists by another value? Something like: if number < 65: q = None, else q = number - 12?
– nagymusic
Nov 19 at 16:31
add a comment |
up vote
1
down vote
You have two options:
Nest your list comprehension:
[[ord(c) for c in nested[0]] for nested in outerlist]
In Python 3, you can encode the string to a
bytes
object, and convert that to a list.bytes
objects are just sequences of integers, after all:
[list(nested[0].encode('ascii')) for nested in outerlist]
The Python 2 equivalent is to use the
bytearray()
type; forstr
(byte strings):
[list(bytearray(nested[0])) for nested in outerlist]
In both cases I assume that your nested list contain just a single element each, a string.
Demo on Python 3.7:
>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
add a comment |
up vote
1
down vote
You have two options:
Nest your list comprehension:
[[ord(c) for c in nested[0]] for nested in outerlist]
In Python 3, you can encode the string to a
bytes
object, and convert that to a list.bytes
objects are just sequences of integers, after all:
[list(nested[0].encode('ascii')) for nested in outerlist]
The Python 2 equivalent is to use the
bytearray()
type; forstr
(byte strings):
[list(bytearray(nested[0])) for nested in outerlist]
In both cases I assume that your nested list contain just a single element each, a string.
Demo on Python 3.7:
>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
add a comment |
up vote
1
down vote
up vote
1
down vote
You have two options:
Nest your list comprehension:
[[ord(c) for c in nested[0]] for nested in outerlist]
In Python 3, you can encode the string to a
bytes
object, and convert that to a list.bytes
objects are just sequences of integers, after all:
[list(nested[0].encode('ascii')) for nested in outerlist]
The Python 2 equivalent is to use the
bytearray()
type; forstr
(byte strings):
[list(bytearray(nested[0])) for nested in outerlist]
In both cases I assume that your nested list contain just a single element each, a string.
Demo on Python 3.7:
>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
You have two options:
Nest your list comprehension:
[[ord(c) for c in nested[0]] for nested in outerlist]
In Python 3, you can encode the string to a
bytes
object, and convert that to a list.bytes
objects are just sequences of integers, after all:
[list(nested[0].encode('ascii')) for nested in outerlist]
The Python 2 equivalent is to use the
bytearray()
type; forstr
(byte strings):
[list(bytearray(nested[0])) for nested in outerlist]
In both cases I assume that your nested list contain just a single element each, a string.
Demo on Python 3.7:
>>> l = [['You '], ['have '], ['made '], ['my '], ['day.']]
>>> [[ord(c) for c in nested[0]] for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
>>> [list(nested[0].encode('ascii')) for nested in l]
[[89, 111, 117, 32], [104, 97, 118, 101, 32], [109, 97, 100, 101, 32], [109, 121, 32], [100, 97, 121, 46]]
answered Nov 19 at 16:17
Martijn Pieters♦
692k12923972236
692k12923972236
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378577%2fhow-to-convert-ascii-values-from-a-string-of-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Are your nested lists always just a single element long?
– Martijn Pieters♦
Nov 19 at 16:11
You don't need
ord
if you're using Python 3.– PM 2Ring
Nov 19 at 16:13
1
BTW, you have a list of lists, with each inner list containing a string. There are no tuples in that code.
– PM 2Ring
Nov 19 at 16:15
Ok. Thanks. Just revised the question.
– nagymusic
Nov 19 at 16:52