在列表列表中替换垂直符号



这个问题是这个问题的扩展。

我代表使用列表 L列表的二维数组,说:

[ [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4] ]

对于给定的子列表,例如 [9, 99],我想使用此 sublist替换" 2-d"列表中的特定子列表,例如:

L[1][0:2] = sublist
# which updates `L` to:
[ [1, 2, 3, 4],
  [1, 9, 99, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4] ] # not in this format, but written like this for clarity

这适用于水平替换,但不适合垂直替换,因为我们无法切片以分开这样的列表:L[0:2][0]。如果我使用此切片系统,则可以转置L(列表的转置列表(,然后使用此切片方法,然后将其转换回。但这也不是有效的,即使是为了简单起见。

复制 L[0:2][0] 并获取此输出是什么有效的方法?

[ [1, 2, 3, 4],
  [1, 9, 3, 4],
  [1, 99, 3, 4],
  [1, 2, 3, 4] ]

注意:假定len(sublist) <= len(L),用于垂直替换(这是此问题的重点(。

循环方法:

def replaceVert(al : list, repl:list, oIdx:int, iIdx:int):
    for pos in range(len(repl)):
        al[oIdx+pos][iIdx] =  repl[pos]
a = [ [1, 2, 3, 4],
      [5, 6, 7, 8],
      [9, 10, 11, 12],
      [13, 14, 15, 16] ]
print(a)   # [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
replaceVert(a,['ä','ü'],2,2)  # this is a one liner ;)
print(a)   # [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 'ä', 12], [13, 14, 'ü', 16]]

transpose/slice/transpose方法:

我超阅读了"无换式"的提及。这是使用 transpose,更改,transpose 方法,带有切片,而Q 不需要。是这个问题标题的答案,所以我决定将其留给以后的人们搜索,然后偶然发现了这个Q:

a = [ [1, 2, 3, 4],
      [5, 6, 7, 8],
      [9, 10, 11, 12],
      [13, 14, 15, 16] ] 
b = list(map(list,zip(*a)))  # will make [ [1,5,9,13], ... ,[4,8,12,16]]
b[1][0:2]=['a','b']          # replaces what you want here (using a and b for clarity)
c = list(map(list,zip(*b)))  # inverts b back to a's form
print(a)
print(b)
print(c) 

输出:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]     # a
[[1, 5, 9, 13], ['a', 'b', 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] # b replaced 
[[1, 'a', 3, 4], [5, 'b', 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] # c

计时4x4列表,2替换

setuptxt = """
def replaceVert(al : list, repl:list, oIdx:int, iIdx:int):
    for pos in range(len(repl)):
        al[oIdx+pos][iIdx] =  repl[pos]
a = [ [1, 2, 3, 4],
      [5, 6, 7, 8],
      [9, 10, 11, 12],
      [13, 14, 15, 16] ]
"""
zipp = """b = list(map(list,zip(*a)))  
b[1][0:2]=['a','b']           
c = list(map(list,zip(*b)))
"""
import timeit
print(timeit.timeit("replaceVert(a,['ä','ü'],2,2)",setup = setuptxt))
print(timeit.timeit(stmt=zipp, setup=setuptxt))

输出:

looping: 12.450226907037592
zipping: 7.50479947070815

Wit Zipping(转台/切片/转置(的方法大约需要60%的时间才能完成4x4列表。


更大的列表1000x1000和〜70个元素替换:

setuptxt = """
def replaceVert(al : list, repl:list, oIdx:int, iIdx:int):
    for pos in range(len(repl)):
        al[oIdx+pos][iIdx] =  repl[pos]
a = [ [kk for kk in range(1+pp,1000+pp)] for pp in range(1,1000)] 
repl = [chr(mm) for mm in range(32,100)]
"""
import timeit

print(timeit.timeit("replaceVert(a,repl,20,5)",number=500, setup = setuptxt))
zipp = """b = list(map(list,zip(*a)))  
b[20][5:5+len(repl)]=repl           
c = list(map(list,zip(*b)))
"""
print(timeit.timeit(stmt=zipp, setup=setuptxt,number=500))

输出:

looping: 0.07702917579216137
zipping: 69.4807168493871 

循环获胜。感谢@sphinx的评论

最新更新