简体   繁体   English

python:将列表中的整数项连接到单个字符串

[英]python: concatenate integer items in a list to a single string

Is there a better way of the following for concatenating items in a list that are "integers" into a string: 是否有以下更好的方法将“整数”列表中的项目连接成字符串:

import numpy as np
my_list = [1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0]
changed_list = np.char.mod('%d', my_list)
final_ans = ''.join(changed_list )

我不确定您的意思是更好,但是您可以尝试一下。

''.join([str(x) for x in my_list])

这个怎么样?

''.join([str(item) for item in my_list])

You can use the bitstring module: 您可以使用bitstring模块:

>>> from bitstring import BitArray
>>> f'{BitArray(my_list).uint:b}'
'110000111010'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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