将字符串数组转换为整数python [重复]
Posted
技术标签:
【中文标题】将字符串数组转换为整数python [重复]【英文标题】:Convert string array into integer python [duplicate] 【发布时间】:2019-11-08 08:41:15 【问题描述】:我需要将以下字符串数组转换为整数。如何在python中做到这一点。 这些字符串是十六进制值。
['02', '01', '03', '01', '04', '01', '05', '01', '06', '01', '07', '01', '08', '01', '09', '01', '0a', '01', '0b', '01', '0c', '01', '0d', '01', '0e', '01', '0f', '01', '10', '01', '11', '01', '12', '01', '13', '01', '14', '01', '03', '02', '04', '02', '05', '02', '06', '02', '07', '02', '08', '02', '09', '02', '0a', '02', '0b', '02', '0c', '02', '0d', '02', '0e', '02', '0f', '02', '10', '02', '11', '02', '12', '02', '13', '02', '14', '02', '04', '03', '05', '03', '06', '03', '07', '03', '08', '03', '09', '03', '0a', '03', '0b', '03', '0c', '03', '0d', '03', '0e', '03', '0f', '03', '10', '03', '11', '03', '12', '03', '13', '03', '14', '03', '05', '04', '06', '04', '07', '04', '08', '04', '09', '04', '0a', '04', '0b', '04', '0c', '04', '0d', '04', '0e', '04', '0f', '04', '10', '04', '11', '04', '12', '04', '13', '04', '14', '04', '06', '05', '07', '05', '08', '05', '09', '05', '0a', '05', '0b', '05', '0c', '05', '0d', '05', '0e', '05', '0f', '05', '10', '05', '11', '05', '12', '05', '13', '05', '14', '05', '07', '06', '08', '06', '09', '06', '0a', '06', '0b', '06', '0c', '06', '0d', '06', '0e', '06', '0f', '06', '10', '06', '11', '06', '12', '06', '13', '06', '14', '06', '08', '07', '09', '07', '0a', '07', '0b', '07', '0c', '07', '0d', '07', '0e', '07', '0f', '07', '10', '07', '11', '07', '12', '07', '13', '07', '14', '07', '09', '08', '0a', '08', '0b', '08', '0c', '08', '0d', '08', '0e', '08', '0f', '08', '10', '08', '11', '08', '12', '08', '13', '08', '14', '08', '0a', '09', '0b', '09', '0c', '09', '0d', '09', '0e', '09', '0f', '09', '10', '09', '11', '09', '12', '09', '13', '09', '14', '09', '0b', '0a', '0c', '0a', '0d', '0a', '0e', '0a', '0f', '0a', '10', '0a', '11', '0a', '12', '0a', '13', '0a', '14', '0a', '0c', '0b', '0d', '0b', '0e', '0b', '0f', '0b', '10', '0b', '11', '0b', '12', '0b', '13', '0b', '14', '0b', '0d', '0c', '0e', '0c', '0f', '0c', '10', '0c', '11', '0c', '12', '0c', '13', '0c', '14', '0c', '0e', '0d', '0f', '0d', '10', '0d', '11', '0d', '12', '0d', '13', '0d', '14', '0d', '0f', '0e', '10', '0e', '11', '0e', '12', '0e', '13', '0e', '14', '0e', '10', '0f', '11', '0f', '12', '0f', '13', '0f', '14', '0f', '11', '10', '12', '10', '13', '10', '14', '10', '12', '11', '13', '11', '14', '11', '13', '12', '14', '12', '14', '13']
【问题讨论】:
让你的名单被称为string_list
然后你可以[int(el) for el in string_list]
【参考方案1】:
使用int
和base=16
进行转换,并对输出列表使用列表推导:
[int(num, base=16) for num in nums]
例子:
In [376]: nums = ['02', '01', '03', '01', '04', 'ab', 'cd']
In [377]: [int(num, base=16) for num in nums]
Out[377]: [2, 1, 3, 1, 4, 171, 205]
【讨论】:
【参考方案2】:您可以为此使用列表推导
>>> data = ['02', '01', '03', '01', '04', '01', '05', '01', '06', '01', '07', '01', '08', '01', '09', '01', '0a', '01', '0b', '01', '0c', '01', '0d', '01', '0e', '01', '0f', '01', '10', '01', '11', '01', '12', '01', '13', '01', '14', '01', '03', '02', '04', '02', '05', '02', '06', '02', '07', '02', '08', '02', '09', '02', '0a', '02', '0b', '02', '0c', '02', '0d', '02', '0e', '02', '0f', '02', '10', '02', '11', '02', '12', '02', '13', '02', '14', '02', '04', '03', '05', '03', '06', '03', '07', '03', '08', '03', '09', '03', '0a', '03', '0b', '03', '0c', '03', '0d', '03', '0e', '03', '0f', '03', '10', '03', '11', '03', '12', '03', '13', '03', '14', '03', '05', '04', '06', '04', '07', '04', '08', '04', '09', '04', '0a', '04', '0b', '04', '0c', '04', '0d', '04', '0e', '04', '0f', '04', '10', '04', '11', '04', '12', '04', '13', '04', '14', '04', '06', '05', '07', '05', '08', '05', '09', '05', '0a', '05', '0b', '05', '0c', '05', '0d', '05', '0e', '05', '0f', '05', '10', '05', '11', '05', '12', '05', '13', '05', '14', '05', '07', '06', '08', '06', '09', '06', '0a', '06', '0b', '06', '0c', '06', '0d', '06', '0e', '06', '0f', '06', '10', '06', '11', '06', '12', '06', '13', '06', '14', '06', '08', '07', '09', '07', '0a', '07', '0b', '07', '0c', '07', '0d', '07', '0e', '07', '0f', '07', '10', '07', '11', '07', '12', '07', '13', '07', '14', '07', '09', '08', '0a', '08', '0b', '08', '0c', '08', '0d', '08', '0e', '08', '0f', '08', '10', '08', '11', '08', '12', '08', '13', '08', '14', '08', '0a', '09', '0b', '09', '0c', '09', '0d', '09', '0e', '09', '0f', '09', '10', '09', '11', '09', '12', '09', '13', '09', '14', '09', '0b', '0a', '0c', '0a', '0d', '0a', '0e', '0a', '0f', '0a', '10', '0a', '11', '0a', '12', '0a', '13', '0a', '14', '0a', '0c', '0b', '0d', '0b', '0e', '0b', '0f', '0b', '10', '0b', '11', '0b', '12', '0b', '13', '0b', '14', '0b', '0d', '0c', '0e', '0c', '0f', '0c', '10', '0c', '11', '0c', '12', '0c', '13', '0c', '14', '0c', '0e', '0d', '0f', '0d', '10', '0d', '11', '0d', '12', '0d', '13', '0d', '14', '0d', '0f', '0e', '10', '0e', '11', '0e', '12', '0e', '13', '0e', '14', '0e', '10', '0f', '11', '0f', '12', '0f', '13', '0f', '14', '0f', '11', '10', '12', '10', '13', '10', '14', '10', '12', '11', '13', '11', '14', '11', '13', '12', '14', '12', '14', '13']
>>> [int(i, base=16) for i in data]
[2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 2, 12, 2, 13, 2, 14, 2, 15, 2, 16, 2, 17, 2, 18, 2, 19, 2, 20, 2, 4, 3, 5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 3, 12, 3, 13, 3, 14, 3, 15, 3, 16, 3, 17, 3, 18, 3, 19, 3, 20, 3, 5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 4, 11, 4, 12, 4, 13, 4, 14, 4, 15, 4, 16, 4, 17, 4, 18, 4, 19, 4, 20, 4, 6, 5, 7, 5, 8, 5, 9, 5, 10, 5, 11, 5, 12, 5, 13, 5, 14, 5, 15, 5, 16, 5, 17, 5, 18, 5, 19, 5, 20, 5, 7, 6, 8, 6, 9, 6, 10, 6, 11, 6, 12, 6, 13, 6, 14, 6, 15, 6, 16, 6, 17, 6, 18, 6, 19, 6, 20, 6, 8, 7, 9, 7, 10, 7, 11, 7, 12, 7, 13, 7, 14, 7, 15, 7, 16, 7, 17, 7, 18, 7, 19, 7, 20, 7, 9, 8, 10, 8, 11, 8, 12, 8, 13, 8, 14, 8, 15, 8, 16, 8, 17, 8, 18, 8, 19, 8, 20, 8, 10, 9, 11, 9, 12, 9, 13, 9, 14, 9, 15, 9, 16, 9, 17, 9, 18, 9, 19, 9, 20, 9, 11, 10, 12, 10, 13, 10, 14, 10, 15, 10, 16, 10, 17, 10, 18, 10, 19, 10, 20, 10, 12, 11, 13, 11, 14, 11, 15, 11, 16, 11, 17, 11, 18, 11, 19, 11, 20, 11, 13, 12, 14, 12, 15, 12, 16, 12, 17, 12, 18, 12, 19, 12, 20, 12, 14, 13, 15, 13, 16, 13, 17, 13, 18, 13, 19, 13, 20, 13, 15, 14, 16, 14, 17, 14, 18, 14, 19, 14, 20, 14, 16, 15, 17, 15, 18, 15, 19, 15, 20, 15, 17, 16, 18, 16, 19, 16, 20, 16, 18, 17, 19, 17, 20, 17, 19, 18, 20, 18, 20, 19]
当您转换为int
时,有第二个参数指定基数,16
在本例中为十六进制。
【讨论】:
以上是关于将字符串数组转换为整数python [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Django/Python:如何将整数转换为等效的枚举字符串? [复制]