0

I am using below python code to get unique combinations out of the list.

import itertools
unique_combinations = [] 
ss = [['0_20F','1_20F','2_20F','3_20F','4_20F','5_20F','6_20F','7_20F','8_20F','9_20F','10_20F'],
      ['0_40F','1_40F','2_40F','3_40F','4_40F','5_40F','6_40F','7_40F','8_40F', '9_40F','10_40F'],
      ['0_40HC','1_40HC','2_40HC','3_40HC','4_40HC','5_40HC','6_40HC','7_40HC','8_40HC', '9_40HC','10_40HC']]

for l in list(itertools.product(*ss)):
    unique_combinations.append(l)
    print(l)

The sample output is as follows.

('0_20F', '0_40F', '0_40HC')
('0_20F', '0_40F', '1_40HC')
('0_20F', '0_40F', '2_40HC')
('0_20F', '0_40F', '3_40HC')
('0_20F', '0_40F', '4_40HC')
('0_20F', '0_40F', '5_40HC')
('0_20F', '0_40F', '6_40HC')
('0_20F', '0_40F', '7_40HC')
('0_20F', '0_40F', '8_40HC')
('0_20F', '0_40F', '9_40HC')

I need to get this done using pyspark.Is it possible via pyspark?.

0

1 Answer 1

1

For each list in ss a new dataframe can be created. After that all dataframes can be cross joined:

dfs = [spark.createDataFrame([[s] for s in ssx], schema=[f"col_{i}"]) 
    for i, ssx in enumerate(ss)]

import functools
result = functools.reduce(lambda l,r: l.crossJoin(r), dfs )
result.show(5)

#+-----+-----+------+
#|col_0|col_1| col_2|
#+-----+-----+------+
#|0_20F|0_40F|0_40HC|
#|0_20F|0_40F|1_40HC|
#|0_20F|1_40F|0_40HC|
#|0_20F|1_40F|1_40HC|
#|1_20F|0_40F|0_40HC|
#+-----+-----+------+
#only showing top 5 rows
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.