-1

I have a list how do a assigned it to condition1[] what is the optimal way to scale up if I want to have more search criteria. As my steps below does not produce consistent result.

list_a = ["jywREDhgg", "REdghfg", "fdsfhgReD", "dsadBLuE"]

condition1Keyword = ["Red", "REd", "ReD"]
condition1 = [con1 for con1 in image if any(Con1 in con1 for Con1 in condition1Keyword)] 
condition2Keyword = ["BLuE"]
condition2 = [con2 for con2 in image if any(Con2 in con2 for Con2 in condition2Keyword)] 
  • What is your sort criteria? What do you expect as the output for the given input? Is case important? (i.e. `blue` vs `Blue` vs `BLuE`) – MatsLindh Jan 22 '21 at 08:56
  • I know that python is case sensitive. To work it around I only know how to match it according to the case sensitivity letters in the ```list_a```. But what I want to achieve is that as long as the the items in the list contains a key word like Red, it will be assigned to condition1 list. – KelvinWorks Jan 22 '21 at 09:03
  • 1
    So, what do you want exactly? [python - How do I do a case-insensitive string comparison? - Stack Overflow](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) ? Or something else? (explain it in the question) – user202729 Jan 22 '21 at 09:06
  • Yes; but I'm asking _what you want_ to do. Unless your intentions are clear, it's very hard to say that "you can do it this way or that way"; it'll be a guessing game. Add proper examples to your question about what you want your output to be given specific inputs and why that output is what it is. Since you're also talking about sorting - which have a specific meaning - it'll be helpful with examples. – MatsLindh Jan 22 '21 at 09:32

1 Answers1

0

Are you looking to do this?:

list_a = ["jywREDhgg", "REdghfg", "fdsfhgReD", "dsadBLuE"]

cond1 = [w for w in list_a if 'red' in w.lower()]
cond2 = [w for w in list_a if 'blue' in w.lower()]

print('cond1:', cond1)
print('cond2:', cond2)

Result:

cond1: ['jywREDhgg', 'REdghfg', 'fdsfhgReD']
cond2: ['dsadBLuE']

or to generalize it to any keyword list:

conditions = {}

for cond in ['red', 'blue', 'gg']:
    conditions[cond] = [w for w in list_a if cond in w.lower()]

print(conditions)

Result:

{'red': ['jywREDhgg', 'REdghfg', 'fdsfhgReD'], 'blue': ['dsadBLuE'], 'gg': ['jywREDhgg']}
CryptoFool
  • 21,719
  • 5
  • 26
  • 44