0

Hi please how can I loop through lst2, check for the matching ones and the sum up the value of v in the in the corresponding dictionary.

i.e. if the elements of ('1','2') in lst2 matches the '1', '2' in lst1, get the value of v ie 5 and add the v values for all the tuples in that list of list.

lst1 = [('1','2',{"v" : 5, "a" : 3}),('7','9',{"v" : 7, "a" : 3}),('3','4',{"v" : 4, "a" : 3}),('1','6',{"v" : 0, "a" : 3}),('2','9',{"v" : 4, "a" : 3})]

lst2 = [[('1','2'),('2','9')], [('1','6'),('7','9')]]

output should be vol = 5 + 4 = 9 for [('1','2'),('2','9')] 

please I tried this and it did give the desired output. Please help

vol = []
f = 0.0
for b in lst2:
    if  float(b[0][0]) == float(lst1[0][0]) and float(b[0][0])== float(lst1[0][1]):
        f = f + float(lst1[0][0][v])
        vol.append(f)
3
  • 1
    Can you post what you tried and the output it gave you? Commented May 1, 2014 at 21:45
  • For a start, I would suggest changing lst1 to a dictionary with the format {('1', '2'): {'v': 5, 'a': 3}, ('7', '9'): {'v': 7, 'a': 3}} (and so on for the other elements). Commented May 1, 2014 at 21:48
  • sorry about that I just updated. @F.J that's the data format for the project i am working on. Thanks Commented May 1, 2014 at 21:57

1 Answer 1

1
lst1 = [('1','2',{"v" : 5, "a" : 3}),('7','9',{"v" : 7, "a" : 3}),('3','4',{"v" : 4, "a" : 3}),('1','6',{"v" : 0, "a" : 3}),('2','9',{"v" : 4, "a" : 3})]

lst2 = [[('1','2'),('2','9')], [('1','6'),('7','9')]]
vol = [] 

for elem in lst2:
    for sub_e in elem:
        for l in lst1:
            if  l[0:2] == sub_e:
                vol.append(l[2]["v"])
 In [4]: print vol
 [5, 4, 0, 7]

 In [5]: print sum(vol)
 16


To pair the output:

pairs=[]
for i in xrange(0,len(vol),2) :
    pairs.append([vol[i],vol[i+1]])
In [10]: pairs
Out[10]: [[5, 4], [0, 7]]

# even and odd length lists
pairs=[]
vol_it = iter(vol)
for i,j in zip(vol_it, vol_it):
   pairs.append([i,j])
if len(vol) %2 ==1:
    pairs.append([vol[-1]])

In [28]:  vol=[1,2,3,4,5,6]

In [29]: pairs
Out[30]: [[1, 2], [3, 4], [5, 6]]

In[31]: vol=[1,2,3,4,5]
In [31]: pairs
Out[31]: [[1, 2], [3, 4], [5]]


using  itertools:

from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)
print [[x[0],x[1]]for x in (grouper(vol,2)) ]

Is this what you are after?

In [6]: paste
lst1 = [('1','2',{"v" : 5, "a" : 3}),('7','9',{"v" : 7, "a" : 3}),('3','4',{"v" : 4, "a" : 3}),('1','6',{"v" : 0, "a" : 3}),('2','9',{"v" : 4, "a" : 3})]

lst2 = [[('1','2'),('2','9')], [('1','6'),('7','9')]]
vol = [] 

for elem in lst2:
    for sub_e in elem:
        for l in lst1:
            if  l[0:2] == sub_e:
                vol.append(l[2]["v"])

## -- End pasted text --

In [7]: print sum(vol)
16

In [8]: print vol
[5, 4, 0, 7]
Sign up to request clarification or add additional context in comments.

7 Comments

please this gives this error: Traceback (most recent call last): File "C:/Users/AMAMIFE/Desktop/obi/nnnnnnnnnn.py", line 160, in <module> if l[0:2] == sub_e: TypeError: 'int' object has no attribute 'getitem'
@Nobi I added my output from ipython.
Sorry @padraic, but is there a way to have the output of vol be like this [[5,4], [0,7]] rather than [5,4,0,7]. Thanks
I really appreciate your help
@Nobi added code that will handle both even and odd length lists.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.