Reputation: 81
Is there a build in method to transform a list of characters to a list of ascii values of those characters?
For example, The method should transform ['a','b','c']
to [ord('a'),ord('b'),ord('c')]
.
Upvotes: 0
Views: 303
Reputation: 3981
Don't think that Python has a build in method, but that's pretty easy to write a "one-liner".
source = ['a', 'b', 'c']
print([ord(c) for c in source])
outputs
[97, 98, 99]
Upvotes: 4
Reputation: 65
You can use map to call the ord function on all of the entries.
result = list(map(ord, ['a','b','c']))
print(results)
Upvotes: 0
Reputation: 7204
If you have python 3 you can do this with the bytes function:
list(bytes(''.join(['a','b','c']).encode()))
# [97, 98, 99]
Upvotes: 0