说我有这样的文字:
www.something.com
get the 2 above
www.somethingss.com
get the 2 above
www.somethingssss.com
get the 2 above
也许是这样的:
for a in text:
if 'get the 2 above' in a:
get 2 above
我该怎么做?
答案 0 :(得分:3)
假设上面是一个字符串:
s = '''www.something.com
get the 2 above
www.somethingss.com
get the 2 above
www.somethingssss.com
get the 2 above'''
s = s.splitlines()
for i, line in enumerate(s):
if 'get the 2 above' in line:
print s[i-1], s[i-2]
s.splitlines()
删除换行符;如果你想让他们离开,请通过True
,如同s.splitlines(True)
一样(感谢Sven Marnach)。
要从文件中获取行列表,您可以这样做:
with open('myfile.txt', 'r') as f:
s = f.readlines()
答案 1 :(得分:3)
您可以使用itertools配方迭代迭代上的固定长度“窗口”:
import itertools
def windows(iterable, length=2):
# If iterable is a list, this is equivalent to
# (iterable[i:i+length] for i in range(len(iterable)-length+1))
return itertools.izip(*(itertools.islice(it,n,None)
for n,it in enumerate(itertools.tee(iterable,length))))
text='''\
www.something.com
get the 2 above
www.somethingss.com
get the 2 above
www.somethingssss.com
get the 2 above
'''.splitlines()
for lines in windows(text,3):
if lines[2]=='get the 2 above':
print(lines[0])
# www.something.com
# www.somethingss.com
# www.somethingssss.com
答案 2 :(得分:2)
轻松入侵:反转列表。然后你按照正确的顺序进行迭代,这很容易:
lines = iter(reversed(s.splitlines()))
for line in lines:
if <whatever>:
next(lines)
yield next(lines)
(编辑:固定脑屁,谢谢@Sven)
答案 3 :(得分:0)
text = '''www.something0.com
get the 2 above
www.somethingss1.com
get the 2 above
www.somethingssss2.com
get the 2 above
www.somethingss3.com
get the 2 above
www.somethingss4.com
www.somethingss5.com
get the 2 above'''
from collections import deque
above = deque(maxlen=2)
for x in text.splitlines():
if 'get the 2 above' in x:
print above
elif x:
above.append(x)
print '\n=================\n'
# Senderle's code:
s = text.splitlines()
for i, line in enumerate(s):
if 'get the 2 above' in line:
print s[i-1], s[i-2]
结果
deque(['www.something0.com'], maxlen=2)
deque(['www.something0.com', 'www.somethingss1.com'], maxlen=2)
deque(['www.somethingss1.com', 'www.somethingssss2.com'], maxlen=2)
deque(['www.somethingssss2.com', 'www.somethingss3.com'], maxlen=2)
deque(['www.somethingss4.com', 'www.somethingss5.com'], maxlen=2)
=================
www.something0.com get the 2 above
www.somethingss1.com
www.somethingssss2.com
www.somethingss3.com
www.somethingss5.com