覆盖python中的二维列表

时间:2014-02-08 08:27:19

标签: python list multidimensional-array overwrite

我最近开始使用python,并想知道如何覆盖列表中的元素。我制作了一个有x行和y列的二维列表。我正在计算每列的总和,并将其存储在相同的数组中,该数组应该有x个不同的列总和,但得到2 * x行,因为我不知道如何覆盖元素,因此我将总和追加到list。我想用sum覆盖2-D列表值,这样只有x行而不是2 * x行。

List = [[0 for col in range(K)] for row in range(T)]
file = open("xyz.txt", "r")
for word in file:
   for j in range(T):
      for k in range(K):
          concat = str(k) + str(repr((1+e)**j+1)) + word
          m = hashlib.md5()
          m.update(concat) 
          concat = long(m.hexdigest(),16) 
          if (concat % int((1+e)**j+1)) == 0:
            List[j][k] = 0    

for i in range(T):
    List[i] = sum(List[i])
    List.append(List[i])
print List

输出:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 8, 10, 34, 59, 102, 157, 204, 248, 281,
339, 348, 389, 429, 435, 464, 464, 478, 489, 496, 501, 497,
504, 504, 503, 508, 506, 509, 508, 510, 510, 511, 511, 509,
509, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511,
511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511,
511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511]

我希望输出为:

[1, 8, 10, 34, 59, 102, 157, 204, 248, 281, 339, 348, 389,
429, 435, 464, 464, 478, 489, 496, 501, 497, 504, 504, 503,
508, 506, 509, 508, 510, 510, 511, 511, 509, 509, 511, 511,
511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511,
511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511, 511,
511, 511, 511, 511, 511, 511, 511, 511]

1 个答案:

答案 0 :(得分:0)

此行不是必需的:List.append(List[i])