Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have the following input:

newValue = [['men', 'John Doe,Chris perry'], ['women', 'Mary,Anne,Lita']] - list within list

keywords_list = [('MEN', 'john,zack')] - tuple in a list (Dictionary on which .items() is used.)

Expected Output is:

newValue = [['men', 'John Doe'],['women','']]

What I want to do here is extract those names for Men which is common in both newValue and keywords_list.

Below is my working implementation that gets the desired result:

      allValues = []
      for item in newValue:
            for key in keywords_list:
                if str(item[0])[1:].upper() == key[0]:
                    ValueSplit = item[1].split(',')
                    keyWordSplit = key[1].split(',')
                    for EverySplit in ValueSplit:
                        for everyKey in keyWordSplit:
                            if everyKey.lower() in EverySplit.lower():
                                pass
                            else:
                                ValueSplit.remove(EverySplit)
                    item[1]= ValueSplit
      allValues.append(item)

This isn't an efficient way of solving the problem. If anyone could recommend an efficient way with lesser complexity to achieve the desired result, it would be really helpful.

share|improve this question

migrated from stackoverflow.com Mar 18 at 14:52

This question came from our site for professional and enthusiast programmers.

1 Answer 1

both your datastructures can be cast into a dictionary directly:

>>> dict(newValue)
{'men': 'John Doe,Chris perry', 'women': 'Mary,Anne,Lita'}

>>> dict(keywords_list)
{'MEN': 'john,zack'}

from there you can just do a split based on , and compare in lowercase.

share|improve this answer
    
Is it possible to do something like the below to achieve this? [keyE [key.lower() for key in keywords_list] for keyE in extractedValue if keyE.lower() == key.lower()] –  Dork Mar 18 at 6:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.