Merging sublists with same initial element by substituting null values

Question:

I want to merge sublists with the same initial first element, but instead of adding the other values one after the other, I want to substitute values where they are None.
I have a matrix with sublists, each sublist contains 7 values: number of the element, score A, score B, score C, score D, score E, score F. So far for each sublist there is only one value (even if this is the same for different sublists), but I want to merge sublists that contain different scores for the same element.

I have

sub_lists = [(1,None,None,12,None,None,None),
             (2,67,None,None,None,None,None), 
             (2,None,None,83,None,None,None),
             ...]

So for each sublist there is only 1 score indicated while the others are null. The result I am looking for is

sub_lists = [(1,None,None,12,None,None,None),
             (2,67,None,83,None,None,None),
             ...]

What I have tried is

res = []
for sub in sub_lists:
    if res and res[-1][0] == sub[0]:
        res[-1].extend(sub[1:])
    else:
        res.append([ele for ele in sub])
res = list(map(tuple, res))

But this only adds the values one after the other, resulting in

sub_lists = [(1,None,None,12,None,None,None),
             (2,67,None,None,None,None,None,None,None,83,None,None,None),
             ...]

Does someone know how to help me with this?

Asked By: Mary

||

Answers:

since sub-lists’ first numbers will be unique at the end, you could use a dictionary.

so do something like:

def my_combine(l1, l2):
  l1 = list(l1)
  for i in range(len(l1)):
    if l1[i] is None:
      l1[i] = l2[i]

  return tuple(l1)

results = dict()
for sl in sub_lists:
  if sl[0] not in results:
    results[sl[0]] = sl[1:]
  else:
    results[sl[0]] = my_combine(results[sl[0]], sl[1:])
Answered By: Adam Smooch

sub_lists=[(1,None,None,12,None,None,None),(2,67,None,None,None,None,None),(2,None,None,83,None,None,None)]
res = [] #resultant matrix
for sub in sub_lists:
    if res and res[-1][0] == sub[0]:
        valid = [] # let "valid" valid sublist that can be e added in resultant matrix.
        valid.append(res[-1][0]) # we will add the the first element of last sublist of resultant matrix, and rest of the elements of sub list of "sub_lists"
        valid.extend(sub[1:])
        res[-1] = valid
        print(sub)
    else:
        res.append([ele for ele in sub])
res = list(map(tuple, res))

print(res)

# desired outcome.
# sub_lists=[(1,None,None,12,None,None,None),(2,67,None,83,None,None,None)]

hope you got the desirable code,
and my comments make sense.☺️

feel free to ask anything further…!

Answered By: Manohar

adam-smooch

he was right I think


sub_lists=[
  (1,None,None,12,None,None,None),
  (2,67,None,None,None,None,None),
  (2,None,None,83,None,None,None)
]
def my_combine(l1, l2):
  l1 = list(l1)
  l2 = list(l2)
  for i in range(len(l1)):
    if l1[i] is None:
      l1[i] = l2[i]

  return l1

results = dict()
for sl in sub_lists:
  if sl[0] not in results:
    results[sl[0]] = sl[1:]
  else:
    results[sl[0]] = my_combine(results[sl[0]], sl[1:])
print(results)

# desired outcome.
# sub_lists=[
#   (1,None,None,12,None,None,None),
#   (2,67,None,83,None,None,None)
# ]

I think @Adam Smooch was right I am modified it a bit

Answered By: Manohar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.