Home > Software design >  Extract one common element from two lists and create a dictionary with mapping from one list to othe
Extract one common element from two lists and create a dictionary with mapping from one list to othe

Time:06-13

I am trying to extract same values from one position of a nested list and its corresponding values from another position.

JJ = [['HC', 0, ' 3.6'], 
      ['HC', 1, ' 3.9'], 
      ['HC', 2, ' 7.0'], 
      ['NC', 7, ' 0.3'], 
      ['NC', 8, ' 0.4'], 
      ['NC', 9, ' 0.5'], 
      ['NC', 10, ' 0.6'], 
      ['NC', 11, ' 0.7'], 
      ['DC', 12, ' 0.8'], 
      [['DC','NC'], 13, ' 0.9']]

This is a list where the first element is repeated in some of the sub-lists. I am trying to write a code which will take all the third elements from each of the sub-list and map it with its corresponding first element using two lists. Ignore the second element. The required output is supposed to be:

list1 = [['HC'],['NC'],['DC'],['DC','NC']]
list2 = [[3.6, 3.9, 7], [0.3, 0.4, 0.5, 0.6, 0.7],[0.8],[0.9]]

where each element in list1 now corresponds to the element of the same position in list2, or can be a dictionary mapping. I have tried this:

list1 = []
list2 = []
for i in range(0,len(JJ)-1):
    #i_set = set(i)
    #for j in range(len(i)):
    if JJ[i][0] == JJ[i 1][0]:
        list1.append(JJ[i][0])
        list2.append(JJ[i][2])
list1 = set(list1)

This gives me same elements again and again in list1 and if I convert it to a set, the mapping/correspondence between elements is lost. Also, since the last element is len(JJ)-1 (without subtracting 1 the code shows index error), I cannot collect the last element. Is there any other way to do this?

CodePudding user response:

With a simple for-loop: The basic idea is to keep track of the previous first element and the current first element and if it differs, append a new list to list1 and list2.

list1 = []
list2 = []
prev = curr = None
for sublist in JJ:
    curr = sublist[0]
    if curr != prev:
        list1.append([curr])
        list2.append([])
    list2[-1].append(float(sublist[2]))
    prev = curr

groupby from the itertools module could be useful

from itertools import groupby

JJ = [['HC', 0, ' 3.6'], ['HC', 1, ' 3.9'], ['HC', 2, ' 7.0'], ['NC', 7, ' 0.3'], 
      ['NC', 8, ' 0.4'], ['NC', 9, ' 0.5'], ['NC', 10, ' 0.6'], ['NC', 11, ' 0.7'], 
      ['DC', 12, ' 0.8'], [['DC','NC'], 13, ' 0.9']]

list1, list2 = [], []
#  groupby the first elements in the inner lists
for key, group in groupby(JJ, lambda x: x[0]):
    # append keys as lists
    list1.append(key if isinstance(key, list) else [key])
    # append the last element of the sublists
    list2.append([float(e[-1]) for e in group])

print(list1)
# [['HC'], ['NC'], ['DC'], ['DC', 'NC']]

print(list2)
# [[3.6, 3.9, 7.0], [0.3, 0.4, 0.5, 0.6, 0.7], [0.8], [0.9]]

CodePudding user response:

You can do it like this without any additional imports:

JJ = [['HC', 0, ' 3.6'], 
      ['HC', 1, ' 3.9'], 
      ['HC', 2, ' 7.0'], 
      ['NC', 7, ' 0.3'], 
      ['NC', 8, ' 0.4'], 
      ['NC', 9, ' 0.5'], 
      ['NC', 10, ' 0.6'], 
      ['NC', 11, ' 0.7'], 
      ['DC', 12, ' 0.8'], 
      [['DC','NC'], 13, ' 0.9']]

dict_ = {}

for k, _, n in JJ:
    if isinstance(k, list):
        k = tuple(k)
    dict_.setdefault(k, []).append(float(n))

list1 = []
list2 = []


for k, v in dict_.items():
    if isinstance(k, tuple):
        k = list(k)
    list1.append(k)
    list2.append(v)

print(list1)
print(list2)

Output:

['HC', 'NC', 'DC', ['DC', 'NC']]
[[3.6, 3.9, 7.0], [0.3, 0.4, 0.5, 0.6, 0.7], [0.8], [0.9]]
  • Related