根据前面的元素从列表中分离元素 [英] Separating elements from a list according to preceding element

查看:72
本文介绍了根据前面的元素从列表中分离元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这方面有点麻烦所以任何帮助都会感激不尽

收到了......


拆分后的网址我有一个形式的字符串

''tag1 + tag2 + tag3-tag4''或''-tag1-tag2''等。第一个标签只有

如果操作符是' - '',则前面是操作符,如果前面没有任何操作符,则假定为
''+''。


使用re.split,我可以生成一个看起来如此的列表:

[''tag1'',''+'',''tag2'',''+'',' 'tag3'','' - '','''tag4'']


我希望得到两个列表 - 每个列表都包含标签

包含或标记要排除。我的想法是采用一个元素,

检查它前面的元素,并相应地将其插入

相关列表中。但是,我没有成功。


有没有更好的方法我没考虑过?如果这种方法适合我,我该如何实施呢?


谢谢大家,


Rob Cowie

I''m having a bit of trouble with this so any help would be gratefully
recieved...

After splitting up a url I have a string of the form
''tag1+tag2+tag3-tag4'', or ''-tag1-tag2'' etc. The first tag will only be
preceeded by an operator if it is a ''-'', if it is preceded by nothing,
''+'' is to be assumed.

Using re.split, I can generate a list that looks thus:
[''tag1'', ''+'', ''tag2'', ''+'', ''tag3'', ''-'', ''tag4'']

I wish to derive two lists - each containing either tags to be
included, or tags to be excluded. My idea was to take an element,
examine what element precedes it and accordingly, insert it into the
relevant list. However, I have not been successful.

Is there a better way that I have not considered? If this method is
suitable, how might I implement it?

Thanks all,

Rob Cowie

推荐答案



Rob Cowie写道:

Rob Cowie wrote:
我有一个这有点麻烦所以任何帮助都会感激地收到...

分裂后我有一个形式的字符串
''tag1 + tag2 + tag3 -tag4''或'' - tag1-tag2''等。如果第一个标签是' - '',那么第一个标签只会在操作符前面,如果它前面没有任何内容,''+''是假定的。

使用re.split,我可以生成一个看起来如此的列表:
[''tag1'',''+'', ''tag2'',''+'',''tag3'','' - '',''tag4'']

我希望派生出两个列表 - 每个列表都包含一个标签包括在内,或标签被排除在外。我的想法是采用一个元素,检查它之前的元素,并相应地将其插入
相关列表。但是,我没有成功。

有没有更好的方法,我没有考虑?如果这个方法合适,我该如何实现呢?

谢谢大家,

Rob Cowie
I''m having a bit of trouble with this so any help would be gratefully
recieved...

After splitting up a url I have a string of the form
''tag1+tag2+tag3-tag4'', or ''-tag1-tag2'' etc. The first tag will only be
preceeded by an operator if it is a ''-'', if it is preceded by nothing,
''+'' is to be assumed.

Using re.split, I can generate a list that looks thus:
[''tag1'', ''+'', ''tag2'', ''+'', ''tag3'', ''-'', ''tag4'']

I wish to derive two lists - each containing either tags to be
included, or tags to be excluded. My idea was to take an element,
examine what element precedes it and accordingly, insert it into the
relevant list. However, I have not been successful.

Is there a better way that I have not considered? If this method is
suitable, how might I implement it?

Thanks all,

Rob Cowie




a = [''+'',''tag1'',''+'',''tag2'','' - '',''tag3'',''+'',''tag4 '']


导入itertools


b = list(itertools.islice(a,0,8,2))

c = list(itertools.islice(a,1,8,2))

result1 = [x [1] for x in itertools.izip(b,c)if x [0] ==''+'']

result2 = [x [1] for x in itertools.izip(b,c)if x [0] =='' - '' ]


打印

打印结果1

打印结果2

Gerard


Rob Cowie写道:
Rob Cowie wrote:
我希望得到两个列表 - 每个列表包含要包含的标签或要排除的标签。我的想法是采用一个元素,检查它之前的元素,并相应地将其插入
相关列表。但是,我没有成功。

有没有更好的方法,我没有考虑?


也许。你可以写几个正则表达式,一个用于查找包含的

标签,一个用于排除,然后在它们上运行re.findall。


但是你的方法没有根本的错误。

如果这个方法合适,我该如何实现呢?
I wish to derive two lists - each containing either tags to be
included, or tags to be excluded. My idea was to take an element,
examine what element precedes it and accordingly, insert it into the
relevant list. However, I have not been successful.

Is there a better way that I have not considered?
Maybe. You could write a couple regexes, one to find the included
tags, and one for the excluded, then run re.findall on them both.

But there''s nothing fundamentally wrong with your method.
If this method is
suitable, how might I implement it?




tags = [''tag1'',''+'',''tag2'',''+'',''tag3'','' - '','''tag4'']


include,exclude = [],[]

op =''+''

for cur in tags:

如果是'+ - ''':

op = cur

else:

如果op ==''+' ':

include.append(cur)

else:

exclude.append(cur)


--Ben



tags = [''tag1'', ''+'', ''tag2'', ''+'', ''tag3'', ''-'', ''tag4'']

include, exclude = [], []
op = ''+''
for cur in tags:
if cur in ''+-'':
op = cur
else:
if op == ''+'':
include.append(cur)
else:
exclude.append(cur)

--Ben




Gerard Flanagan写道:

Gerard Flanagan wrote:
Rob Cowie写道:
Rob Cowie wrote:
我在这方面遇到了一些麻烦所以任何帮助都会感激不尽。 cieved ...

分割后我有一个形式的字符串
''tag1 + tag2 + tag3-tag4''或''-tag1-tag2''等。第一个标签只有在操作员之前才会是'' - '',如果之前没有任何内容,则应假定为'+''。

使用re.split,我可以生成一个看起来如此的列表:
[''tag1'',''+'',''tag2'',''+'',''tag3 '','' - '',''tag4'']

我希望得到两个列表 - 每个列表包含要包含的标签或要排除的标签。我的想法是采用一个元素,检查它之前的元素,并相应地将其插入
相关列表。但是,我没有成功。

有没有更好的方法,我没有考虑?如果这个方法合适,我该如何实现呢?

全部谢谢你,Rob Cowie
I''m having a bit of trouble with this so any help would be gratefully
recieved...

After splitting up a url I have a string of the form
''tag1+tag2+tag3-tag4'', or ''-tag1-tag2'' etc. The first tag will only be
preceeded by an operator if it is a ''-'', if it is preceded by nothing,
''+'' is to be assumed.

Using re.split, I can generate a list that looks thus:
[''tag1'', ''+'', ''tag2'', ''+'', ''tag3'', ''-'', ''tag4'']

I wish to derive two lists - each containing either tags to be
included, or tags to be excluded. My idea was to take an element,
examine what element precedes it and accordingly, insert it into the
relevant list. However, I have not been successful.

Is there a better way that I have not considered? If this method is
suitable, how might I implement it?

Thanks all,

Rob Cowie



= [''+'',''tag1'',''+'',''tag2'','' - '',''tag3'',''+'',''tag4'']

导入itertools

b = list(itertools.islice(a,0,8,2))
c = list(itertools.islice(a,1) ,8))

结果1 = [x [1] for x in itertools.izip(b,c)if x [0] ==''+'']
result2 = [x [1] for x in itertools.izip(b,c)if x [0] =='' - '']

打印
打印结果1
打印结果2

Gerard



a = [ ''+'', ''tag1'', ''+'', ''tag2'', ''-'', ''tag3'', ''+'', ''tag4'' ]

import itertools

b = list(itertools.islice(a,0,8,2))
c = list(itertools.islice(a,1,8,2))

result1 = [x[1] for x in itertools.izip(b,c) if x[0] == ''+'']
result2 = [x[1] for x in itertools.izip(b,c) if x[0] == ''-'']

print
print result1
print result2
Gerard




''8''是''''的长度(len(a))



''8'' is the length of ''a'' (len(a))


这篇关于根据前面的元素从列表中分离元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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