我怎样才能使一个二维数组与现有的名单? [英] How can i make a 2D array with existing lists?

查看:102
本文介绍了我怎样才能使一个二维数组与现有的名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个称为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]

更简单地说,你可以跳过中间变量并用一个列表COM prehension

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

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

这篇关于我怎样才能使一个二维数组与现有的名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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