文件到嵌套列表 [英] file to nested list

查看:62
本文介绍了文件到嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多麻烦...所以基本上我给了一个文件例如:


File =


> 123

qwerty

> 567

tyuiyuy

> 987

poiuyt


基本上输出应该是[["> 123" ,qwerty,["> 567" ,tyuiuy,[" 987" ,poiuyt]]


i不知道怎么做才能有人帮助我吗?。

I''m having alot of troubles with this...so basically i am given a file for example:

File =

>123
qwerty
>567
tyuiyuy
>987
poiuyt

and basically the output is supposed to be [[">123" , "qwerty"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

i have no idea how to do that can someone help me please?.

推荐答案

我假设您知道基本文件I / O?


基本上,您需要做的是创建一个空列表(让我们称之为根) ,用 open()打开你想要的文件(让我们称之为''文件''),然后有一个循环,你可以使用 file.readline一次获得两行()(将它们存储在两个单独的变量中,例如'a''和'b'')。然后你需要做 root.append([a,b]),其中 [a,b] 最终将是 ["> 123" ,你的例子中的qwerty] 。当没有更多行要读取时,停止循环,然后你就有了你的列表( root )。


这有什么意义吗?
I''m assuming you know basic file I/O?

Basically, what you need to do is create an empty list (let''s call it ''root''), open the file you want with open() (let''s call this ''file''), then have a loop where you get two lines at a time using file.readline() (store these in two separate variables, ''a'' and ''b'' for example). Then you need to do root.append([a, b]), where [a, b] will end up being [">123" , "qwerty"] from your example. When there are no more lines to read, stop the loop, and there you have your list (root).

Does that make any sense?


@musicfreak


这是非常有意义的,我完全理解你想说什么,除了我忘了说文件可以也是


file =


> 123

qwerty

ioweio

> 567

tyuiyuy

> 987

poiuyt


output = [["> 123" ,qwertyioweio,["> 567" ,tyuiuy,[" 987" ,>>>> 123的输出是qwertyioweio。因为为了简单起见,我让文件看起来很简单,所以基本上是在qwerty之后。它走到了一条新线路,因为它已经满了。我希望有道理......


还有一种方法可以使用for循环来解决这个问题吗?
@musicfreak
yep that makes a lot of sense, and i understand fully what your trying to say except i forgot to say that the file could also be

file =

>123
qwerty
ioweio
>567
tyuiyuy
>987
poiuyt

output = [[">123" , "qwertyioweio"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

the output for >123 is "qwertyioweio" because for simplicity''s sake i made the file look that way to look simple, so basically after "qwerty" it went to a new line because it was full. i hope that made sense....

also is there a way to use a for loop for this question?


哦,我明白了。那么在这种情况下,你需要为每个子列表制作一个临时列表。如果该行以''>''开头,则将该行作为列表的第一部分,反之亦然。然后将该列表附加到''root''。


可以使用for循环,尽管不容易。这是作业吗?如果你必须使用for循环,那么你可以将所有文件一次性读入一个字符串,然后用新行读取 split(),然后遍历每一行(表示行中的行:,或类似的东西)。


编辑:实际上,现在我想起来,我刚才提到的方式可能会更简单哈哈。
Oh I see. Well in that case, you''re going to need to make a temporary list for each "sub list," and if the line starts with ''>'', make that line the first part of the list, and vice versa. Then just append that list to the ''root''.

You could use a for loop, although it would be easier not to. Is this for an assignment? If you have to use a for loop, then you could read the file all at once into a string and split() it by newlines, and then loop through each line (for line in lines:, or something like that).

Actually, now that I think about it, the way I just mentioned would probably be simpler haha.


这篇关于文件到嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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