gython
gython

Reputation: 875

How can I append missing values into a nested based on an another nested list?

I have a nested lists:

pw = [[[1, 0.020387050117001683],
       [2, 0.023095733958878487]],
      [[1, 0.020387050117001683],
       [2, 0.023095733958878487],
       [3, 0.05133549261794933]]]

The sublists are missing specific values, which I have stored in another nested:

nl = [[0 ,0.01], [3, 0.01]]

Sublist 1 is missing the entries with zero and three at index 0, while Sublist 2 is missing the entry with zero at index 0.

My desired result would look like this:

pw = [[[0 ,0.01],
       [1, 0.020387050117001683],
       [2, 0.023095733958878487]
       [3 ,0.01]],
      [[0 ,0.01],
       [1, 0.020387050117001683],
       [2, 0.023095733958878487],
       [3, 0.05133549261794933]]]

However my code:

for line in pw:
    for l in line:
        for f in nl: 
            if not any (f[0] in l[0] for f in nl):
                l.append(f)

Produces this error:

TypeError: argument of type 'int' is not iterable

How can I do this correctly?

Upvotes: 0

Views: 73

Answers (4)

gzix
gzix

Reputation: 269

This will inturn check if "pw" has the item and add acoordingly.

pw = [[[1, 0.020387050117001683],
   [2, 0.023095733958878487]],
  [[1, 0.020387050117001683],
   [2, 0.023095733958878487],
   [3, 0.05133549261794933]]]

nl = [[0 ,0.01], [3, 0.01]]

nl_zero, nl_one = zip(*nl)
for i in pw:
    pwi_zero,pwi_one=zip(*i)
    for j in nl_zero:
        if(j not in pwi_zero):
            item_index=nl_zero.index(j)
            i.append([nl_zero[item_index],nl_one[item_index]])
print(pw)

Output:
     [[[1, 0.020387050117001683], [2, 0.023095733958878487], [0, 0.01], [3, 0.01]], [[1, 0.020387050117001683], [2, 0.023095733958878487], [3, 0.05133549261794933], [0, 0.01]]]

Upvotes: 1

Erik Gomersbach
Erik Gomersbach

Reputation: 44

try this:

pw = [
       [ # line
         [1, 0.020387050117001683], # f
         [2, 0.023095733958878487]
       ],
       [
         [1, 0.020387050117001683],
         [2, 0.023095733958878487],
         [3, 0.05133549261794933]
       ]
     ]

nl = [ 
         [0 ,0.01], # f
         [3, 0.01]
     ]

for line in pw:
    missing = []
    for f in nl:
        for l in line:
            if f[0] != l[0]: # compare first elements
                missing.append(f)
                break # done with this "f" avoid appending again 
    for m in missing:
      line.append(m)

print(pw)

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148975

You are in fact using your nested lists as is they were dictionaries, so I would convert them to plain dictionaries to have an easier processing:

hpw = [dict(i) for i in pw]   # convert nested list to dicts

for h in hpw:                 # add missing values to the dicts
    for i in nl:
        if i[0] not in h:
            h[i[0]] = i[1]

pw = [sorted([[k,v] for k,v in h.items()]) for h in hpw]  # convert back to sorted nested lists

Upvotes: 1

Ratnesh
Ratnesh

Reputation: 1700

If you only need to insert at zero and third index on Sublist1 and at zero index on Sublist2. So you can use this method:

for line in pw:
    if nl[0] not in line:
        line.insert(0,nl[0])

if nl[1] not in pw[0]:
    pw[0].insert(3,nl[1])

Upvotes: 0

Related Questions