Create one long string from multiple lists of strings
Question:
I am writing this back-end code to filter through a dataframe using df.query(). I have two lists from which I need to make this new string which will be passed as the query. Can anyone help me out?
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
#expected output
new_string = "Product == "Dress" | Product == "Shoes" & City == "Mumbai""
This is my code:
res = ' '.join([' '.join(x + i for i in list2) for x in list1])
print(res)
And this is the output I am getting which is wrong:
Answers:
This should do the trick:
' '.join(value for pair in zip(list1, list2 + ['']) for value in pair)[:-1]
How about creating a list of the relevant products, and using a query that check whether or not the value appear in the list, and also requiring the city to be Mumbai?
It’s cleaner and the code is easier to maintain
Filtering script:
releavnt_product = ['Dress', 'Shoes']
df.query('(Product in @releavnt_product) and (City == "Mumbai")')
Here an example:
test_data = [
{'Product': 'product1', 'City': 'city1'},
{'Product': 'product2', 'City': 'city2'},
{'Product': 'Dress', 'City': 'city3'},
{'Product': 'Dress', 'City': 'Mumbai'},
{'Product': 'product3', 'City': 'city2'},
{'Product': 'Shoes', 'City': 'city2'},
{'Product': 'Shoes', 'City': 'Mumbai'},
]
df = pd.DataFrame(test_data)
df
Output:
Product City
0 product1 city1
1 product2 city2
2 Dress city3
3 Dress Mumbai
4 product3 city2
5 Shoes city2
6 Shoes Mumbai
Filtering script:
releavnt_product = ['Dress', 'Shoes']
df.query('(Product in @releavnt_product) and (City == "Mumbai")')
Output:
Product City
3 Dress Mumbai
6 Shoes Mumbai
With the requirement that len(list1) == len(list2) + 1
then you can have a function to combine them and then join
def combine(lst1, lst2):
it = iter(lst1)
yield next(it)
for item in lst2:
yield item
yield next(it)
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
res = ' '.join(combine(list1, list2))
Here’s a variation on the answer from @rudolfovic that doesn’t involve removing redundant characters.
The assumption here is that list1 is of length N and that of list2 is N-1 (as stated by OP) and N > 0
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
new_list = list1[0] + ''.join(f' {a} {b}' for a, b in zip(list2, list1[1:]))
print(new_list)
Output:
Product == "Dress" | Product == "Shoes" & City == "Mumbai"
I am writing this back-end code to filter through a dataframe using df.query(). I have two lists from which I need to make this new string which will be passed as the query. Can anyone help me out?
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
#expected output
new_string = "Product == "Dress" | Product == "Shoes" & City == "Mumbai""
This is my code:
res = ' '.join([' '.join(x + i for i in list2) for x in list1])
print(res)
And this is the output I am getting which is wrong:
This should do the trick:
' '.join(value for pair in zip(list1, list2 + ['']) for value in pair)[:-1]
How about creating a list of the relevant products, and using a query that check whether or not the value appear in the list, and also requiring the city to be Mumbai?
It’s cleaner and the code is easier to maintain
Filtering script:
releavnt_product = ['Dress', 'Shoes']
df.query('(Product in @releavnt_product) and (City == "Mumbai")')
Here an example:
test_data = [
{'Product': 'product1', 'City': 'city1'},
{'Product': 'product2', 'City': 'city2'},
{'Product': 'Dress', 'City': 'city3'},
{'Product': 'Dress', 'City': 'Mumbai'},
{'Product': 'product3', 'City': 'city2'},
{'Product': 'Shoes', 'City': 'city2'},
{'Product': 'Shoes', 'City': 'Mumbai'},
]
df = pd.DataFrame(test_data)
df
Output:
Product City
0 product1 city1
1 product2 city2
2 Dress city3
3 Dress Mumbai
4 product3 city2
5 Shoes city2
6 Shoes Mumbai
Filtering script:
releavnt_product = ['Dress', 'Shoes']
df.query('(Product in @releavnt_product) and (City == "Mumbai")')
Output:
Product City
3 Dress Mumbai
6 Shoes Mumbai
With the requirement that len(list1) == len(list2) + 1
then you can have a function to combine them and then join
def combine(lst1, lst2):
it = iter(lst1)
yield next(it)
for item in lst2:
yield item
yield next(it)
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
res = ' '.join(combine(list1, list2))
Here’s a variation on the answer from @rudolfovic that doesn’t involve removing redundant characters.
The assumption here is that list1 is of length N and that of list2 is N-1 (as stated by OP) and N > 0
list1 = ['Product == "Dress"', 'Product == "Shoes"', 'City == "Mumbai"']
list2 = ['|', '&']
new_list = list1[0] + ''.join(f' {a} {b}' for a, b in zip(list2, list1[1:]))
print(new_list)
Output:
Product == "Dress" | Product == "Shoes" & City == "Mumbai"