Question

I have started with python recently and wanted to know how to overwrite the elements in the list. I have made a 2-D list which have x rows and y columns. I am calculating the sum of each column and storing it in the same array which should have x different sums of the columns but getting 2*x rows because I don't know how to overwrite the elements and therefore I am appending the sum to the list .I want to overwrite the 2-D list values with sum so that there are only x rows and not 2*x rows.

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

Output:

[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]

I want the output to be:

[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]
Was it helpful?

Solution

This line isn't necessary: List.append(List[i]).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top