gpt4 book ai didi

python - 如何使用反向分割线?

转载 作者:行者123 更新时间:2023-11-28 21:36:15 24 4
gpt4 key购买 nike

我想解析变量output并打印最新的标签(按时间),目前我正在打印最旧的标签,有没有办法使用reverse分割线?

import re
output="""Fri Apr 27 18:43:20 2018 username3 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="572224322539363a32641734383a2736392e7934383a" rel="noreferrer noopener nofollow">[email protected]</a>> submitted CoreCapture-274.1 to release1
Fri Apr 27 18:43:20 2018 username3 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="493c3a2c3b2728242c7a092a262439282730672a2624" rel="noreferrer noopener nofollow">[email protected]</a>> forwarded CoreCapture-274.1 to release2, release2E, release1E, release3, and release3E
Fri May 18 20:09:22 2018 username2 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8b8d9b8c909f939bccbe9d91938e9f9087d09d9193" rel="noreferrer noopener nofollow">[email protected]</a>> submitted CoreCapture-274.2 to release1
Fri May 18 20:09:22 2018 username2 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b3b5a3b4a8a7aba3f486a5a9abb6a7a8bfe8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>> forwarded CoreCapture-274.2 to release2, release2E, release1E, release3, and release3E
Fri Jun 8 13:35:44 2018 username1 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01747264736f606c643041626e6c71606f782f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>> submitted CoreCapture-274.3 to release1
Fri Jun 8 13:35:44 2018 username1 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9feaecfaedf1fef2faaedffcf0f2effef1e6b1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>> forwarded CoreCapture-274.3 to release2, release2E, release1E, release3, and release3E"""

train = "release2"
for line in output.splitlines():
#print "line"
#print line
if re.match(r'.*(submitted|forwarded)(.*) to .*%s.*'%train,line):
print line
break

输出:-

CoreCapture-274.1

预期输出:-

CoreCapture-274.3

最佳答案

splitlines 调用包装在 reversed 中,它将以相反的顺序迭代结果行(无需额外的临时 列表):

for line in reversed(output.splitlines()):

或者,如果您需要相反顺序的列表本身,您可以将其分成两行:

lines = output.splitlines()
lines.reverse() # Reverses in place

或者用一个反向切片将其单行(以额外的临时列表为代价):

lines = output.splitlines()[::-1]  # Returns reversed list

关于python - 如何使用反向分割线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51334327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com