- 2020-03-11 14:20
*views 13*- Python Basic grammar
- for
- Python

subject ： Yes 5 Arrays of different lengths a1,a2,a3,a4,a5, Now take a number from the array , Form a new array , Request from a1 The number taken out must be less than from a2 Number taken , From the same principle a2 The number taken out must be less than from a3 Number taken , and so on , Lists all arrays that meet the criteria .

The title is quite simple , Take a look at it , And then I used a bunch of them for the first time for loop , The script is as follows ：

a1 = [1,2,4] a2 = [2,3,4,5] a3 = [1,4,7] a4 = [3,6,7,8] a5 = [3,6,8,9,10] for i

in a1: for j in a2: for k in a3: for l in a4: for m in a5: if i<j<k<l<m: print(i

,j,k,l,m)

The output is right , But look at this pile for loop , I always feel uncomfortable , After a search , Found out itertools.product(A,

B) This function , This function returns A,B Tuple of Cartesian product of elements in , Seems to meet the conditions , So the script above changes to the following

import itertools for i in itertools.product(a1,a2,a3,a4,a5): if i[0]<i[1]<i[2]<

i[3]<i[4]: print(i)

Technology

- Python156 blogs
- Java139 blogs
- Vue91 blogs
- Flow Chart80 blogs
- javascript44 blogs
- C++43 blogs
- algorithm41 blogs
- programing language40 blogs
- more...

Daily Recommendation

views 5

views 3

views 2

views 2

©2020-2021 ioDraw All rights reserved

orm What are the frameworks _java Which technologies are necessary ?Circular radar chartSQL Different values for the same field in , Conduct data statistics Summary of physical knowledge points For beginners Python Is development difficult to learn ? Is it suitable for beginners ? How ABC The three threads execute ten times in sequence “Python Is the best language in the world ”Vue cache algorithm, LRU policy algorithmVue component communication modeStruts2 Deserialization vulnerability recurrence