1

I want to get the elements in a list that match by substring while iterating over it. For instance, in following example list, i want to get "pass1", "pass2" matching elements and "pass3" as element that doesn't match. This is a simplified version of the list, as I need to iterate over the long list.

Please, let me know, what am I doing wrong in the following example.

keyList=["pass1_v1","pass1_v3","pass1_v5","pass2_v1","pass2_v3","pass3_v4"]
for x in keyList:
    match=x.rsplit("_",1)[0] ## splitting the list elements seperated by "_"..eg:- pass1 to check how many elements match
    if  match in keyList:
        print("matching are %s" %x) ## expecting to print eg:-pass1_v1 and pass1_v3
    else :
        print ("non matching are %s"%x)     # expecting to print pass3_v4
3
  • I'm not sure I understand. pass3 is also a substring in the list (the last element). Do you mean that it has to match other elements in the list? Commented Aug 24, 2014 at 0:31
  • Your code splits off pass1 in the first loop, in which case all the pass2 and pass3 elements would not match that prefix. Next two iterations the same would happen. Then a pass2 prefix is found, and it appears you want all pass1 and pass3 elements to be printed as 'not matching'; twice as there are two pass2 elements in the list. That's a lot of extra non matching prints.. Commented Aug 24, 2014 at 0:36
  • Hi ..Thanks for the quick reply...yup, it is the substring in the list and should also be checked as others...and the result should show "pass3_v4" as non matched, as pass3 isn't contained by other elements.. Commented Aug 24, 2014 at 0:39

1 Answer 1

1

You cannot use in membership testing with a substring; you'd have to loop over keyList again to test each substring.

It'd be far more efficient to grou all strings by prefix:

by_prefix = {}
for x in keyList:
    by_prefix.setdefault(x.rsplit('_', 1)[0], []).append(x)

for prefix, matches in by_prefix.iteritems():
    print 'Matches for {} are {}'.format(prefix, by_prefix[prefix])

This only prints the matching elements; all other keys are those that didn't match, but they'll be printed on their own:

>>> keyList = ["pass1_v1", "pass1_v3", "pass1_v5", "pass2_v1", "pass2_v3", "pass3_v4"]
>>> by_prefix = {}
>>> for x in keyList:
...     by_prefix.setdefault(x.rsplit('_', 1)[0], []).append(x)
... 
>>> for prefix, matches in by_prefix.iteritems():
...     print 'Matches for {} are {}'.format(prefix, by_prefix[prefix])
... 
Matches for pass2 are ['pass2_v1', 'pass2_v3']
Matches for pass1 are ['pass1_v1', 'pass1_v3', 'pass1_v5']
Matches for pass3 are ['pass3_v4']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.