python: concatenate integer items in a list to a single string

我只是一个虾纸丫 提交于 2021-02-05 06:10:43

问题


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 )

回答1:


Im not sure what you mean by better, but you could try this.

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



回答2:


how about this?

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



回答3:


You can use the bitstring module:

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


来源:https://stackoverflow.com/questions/50184243/python-concatenate-integer-items-in-a-list-to-a-single-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!