in Education by
I have a series of lists, call them A, B, C, D, E. Now every list has 5 elements with identical names, say: A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ] B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ] etc.. What I want is a list of lists, of the form: [ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'], ['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'], ['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'], ['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ] As can be seen, the rule for this list is: An element cannot be grouped with another element from the same set, for example ['Cars_A', 'Planes_A'] is not allowed. An element cannot be grouped with a similar element from a different set, for example ['Cars_A', 'Cars_B'] is not allowed. My attempt right now is to do nested for loops for all 5 lists, but I want to avoid this if possible. Any ideas? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Using itertools.permutations and itertools.product with filter: import itertools l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE'] l [['cars_A', 'planes_A', 'houses_A', 'bikes_A'], ['cars_B', 'planes_B', 'houses_B', 'bikes_B'], ['cars_C', 'planes_C', 'houses_C', 'bikes_C'], ['cars_D', 'planes_D', 'houses_D', 'bikes_D'], ['cars_E', 'planes_E', 'houses_E', 'bikes_E']] res = [] for sub in itertools.permutations(l, 2): res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub)))) res [('cars_A', 'planes_B'), ('cars_A', 'houses_B'), ('cars_A', 'bikes_B'), ('planes_A', 'cars_B'), ('planes_A', 'houses_B'), ('planes_A', 'bikes_B'), ('houses_A', 'cars_B'), ... ('bikes_E', 'cars_D'), ('bikes_E', 'planes_D'), ('bikes_E', 'houses_D')]

Related questions

0 votes
    I'm creating my first program on python. The objective is to get an output of trip cost. In the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a legit situation like this where I need to implement a method that changes the incoming object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I am using the Google Sheets V4 Values collection and I am having trouble figuring out how to get each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a vert.x cluster on my local machine. There are two verticles public class AccountVerticle extends ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am looking to write a pop-up window which asks the user to select a specific option, and if ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Is there a way to to disable intellisense in ax2012's code editor? sometimes when writing selecting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I was developing one web application in cakephp 2.2.3. that application I was using CakeEmail. But Now ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
...