0

I have a huge list of strings where a couple of strings only differ in 2 or three characters like this:

ENSH-DFFEV1-5F
ENSH-DFFEV2-5F
ENSH-DFFEV3-5F
FVB.DFFVRV2-4T
FVB.DFFVRV3-4T

What I would like to do is to keep only those elements for which the number after the 'V' is the largest. From the above example I would like to have

ENSH-DFFEV3-5F
FVB.DFFVRV3-4T

Is there a simple way to do this in Python?

5
  • 4
    SO is not a code writing service. We help with specific code problems. Please read through the help centre. Commented Jun 29, 2017 at 18:46
  • Are these elements in the certain list? Or they are located in file? Commented Jun 29, 2017 at 18:51
  • are they always grouped together like in your example? Commented Jun 29, 2017 at 18:53
  • also, if they are grouped, are they always increasing the number after V like in your example? Commented Jun 29, 2017 at 18:55
  • @jacoblaw Yes, they are always grouped together like this, increasing number after the V Commented Jun 29, 2017 at 19:33

1 Answer 1

1

@stevieb is right, but anyway, I did the effort for you.

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.