So we have a form that has 4 areas
When a user comes to our page - they can choose any number of cities,
Example:
On submission - user gets the number of people who fit those criteria
Knowing the lists we have (City Age Gender Nationality) I tried to create a python script that would give me every possible combination so we could create queries to seed the database with the counts of each
#import the library
from itertools import product
#create lists for cities, gender, age, and nationality
cities = ['Riyadh', 'Jeddah', 'Mecca', 'Danaam']
gender = ['male', 'female']
age = ['18-25', '26-40', '41-60', '65+']
nationality = ['Saudi', 'UK', 'Filipino', 'Egyptian', 'French']
#combinate the list to find the permutations
list = [cities, gender, age, nationality]
combinations = [p for p in product(*list)]
#output the list in list format
print(combinations)
#find length of the # of lists
print(len(combinations))
However that gives me the combos of iterations but not with multiple in each section
Example output looks like this
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 41-60 AND nationality= Egyptian
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 41-60 AND nationality= French
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 65+ AND nationality= Saudi
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 65+ AND nationality= UK
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 65+ AND nationality= Filipino
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 65+ AND nationality= Egyptian
Select CITY, GENDER, AGE, HERITAGE FROM NA_tablename WHERE city= Danaam AND geneder= female AND age= 65+ AND nationality= French
160
How can I permutate for every possible combintation since I can check multiple items from each list
I keep trying things with itertools but not finding good resources maybe based off my language
I presume this is what you want:
Firstly, we do combinations on each array(cities, gender, age, nationality) with different length. For instance, for gender
, the combination tmp will be [('male',), ('female',), ('male', 'female')]
. Then we perform product on those combinations. Thus, the final length will be:
final_length = sum(C^n_4)(n∈[1,4]) * sum(C^n_2)(n∈[1,2]) * sum(C^n_4)(n∈[1,4]) * sum(C^n_5)(n∈[1,5])
= sum(4+6+4+1) * sum(2+1) * sum(4+6+4+1) * sum(5+10+10+5+1)
= 20925
Here's the code:
from itertools import product, combinations
#create lists for cities, gender, age, and nationality
cities = ['Riyadh', 'Jeddah', 'Mecca', 'Danaam']
gender = ['male', 'female']
age = ['18-25', '26-40', '41-60', '65+']
nationality = ['Saudi', 'UK', 'Filipino', 'Egyptian', 'French']
tmp = []
for i in [cities, gender, age, nationality]:
temp = []
for j in range(1, len(i)+1):
temp.extend(list(combinations(i, j)))
tmp.append(temp)
print(tmp)
combinations = [p for p in product(*tmp)]
print(len(combinations))
print(combinations)