I don't know how to select the list value in order from one list of Python and apply it to another list

Asked 2 years ago, Updated 2 years ago, 22 views

For example, my list is

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

If you say

New list

new_list = [[[0,1] ,[0,2], [0,3],.....,[0,15]], [[1, 2], [1, 3], [1, 4],...[1,15]], [[2, 3],....[2,15]],....[[14,15]]]

I'm going to make a list in the list.

[x, y] If I say x, I want to make sure that the same list is bound to the same list. I want to set y from one larger than x to the last value of 15. The value of y must be greater than x.

sss = [ ]

ttt = [ ]

i = 0

j = 1

while  j < len(seq_list):

    if j>i:
        x = [i,j]
        j = j+1
        sss.append(x)
    if j == len(seq_list):
        i = i+1
        print(sss)
        ttt.append(sss)
        j=i+1

All the sets are tied to the sss list. I ask for your help me. <

python

2022-09-21 12:27

3 Answers

I think you can do it like this.

L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

result = []
for i in L[:-1]:
    for i2 in range(i+1, L[-1]+1):
        result.append([i, i2])
result = result[:-1]

# result value
[
 [[0, 1], [0, 2], ... [0, 14], [0, 15]],
 [[1, 2], [1, 3], ... [1, 14], [1, 15]],
 ...
 [[13, 14], [13, 15]],
 [[14, 15]]
]

Below is a simplified code.

L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

result = [[[i, i2] for i2 in range(i+1, L[-1]+1)] for i in L[:-1]]

I hope it was helpful!


2022-09-21 12:27

Please keep that in mind.

src = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
[[[x, y] for y in src[x+1:]] for x in src[:-1]]

[[[0, 1],
  [0, 2],
  [0, 3],
  [0, 4],
  [0, 5],
  [0, 6],
  [0, 7],
  [0, 8],
  [0, 9],
  [0, 10],
  [0, 11],
  [0, 12],
  [0, 13],
  [0, 14],
  [0, 15]],
 [[1, 2],
  [1, 3],
  [1, 4],
  [1, 5],
  [1, 6],
  [1, 7],
  [1, 8],
  [1, 9],
  [1, 10],
  [1, 11],
  [1, 12],
  [1, 13],
  [1, 14],
  [1, 15]],
...
...
[[10, 11], [10, 12], [10, 13], [10, 14], [10, 15]],
 [[11, 12], [11, 13], [11, 14], [11, 15]],
 [[12, 13], [12, 14], [12, 15]],
 [[13, 14], [13, 15]],
 [[14, 15]]]


2022-09-21 12:27

Extra. Try it with scalar

val src = 0 to 15 
val r = for(x <- src.slice(0, src.size - 1)) yield for(y <- src.slice(x+1, src.size)) yield (x, y)

r(10).foreach(println)
(10,11)
(10,12)
(10,13)
(10,14)
(10,15)


r(12).foreach(println)
(12,13)
(12,14)
(12,15)


2022-09-21 12:27

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.