[英]How to filter out list elements based on if they contain a substring from another list in Python
[英]Delete elements from list based on substring in Python
我有大量的字符串列表,其中幾個字符串的區別僅在於2個或3個字符,如下所示:
ENSH-DFFEV1-5F
ENSH-DFFEV2-5F
ENSH-DFFEV3-5F
FVB.DFFVRV2-4T
FVB.DFFVRV3-4T
我想做的只是保留那些在“ V”之后的數字最大的元素。 從上面的例子中我想
ENSH-DFFEV3-5F
FVB.DFFVRV3-4T
有沒有一種簡單的方法可以在Python中做到這一點?
@stevieb是正確的,但是無論如何,我為您做了努力。
s = """
ENSH-DFFEV1-5F
ENSH-DFFEV2-5F
ENSH-DFFEV3-5F
FVB.DFFVRV2-4T
FVB.DFFVRV3-4T
""".split()
def custom_filter(s):
out = []
current_max = -1
for r in s:
v = int(r.rsplit('-', 1)[0][-1]) # <- you should probably edit this line to fit your data structure
if v > current_max:
current_max = v
out = []
if v == current_max:
out += [r]
return out
for e in custom_filter(s):
print e
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.