I need some help with coding a specific logic mentioned below in python 3.6.
Say I have a list 'a'. Note this list is not of static length and the values can vary.
a= ['Theresa', 'Paul', 'lyndsay', 'Nick', 'Tim', 'Ray', 'Charles']
I would like to generate a list like below using these values:
(((((name equals "Theresa") or (name equals "Paul")) or ((name equals "lyndsay") or (name equals "Nick"))) or ((name equals "Tim") or (name equals "Ray"))) or (name equals "Charles"))
The logic for this is that if you notice the brackets, they always compare 2 conditions at a time.
the 1st set below: if you see it compares on 2 values.
((name equals "Theresa") or (name equals "Paul"))
similarly the 2nd set is:
((name equals "lyndsay") or (name equals "Nick"))
now that we have 2 sets, these can be combined and wrapped together with a brackets. This now becomes a single set that can be combined with another set. lets call it set 3
(((name equals "Theresa") or (name equals "Paul")) or ((name equals "lyndsay") or (name equals "Nick")))
moving on, the next set 4 will be:
((name equals "Tim") or (name equals "Ray"))
now set 3 and set 4 can be combined and wrapped together with a brackets. This is set 5:
((((name equals "Theresa") or (name equals "Paul")) or ((name equals "lyndsay") or (name equals "Nick"))) or ((name equals "Tim") or (name equals "Ray")))
Next we have only 1 value remaining, so that becomes our last set:
(name equals "Charles")
This can now be combined with set 5 to get the final string generated from list 'a':
(((((name equals "Theresa") or (name equals "Paul")) or ((name equals "lyndsay") or (name equals "Nick"))) or ((name equals "Tim") or (name equals "Ray"))) or (name equals "Charles"))
Similarly list 'b' should generate:
b= ['key1', 'key2', 'key3', 'key4','key5']
((((keyword equals "key1") or (keyword equals "key2")) or ((keyword equals "key3") or (keyword equals "key4"))) or (keyword equals "key5"))
The 2 from list 'a' and 'b' should be combined using an 'and' operator to give:
((((((name equals "Theresa") or (name equals "Paul")) or ((name equals "lyndsay") or (name equals "Nick"))) or ((name equals "Tim") or (name equals "Ray"))) or (name equals "Charles")) and ((((keyword equals "key1") or (keyword equals "key2")) or ((keyword equals "key3") or (keyword equals "key4"))) or (keyword equals "key5")))
What I have tried so far is I have been able to generate a list using the following code. But don't know how to proceed further:
from math import ceil
size = 2
seq = ['Theresa', 'Paul', 'lyndsay', 'Nick', 'Tim', 'Ray', 'Charles']
split_list = [
seq[i * size:(i * size) + size]
for i in range(ceil(len(seq) / size))
]
# print(split_list)
mid_list = []
qs = ""
for i, item in enumerate(split_list):
sub_q = ""
if i > 0:
sub_q = f'{qs} or '
if len(item) == 2:
sub_q = f'((name equals "{item[0]}") or (name equals "{item[1]}"))'
else:
sub_q = f'(name equals "{item[0]}"))'
mid_list.append([sub_q])
print(mid_list)
Output: [['((name equals "Theresa") or (name equals "Paul"))'], ['((name equals "lyndsay") or (name equals "Nick"))'], ['((name equals "Tim") or (name equals "Ray"))'], ['(name equals "Charles"))']]
Appreciate any help with this.
Thank you.
for i, item in enumerate(split_list), (2) is name a variable or just a string? (3) for the code you have provided what does the output look like, when split_list is defined as something?