Reputation: 3
import sys
su_pri=0
su_sc=0
n = int(raw_input().strip())
m=0
j=n-1
count=1
diff=0
a = []
for a_i in range(n):
a_temp = map(int,raw_input().strip().split(' '))
a.append(a_temp)
print a
while(m<=len(a)):
su_pri=sum(su_pri,int(a[m]))
m=m+n+1
while(count<=n):
su_sc=su_sc+a[j]
j=j+n-1
diff=abs(su_pri-su_sc)
print diff
if n=3 then the list is
3 11 2 4 4 5 6 10 8 -12 The list is [[11, 2, 4], [4, 5, 6], [10, 8, -12]] i want to store all elements into a single list with length=9(in this case) How can i do that??? Please tell me
Upvotes: 0
Views: 70
Reputation: 32
I think you're looking to try to flatten a list of lists in python, which I found this post really helpful.
l = [[11, 2, 4], [4, 5, 6], [10, 8, -12]] #the list of lists to be flattened
flattenedList = [item for sublist in l for item in sublist]
Upvotes: 0
Reputation: 306
You want to add each element in the array to each other. Something like this maybe? Hope that helps :)
Upvotes: 0
Reputation: 15513
For instance:
a = [[11, 2, 4], [4, 5, 6], [10, 8, -12]]
b = a[0] + a[1] + a[2]
Upvotes: 0
Reputation: 35
Create a new list and add them together
newdiff = []
for eachlist in diff:
newdiff += eachlist
print newdiff
Upvotes: 1