如何使用现有列表制作二维数组? [英] How can i make a 2D array with existing lists?

查看:23
本文介绍了如何使用现有列表制作二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个名为 'mazeline' 的 txt 数据,如下所示:

for instance, i have a txt data called 'mazeline' like this:

abcd
cdae
korp

所以我首先列出了 3 个列表:

So i first made 3 lists:

mazeline = readmaze.split()
mline0 =  list(mazeline[0])
mline1 =  list(mazeline[1])
mline2 =  list(mazeline[2])

所以这 3 个列表是:

So the 3 lists are:

mline0 = [a,b,c,d]
mline1 = [c,d,a,e]
mline2 = [k,o,r,p]

我想制作一个这样的二维数组:

and i want to make a 2D array like this:

[[a,b,c,d],[c,d,a,e],[k,o,r,p]]

或者有什么方法可以直接从第一个数据中创建一个二维数组?

or is there any way that i can make a 2d array directly from the first data?

有什么建议吗?任何帮助都会很好.

any suggestions? any help would be good.

推荐答案

只需将列表放在另一个列表中

Just put the lists inside another list

res = [mline0, mline1, mline2]

更简单的是,您可以跳过中间变量并使用列表推导式

more simply, you can skip the intermediate variables and use a list comprehension

res = [list(mline) for mline in readmaze.split()]

这篇关于如何使用现有列表制作二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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