如何将数字嵌套列表转换为字符串列表? [英] How to convert nested list of numbers to list of strings?

查看:72
本文介绍了如何将数字嵌套列表转换为字符串列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个列表列表

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?

例如,期望结果为:

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']

每个嵌套列表,例如 [1,2,3,4] map(str,[1,2,3,4])都会创建一个字符串列表.在此示例中: ['1','2','3','4'] .使用 join 函数,可以将字符串数字映射到单个字符串,'1234'.

Ever every nested list, like [1, 2, 3, 4], map(str, [1, 2, 3, 4]) would create a list of strings. In this example: ['1', '2', '3', '4']. Using the join function, the string-numbers are mapped into a single string, '1234'.

由于对每个子列表都执行了此操作,因此结果是连接字符串的列表.

Since this operation is performed for every sub-list, the result is a a list of the joined strings.

这篇关于如何将数字嵌套列表转换为字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆