列表操作 [英] list manipulation

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

问题描述

你好,


我在列表操作方面遇到了一些麻烦,希望有人可以帮助我。


我没问题使用filename.readlines()在文本文件中读取列表。但是,我在搜索此列表时遇到了一些问题,无法显示我想要的元素。


我有一个这种通用格式的文本文件:


***************

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本
***************

文本文本文本文本

文本文本文本文本

***************

文本文本文本文本

文本文本文本文本

text文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文字

***************


等等我正在努力做的事情是:

1)将星号之间的文本部分组合在一起,以便制作单独的记录(在某种意义上......)

2 )如果我想在前面提到的''记录'中搜索某个单词,我想显示搜索单词所在的整个''记录''(并且这个搜索rd可以在1个或多个''记录'内')


无论我尝试什么循环组合,我都不成功。文件的第一行打印出来,或者我的代码根本无法打印出来。


请帮忙!

Hi there,

I am having some trouble with list manipulation and was hoping someone could help me.

I have no problem reading in a text file as a list using filename.readlines(). However, I am having some trouble with searching through this list to display the elements that I want.

I have a text file with this general format:

***************
text text text text
text text text text
text text text text
text text text text
***************
text text text text
text text text text
***************
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
***************

etc. and what I''m having trouble trying to do is:
1) group together the text sections which are in between the astericks, so as to make separate ''records'' (in a sense...)
2) if I want to search for a certain word within aforementioned ''records'', I want to display the entire ''record'' in which the search word is inside of (and this search word can be inside 1 or more ''records'')

No matter what combination of loops I try, I am unsuccessful. Either the first line of the file prints out for ever, or my code isn''t able to print anything out at all.

Please help!

推荐答案


你好,


我在列表操作方面遇到了一些麻烦,希望有人可以提供帮助我。


使用filename.readlines()将文本文件作为列表读取没有问题。但是,我在搜索此列表时遇到了一些问题,无法显示我想要的元素。


我有一个这种通用格式的文本文件:


***************

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本
***************

文本文本文本文本

文本文本文本文本

***************

文本文本文本文本

文本文本文本文本

text文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文字

***************


等等我正在努力做的事情是:

1)将星号之间的文本部分组合在一起,以便制作单独的记录(在某种意义上......)

2 )如果我想在前面提到的''记录'中搜索某个单词,我想显示搜索单词所在的整个''记录''(并且这个搜索rd可以在1个或多个''记录'内')


无论我尝试什么循环组合,我都不成功。文件的第一行打印出来,或者我的代码根本无法打印出任何内容。


请帮忙!
Hi there,

I am having some trouble with list manipulation and was hoping someone could help me.

I have no problem reading in a text file as a list using filename.readlines(). However, I am having some trouble with searching through this list to display the elements that I want.

I have a text file with this general format:

***************
text text text text
text text text text
text text text text
text text text text
***************
text text text text
text text text text
***************
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
***************

etc. and what I''m having trouble trying to do is:
1) group together the text sections which are in between the astericks, so as to make separate ''records'' (in a sense...)
2) if I want to search for a certain word within aforementioned ''records'', I want to display the entire ''record'' in which the search word is inside of (and this search word can be inside 1 or more ''records'')

No matter what combination of loops I try, I am unsuccessful. Either the first line of the file prints out for ever, or my code isn''t able to print anything out at all.

Please help!



这段代码将帮助您入门:

This piece of code will get you started:

展开 | 选择 | Wrap | 行号


使用此您提供的示例输入:说您要搜索word
using this sample input you provided: say you want to search for "word"
展开 | 选择 | Wrap | 行号



你好,


我在列表操作方面遇到了一些麻烦,希望有人可以帮助我。


使用filename.readlines()将文本文件作为列表读取没有问题。但是,我在搜索此列表时遇到了一些问题,无法显示我想要的元素。


我有一个这种通用格式的文本文件:


***************

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本
***************

文本文本文本文本

文本文本文本文本

***************

文本文本文本文本

文本文本文本文本

text文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文本

文本文本文本文字

***************


等等我正在努力做的事情是:

1)将星号之间的文本部分组合在一起,以便制作单独的记录(在某种意义上......)

2 )如果我想在前面提到的''记录'中搜索某个单词,我想显示搜索单词所在的整个''记录''(并且这个搜索rd可以在1个或多个''记录'内')


无论我尝试什么循环组合,我都不成功。文件的第一行打印出来,或者我的代码根本无法打印出任何内容。


请帮忙!
Hi there,

I am having some trouble with list manipulation and was hoping someone could help me.

I have no problem reading in a text file as a list using filename.readlines(). However, I am having some trouble with searching through this list to display the elements that I want.

I have a text file with this general format:

***************
text text text text
text text text text
text text text text
text text text text
***************
text text text text
text text text text
***************
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
text text text text
***************

etc. and what I''m having trouble trying to do is:
1) group together the text sections which are in between the astericks, so as to make separate ''records'' (in a sense...)
2) if I want to search for a certain word within aforementioned ''records'', I want to display the entire ''record'' in which the search word is inside of (and this search word can be inside 1 or more ''records'')

No matter what combination of loops I try, I am unsuccessful. Either the first line of the file prints out for ever, or my code isn''t able to print anything out at all.

Please help!



这肯定是正则表达式的工作。常规的exressions(Python上的re模块)是一个非常缺乏利用的工具。这可能是由于它们古怪的语法和简洁性。我现在还没有技能帮助,但我很快就会立刻撕掉我的 Masering Regular Expressions 的新副本。有一些网站教程可以帮助你入门。


基本上,你写了一个表达式,说给我***和***之间的所有文本也有搜索了单词,然后re模块返回所有文本。 (至少)试验是非常值得的。玩得开心!

This is certainly a job for regular expressions. Regular exressions (the re module on Python) are a very under utilized tool. This is probably due to their quirky syntax and terse nature. I don''t have the skills (yet) to help at this moment, but I an exited to tear into my new copy of Masering Regular Expressions very soon. There are some web site tutorials to get you started.

Basically, you write an expression that says "give me all the text between *** and *** that also has the searched for word(s), and the re module returns all that text. It is very worthwhile to (at least) experiment with. Have fun!


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

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