分隔列表的最佳方法是什么? [英] Best way to delimit a list?

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

问题描述

嗨 - 我有一个从popen / readlines返回的列表,我想知道

如何迭代返回的每个项目(而不是

目前有整个批次都返回了。


到目前为止:


>> f = os.open(" ./ get_hostnames")。readlines



返回[''' host1 host2 host3 ... hostN \ n]''


我希望能够遍历这些,抓住每个

主办。我玩转换为str,并使用split,并且

这个有效,但是我从列表输出中得到了下标括号,因为列表输出为

现在是一个字符串文字,这不是我想要的b $ b,而且我觉得做搜索有点啰嗦''

替换它 - 因此我在主题中询问什么是最好的方式。


> ;> f = str(f)
f.split()



[" [''host1" ,host2,...,hostN \ n,]

非常感谢任何帮助


ta


dan。

解决方案

5月13日,5:28 * am,dannywebs ... @ googlemail。 com写道:


嗨 - 我有一个从popen / readlines返回的列表,我想知道如何

去迭代每个返回的项目(而不是

目前已经返回了整个批次)。


so远:


> f = os.open(" ./ get_hostnames")。readlines



返回[''host1 host2 host3 ... hostN \ n]''


我想要进入一个迭代这些的位置,抓住每个

主机。 *我已经玩转换为str,并使用split,并且

这个有效,但是我从列表输出中得到了下标括号,因为

是预期的,作为列表输出现在是一个字符串文字,这不是我想要的b $ b,而且我认为进行搜索有点啰嗦''

替换关于它 - 因此我在主题中询问什么是最好的方式。


> f = str(f)
f.split()



[" [''host1"," host2",...," hostN \ n'']"]


非常感谢任何帮助


ta


dan。



打开Goog​​le Ring。如果你只是摆动手指,它可能需要支付

来在家里找工作。我们在这里所要做的就是

安排一些事情。在谈话时做出决定很容易。我认为它可以很容易地集中全世界的时间并重新分配

的钱。如果我们所做的只是正常生活,那么这个受限制的,开放的市场将很容易建立起来。讨论2004年Microecon的b $ b价格是非法的。课程。


5月13日上午11点28分,dannywebs ... @ googlemail.com写道:


嗨 - 我有一个从popen / readlines返回的列表,我想知道

如何迭代返回的每个项目(而不是

目前有到目前为止,



> f = os。 open(&.; get_hostnames")。readlines



返回[''host1 host2 host3 ... hostN \ n]''


我希望能够遍历这些,抓住每个

主机。我玩转换为str,并使用split,并且

这个有效,但是我从列表输出中得到了下标括号,因为列表输出为

现在是一个字符串文字,这不是我想要的b $ b,而且我觉得做搜索有点啰嗦''

替换它 - 这就是为什么我在主题中询问什么是最好的方式。


> f = str(f)
f.split()



[" [''host1"," host2",...," hostN \\ \\ n'']"]


非常感谢任何帮助


ta


担。



我确实是指os.popen,没有os.open


En Tue,2008年5月13日07:28:03 -0300,< da ********** @googlemail.comescribió:


嗨 - 我有一个从popen / readlines返回的列表,我想知道

如何迭代返回的每个项目(而不是

目前拥有整个到目前为止$



>>> f = os.open(" ./ get_hostnames")。readlines



返回[''host1 host2 host3 ... hostN \ n]' '



你的意思是readlines(),我猜想。一个文件充当它自己的迭代器:


f = os.open(&.; ./ get_hostnames")

试试:

for line in f:

#用行做点什么

终于:

f.close()


-

Gabriel Genellina


Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:

>>f=os.open("./get_hostnames").readlines

returns [''host1 host2 host3 ... hostN\n]''

i''d like to be in a position to iterate through these, grabbing each
host. I have played with transmuting to a str, and using split, and
this works, but I get the subscript brackets from the list output as
expected, as the list output is now a string literal, and this is not
what I want - and I think it''s a bit long-winded to do a search ''n
replace on it - hence why I ask in the subject what''s the best way.

>>f=str(f)
f.split()

["[''host1","host2", ... ,"hostN\n'']"]
Any help is highly appreciated

ta

dan.

解决方案

On May 13, 5:28*am, dannywebs...@googlemail.com wrote:

Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:

>f=os.open("./get_hostnames").readlines


returns [''host1 host2 host3 ... hostN\n]''

i''d like to be in a position to iterate through these, grabbing each
host. *I have played with transmuting to a str, and using split, and
this works, but I get the subscript brackets from the list output as
expected, as the list output is now a string literal, and this is not
what I want - and I think it''s a bit long-winded to do a search ''n
replace on it - hence why I ask in the subject what''s the best way.

>f=str(f)
f.split()


["[''host1","host2", ... ,"hostN\n'']"]

Any help is highly appreciated

ta

dan.

Bring up the Google Ring. Where you only wiggle fingers, it might pay
to get jobs at home. All we up here would have to do would be
schedule something. Make a decision is easy in talking. I think it
would be easy to centralize the time the world''s at and redistribute
money. If all we''d do is normal life, this constrained, open markets
would be easy to set up. It''s just illegal to talk about pricing in
2004 Microecon. classes.


On May 13, 11:28 am, dannywebs...@googlemail.com wrote:

Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:

>f=os.open("./get_hostnames").readlines


returns [''host1 host2 host3 ... hostN\n]''

i''d like to be in a position to iterate through these, grabbing each
host. I have played with transmuting to a str, and using split, and
this works, but I get the subscript brackets from the list output as
expected, as the list output is now a string literal, and this is not
what I want - and I think it''s a bit long-winded to do a search ''n
replace on it - hence why I ask in the subject what''s the best way.

>f=str(f)
f.split()


["[''host1","host2", ... ,"hostN\n'']"]

Any help is highly appreciated

ta

dan.

I did indeed mean "os.popen", no "os.open"


En Tue, 13 May 2008 07:28:03 -0300, <da**********@googlemail.comescribió:

Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:

>>>f=os.open("./get_hostnames").readlines


returns [''host1 host2 host3 ... hostN\n]''

You meant readlines(), I presume. A file acts as its own iterator:

f=os.open("./get_hostnames")
try:
for line in f:
# do something with line
finally:
f.close()

--
Gabriel Genellina


这篇关于分隔列表的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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