简体   繁体   English

Python:在字符串列表中找到X到Y

[英]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 笔记

  • While not perfect formatting of the email, it is fairly consistent so there is a slim chance a start/stop phrase will occur more than once. 尽管电子邮件的格式不完美,但它是相当一致的,因此,启动/停止短语出现的机会很小。
  • There are also no blank elements. 也没有空白元素。

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.

相关问题 在字符串列表中,找到字符串中的短语,并将字符串中的两个整数(x..y)追加到list。 蟒蛇 - In a list of strings, find a phrase within the string and append two integers (x..y) in string to a list . Python 在python中的成对列表中查找最小x和y值的最佳方法? - Best way to find minimum x and y values in a list of pairs in python? 使用 python 在二维列表中搜索以查找 x,y 位置 - Search in 2D list using python to find x,y position Python:将列表理解列入x,y列表 - Python: List comprehension into an x,y list Python列表索引,列表[[x] [y]:-z] - Python list Indexing, List[ [x][y]:-z ] Python - 在[x,y]列表中传递“x”“y”次 - Python - print “x” “y” times when passed in a list of [x,y] python-在字符串列表中,查找至少出现在y个条目中的至少具有n个连续标记的所有模式 - python - in a list of strings, find all patterns with a minimum of n consecutive tokens that occurs in at least y entries 在python数组中查找值的X和Y坐标 - Find X and Y Coordinates of a value in a python array 在列表中查找字符串位置-Python - Find a strings location in a list - Python 在X轴上查找对应于Y的任意值的值,该值不包含在用于在python中绘制数据的列表中 - To find the value on X axis corresponding to an arbitrary value of Y that doesn't contain in the list used to plot the data in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM