[英]how to join list of ints with ints output python
i have list of integers, how this list join to get output with the same list but joined every three value. 我有整数列表,该列表如何联接以获取具有相同列表的输出,但每三个值联接一次。
lst = [22, 33, 44, 11,33,11]
how to get 如何获得
lstOutput = [223344, 113311]
with string everything clear, i try: 用字符串一切都清楚,我尝试:
[lst[i:i + 3] for i in range(0, len(b), lst)]
Sorry for english 对不起英语
you can use map(str,lst[i:i+3])
to convert your int element to string then join them with join
function : 您可以使用
map(str,lst[i:i+3])
将int元素转换为字符串,然后使用join
函数将它们join
:
>>> [''.join(map(str,lst[i:i+3])) for i in range(0,len(lst),3)]
['223344', '113311']
and if you want the int
value use this : 如果想要
int
值,请使用以下命令:
>>> l=[lst[i:i+3] for i in range(0,len(lst),3)]
>>> [sum(j*(100**i) for i,j in enumerate(i[::-1])) for i in l]
[223344, 113311]
as l
is equal [[22, 33, 44], [11, 33, 11]]
and you need to multiple the element with 100 to power of index you need to reverse the split list by [::-1]
and convert every list to its reverse like [44, 33, 22]
and then add them. 因为
l
等于[[22, 33, 44], [11, 33, 11]]
并且您需要将元素乘以100才能得到索引的幂,所以您需要将拆分列表反转[::-1]
并转换每个列表都相反[44, 33, 22]
,然后添加它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.