-2

Background

I have two lists:

list_1 = ['a_1', 'b_2', 'c_3']
list_2 = [ 'g b 2', 'f a 1', 'h c 3']

Please note that the format of the string elements in the lists is different. In other words, elements of one of the lists are not a subset of the other.

I want to

  1. Compare the elements of lists 1 and 2, identify elements in list 2 are similar to list 1
  2. Then I want to sort the list 1 as ['b_2', 'a_1', 'c_3'] in the same order as list 2

Existing questions

  • Q1: Here, the elements of one list match exactly with other to some extent '2010-01-01 00:00' and '2010-01'. However, in my case, the formatting may be different.
  • Similar case for Q2.

There are several other questions looking into list comparisons, but most of them compare similar strings.


Actual lists

list_1 = ['f_Total_water_withdrawal', 
'f_Precipitation', 
'f_Total-_enewable_water_resources', 
'f_Total_exploitable_water_resources',]

list_2 = ['Precipitation',
'Total-renewable-water-resources', 
'Total exploitable water resources', 
'Total water withdrawal']
Neeraj Hanumante
  • 1,575
  • 2
  • 18
  • 37

3 Answers3

0

I believe there is some missing information. Nonetheless, for the given lists, we can devise this approach:

# 1. Format list 2 to look like list 1
list_2_mod = [s[2:].replace(" ", "_") for s in list_2]

# 2. Filter elements in list 2 not in list 1
list_final = [s for s in list_2_mod if s in list_1]

The smart move is that, given your list_1 (with unique elements, and all elements having an evident equivalent in list_2), you only need the first step. No need to sort! list_2 already as everything sorted.

ibarrond
  • 6,617
  • 4
  • 26
  • 45
0

I might be misunderstanding, but if your lists correspond to your example, you can simply define list_1 using list_2:

list_2 = ['g b 2', 'f a 1', 'h c 3']
list_1 = [f"{s[2]}_{s[4]}" for s in list_2]

print(list_1)

Output:

['b_2', 'a_1', 'c_3']
Red
  • 26,798
  • 7
  • 36
  • 58
0

I think I roughly understand what you want to do. Please see the below:

list_1 = ['a_1', 'b_2', 'c_3']
list_2 = [ 'g b 2', 'f a 1', 'h c 3']
dict_1 = {item1[0] + ' ' + item1[-1]: item1 for item1 in list_1}
l = [dict_1[item2[2:]] for item2 in list_2 if item2[2:] in dict_1]
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dev
  • 665
  • 1
  • 4
  • 12