[英]Python: Find X to Y in a list of strings
I have a list of maybe a 100 or so elements that is actually an email with each line as an element. 我有大约100个左右元素的列表,实际上是一封电子邮件,每行都是一个元素。 The list is slightly variable because lines that have a \\n in them are put in a separate element so I can't simply slice using fixed values.
该列表略有变化,因为其中包含\\ n的行被放在单独的元素中,因此我不能简单地使用固定值进行切片。 I essentially need a variable start and stop phrase (needs to be a partial search as well because one of my start phrases might actually be
Total Cost: $13.43
so I would just use Total Cost:
.) Same thing with the end phrase. 我本质上需要一个可变的开始和结束短语(也需要进行部分搜索,因为我的一个开始短语实际上可能是
Total Cost: $13.43
所以我只使用Total Cost:
。)End短语也是如此。 I also do not wish to include the start/stop phrases in the returned list. 我也不希望在返回列表中包含开始/停止短语。 In summary:
综上所述:
>>> email = ['apples','bananas','cats','dogs','elephants','fish','gee']
>>> start = 'ban'
>>> stop = 'ele'
# the magic here
>>> print new_email
['cats', 'dogs']
NOTES 笔记
SOLUTION 解
Just for funzies and thanks to everybody's help here is my final code: 只是为了玩笑,感谢大家的帮助,这是我的最终代码:
def get_elements_positions(stringList=list(), startPhrase=None, stopPhrase=None):
elementPositionStart, elementPositionStop = 0, -1
if startPhrase:
elementPositionStart = next((i for i, j in enumerate(stringList) if j.startswith(startPhrase)), 0)
if stopPhrase:
elementPositionStop = next((i for i, j in enumerate(stringList) if j.startswith(stopPhrase)), -1)
if elementPositionStart + 1 == elementPositionStop - 1:
return elementPositionStart + 1
else:
return [elementPositionStart, elementPositionStop]
It returns a list with the starting and ending element position and defaults to 0 and -1 if the respective value cannot be found. 它返回一个列表,其中包含元素的开始和结束位置,如果找不到相应的值,则默认为0和-1。 (0 being the first element and -1 being the last).
(0是第一个元素,-1是最后一个元素)。
SOLUTION-B 解决方案-B
I made a small change, now if the list is describing a start and stop position resulting in just 1 element between it returns that elements position as an integer instead of a list which you still get for multi-line returns. 我做了一个小小的更改,现在,如果列表描述的是开始位置和停止位置,导致列表之间只有1个元素,则该元素位置将以整数形式返回,而不是仍然为多行返回而得到的列表。
Thanks again! 再次感谢!
>>> email = ['apples','bananas','cats','dogs','elephants','fish','gee']
>>> start, stop = 'ban', 'ele'
>>> ind_s = next(i for i, j in enumerate(email) if j.startswith(start))
>>> ind_e = next(i for i, j in enumerate(email) if j.startswith(stop) and i > ind_s)
>>> email[ind_s+1:ind_e]
['cats', 'dogs']
To satisfy conditions when element might not be in the list: 要满足元素可能不在列表中的条件:
>>> def get_ind(prefix, prev=-1):
it = (i for i, j in enumerate(email) if i > prev and j.startswith(prefix))
return next(it, None)
>>> start = get_ind('ban')
>>> start = -1 if start is None else start
>>> stop = get_ind('ele', start)
>>> email[start+1:stop]
['cats', 'dogs']
An itertools
based approach: 基于
itertools
的方法:
import itertools
email = ['apples','bananas','cats','dogs','elephants','fish','gee']
start, stop = 'ban', 'ele'
findstart = itertools.dropwhile(lambda item: not item.startswith(start), email)
findstop = itertools.takewhile(lambda item: not item.startswith(stop), findstart)
print list(findstop)[1:]
// ['cats', 'dogs']
Here you go: 干得好:
>>> email = ['apples','bananas','cats','dogs','elephants','fish','gee']
>>> start = 'ban'
>>> stop = 'ele'
>>> out = []
>>> appending = False
>>> for item in email:
... if appending:
... if stop in item:
... out.append(item)
... break
... else:
... out.append(item)
... elif start in item:
... out.append(item)
... appending = True
...
>>> out.pop(0)
'bananas'
>>> out.pop()
'elephants'
>>> print out
['cats', 'dogs']
I think my version is much more readable than the other answers and doesn't require any imports =) 我认为我的版本比其他答案更具可读性,不需要任何导入=)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.