不是Python的副本- 将字符串转换为数组,因为那里的答案仅与 Python 2 相关
如何将字符串转换为Python 3 数组?
我知道如何将字符串转换为列表:
>>> string = 'abcd'
>>> string
'abcd'
>>> list(string)
['a', 'b', 'c', 'd']
但是,我需要一个 Python数组。
我需要一个特定于Python 3的答案
import array as arr
output = arr.array('b', [ord(c) for c in 'abcdef'])
会输出
array('b', [97, 98, 100, 101, 102])
当然chr()
,当您需要将它们用作字母/字符串时,您必须记住使用 将它们转换回字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句