简体   繁体   English

如何将嵌套的数字列表转换为字符串列表?

[英]How to convert nested list of numbers to list of strings?

I have a list of lists below 我有一份清单如下

p=[[1,2,3,4],[2,3,4,1]]

How do I put the sublist into a string? 如何将子列表放入字符串?

For example, desire result is: 例如,欲望结果是:

p=["1234","2341"]

It can be done by converting every integer to string and joining the strings: 可以通过将每个整数转换为字符串并连接字符串来完成:

p = [''.join(map(str, sub_list)) for sub_list in p]  # ['1234', '2341']

Ever every nested list, like [1, 2, 3, 4] , map(str, [1, 2, 3, 4]) would create a list of strings. 每个嵌套列表,如[1, 2, 3, 4]map(str, [1, 2, 3, 4])都会创建一个字符串列表。 In this example: ['1', '2', '3', '4'] . 在这个例子中: ['1', '2', '3', '4'] Using the join function, the string-numbers are mapped into a single string, '1234' . 使用join函数,字符串数字被映射为单个字符串'1234'

Since this operation is performed for every sub-list, the result is aa list of the joined strings. 由于对每个子列表执行此操作,因此结果是已连接字符串的列表。

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

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