简体   繁体   中英

Python List selection under certain condition?

MWE: I would like to do the following :

M=[[0,2,1,4,9,7,6,0],
    [0,7,1,4,7,7,6,2],
    [0,5,1,4,5,5,5,0],
    [0,1,1,1,9,7,6,0],
    [0,2,1,8,8,7,8,2],
    [0,9,0,4,9,0,0,0],
    [0,2,3,4,4,6,6,0]]

s=[[1,2,3],[1,2],[4,5],[1,3,6,7]]

if s[0] is selected then the operation should be

  • 1st row of M no change
  • 2nd of M = 1st row of M + 2nd row of M
  • 3rd row of M = 3rd row of M + (1st row of M + 2nd row of M)
  • and for the remaining r-th row=r-th row of M + (1st row of M + 2nd row of M+3rd row of M +3rd row of M ), r=4,5,6,7

if s[2] is selected then the operation should be

  • 4th row of M no change
  • 5th of M = 4th row of M + 5th row of M
  • and for the remaining r-th row=r-th row of M + ( 4th row of M + 5th row of M), r=1,2,3,6,7

and so on..

When s varies randomly for each counter, how can I handle it. Here is the code I have tried, but ultimately stuck.

for r in s:
    for i in range(len(M)):
        for j in range(len(M[0])):
            if s[0][0]==1
                M[1][i]=M[1][i]

After then I can not figure out how to do the rest. How can I do this?

The following will output the results for all four sublists of s :

from pprint import pprint
from copy import deepcopy
M=[[0,2,1,4,9,7,6,0],
    [0,7,1,4,7,7,6,2],
    [0,5,1,4,5,5,5,0],
    [0,1,1,1,9,7,6,0],
    [0,2,1,8,8,7,8,2],
    [0,9,0,4,9,0,0,0],
    [0,2,3,4,4,6,6,0]]
s=[[1,2,3],[1,2],[4,5],[1,3,6,7]]
def f(m, s):
    new = deepcopy(m)
    for r in s:
        for row in range(r, len(m)):
            for col in range(len(m[row])):
                new[row][col] += m[r - 1][col]
    return new
for i in s:
    pprint(f(M, i), width=40)

This outputs:

[[0, 2, 1, 4, 9, 7, 6, 0],
 [0, 9, 2, 8, 16, 14, 12, 2],
 [0, 14, 3, 12, 21, 19, 17, 2],
 [0, 15, 4, 13, 30, 26, 23, 2],
 [0, 16, 4, 20, 29, 26, 25, 4],
 [0, 23, 3, 16, 30, 19, 17, 2],
 [0, 16, 6, 16, 25, 25, 23, 2]]
[[0, 2, 1, 4, 9, 7, 6, 0],
 [0, 9, 2, 8, 16, 14, 12, 2],
 [0, 14, 3, 12, 21, 19, 17, 2],
 [0, 10, 3, 9, 25, 21, 18, 2],
 [0, 11, 3, 16, 24, 21, 20, 4],
 [0, 18, 2, 12, 25, 14, 12, 2],
 [0, 11, 5, 12, 20, 20, 18, 2]]
[[0, 2, 1, 4, 9, 7, 6, 0],
 [0, 7, 1, 4, 7, 7, 6, 2],
 [0, 5, 1, 4, 5, 5, 5, 0],
 [0, 1, 1, 1, 9, 7, 6, 0],
 [0, 3, 2, 9, 17, 14, 14, 2],
 [0, 12, 2, 13, 26, 14, 14, 2],
 [0, 5, 5, 13, 21, 20, 20, 2]]
[[0, 2, 1, 4, 9, 7, 6, 0],
 [0, 9, 2, 8, 16, 14, 12, 2],
 [0, 7, 2, 8, 14, 12, 11, 0],
 [0, 8, 3, 9, 23, 19, 17, 0],
 [0, 9, 3, 16, 22, 19, 19, 2],
 [0, 16, 2, 12, 23, 12, 11, 0],
 [0, 18, 5, 16, 27, 18, 17, 0]]

Let the selected sublist from s be called lst
First calculate the sum of all M[i] for i in lst

sum_ = [0]*len(M[0])
for i in lst:
    sum_ += M[i]

Now for i not in lst

for i in range(len(M)):
    if i in lst:
        if i == 0: continue
        for j in range(len(M[i])):
            M[i][j] += M[i-1][j]
    else:
        M[i] += sum_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM