Home > other >  Python array reorganization
Python array reorganization

Time:09-25

Have multiple arrays, such as [1, 2, 3] and [three, four, five], a number from each array, the formation of new array, how do algorithm implementation, to achieve all the combination!

CodePudding user response:

Don't know what you want from the array of the number of arbitrary number, is also the first number in the array or other

CodePudding user response:

For I in [1, 2, 3] :
For x in (three, four, five) :
Print (I + ", "+ x)

CodePudding user response:

reference 1/f, try _except response:
don't know what you want from the array of the number of arbitrary number, is also the first number in the array or other
arbitrary number, all combinations

CodePudding user response:

Smart - reference 2 floor Space response:
for I in [1, 2, 3] :
For x in (three, four, five) :
Print (I + ", "+ x)
this I just take, for example, is not necessarily a few arrays, want a generic

CodePudding user response:

Smart - reference 2 floor Space response:
for I in [1, 2, 3] :
For x in (three, four, five) :
Print (I + ", "+ x)
I just cited the example of two, is not necessarily a few arrays, want a generic

CodePudding user response:

So!!!!
 
Import the random # import random module

List_1=[[1, 2, 3], [4 and 6], [6,7,8,9,10],,2,3,23,56 [1], [1,2,3,4,5,4,3,2122,345,5678]]
List_2=[] # used to store data from the list_1
For list_3 list_1 in:
List_2. Append (list_3 [random randrange (len (list_3)])

Print (list_2)

CodePudding user response:

To define a number of lists, such as
A=[...].
B=[...].
For I in a:
For x in b:
It can output all combinations, of course, you can add more list and circulating
Also can real-time add like upstairs, generate random combination

CodePudding user response:

The original poster wants to such effect?

 
Def lists_combination (lists) :
# enter multiple list of lists,
# output each list all elements may be all the permutation and combination
Try:
Import the reduce
Except:
The from functools import reduce

Def myfunc (list1 list2) :
Return [STR (I) + STR (j) for I in list1 for j list2] in

Return the reduce (myfunc, lists)


Def info_print (lists) :
The order=0
Total=1 if len (lists) & gt; The else 0 0
Sizes=[]
For LST in lists:
Size=len (LST)
Sizes. Append (STR (size))
Total *=size
The order +=1
Print (f "first {order} {LST} the size of the child list is: {size}")
Exp='*'. The join (sizes)
The number of print (f "permutation and combination should be: {exp}={total}")


If __name__=="__main__ ':
L1=[1, 2, 3, 4]
L2=[4, 5, 6]
L3=[7, 8, 9]
# l4, l5, l6=... Ln=... # themselves continue to add list
Ls=[l1, l2, l3] # note that match your initialization good all array
Info_print (ls)
Result_list=lists_combination (ls)
All print (f "permutation and combination is: \ n {result_list}")

CodePudding user response:

Suppose two arrays is x, y
The first in the list comprehensions
[(a, b) for a in x for b y] in
The second
The from itertools import product
The list (product ((x, y)))
If you don't add a list is just an iterable

CodePudding user response:

 
> A, b, c=[1, 2, 3], [4 and 6], [7,8,9]
> The from itertools import product
> The list (the product (a, b, c))
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]
>
  • Related