How to compare 2 lists in python 3?

1

I'm working on Python 3.4, I have 2 lists:

lista_1 = ['2017-10-01', '2017-10-02', '2017-10-03', '20107-10-04'] # Fechas de un periodo  
lista_2 = [['20107-10-01', campo2, campo3], ['2017-10-03', campo2, campo3], ['2017-10-04', campo2, campo3]] #Datos a analizar  

I want to compare the two lists to see if the first data (date) of each sublist of the lista_2 is within the lista_1 .

If not found in lista_2 , a sublist with the missing date and field 2 and field 3 must be added to lista_2 as an empty string; at the end sort the lista_2 by date from the oldest to the most recent. Fecha is a DateTime object in both lists.

    
asked by Alejandro Gomez 20.10.2017 в 19:24
source

2 answers

0

A simple option is to use sets difference to get the dates that are in the first list but not in the second.

Then just use list.extend and list.sort :

from datetime import datetime

lista_1 = [datetime(2017, 10, 1),
           datetime(2017, 10, 2),
           datetime(2017, 10, 3),
           datetime(2017, 10, 4)] 

lista_2 = [[datetime(2017, 10, 1), "a", "b"],
           [datetime(2017, 10, 3), "a", "b"],
           [datetime(2017, 10, 4), "a", "b"]] 

faltan = ([fecha, "", ""] for fecha in set(lista_1) - set(sub_l[0] for sub_l in lista_2))    
lista_2.extend(faltan)    
lista_2.sort()

Exit:

[[datetime.datetime(2017, 10, 1, 0, 0), 'a', 'b'],
 [datetime.datetime(2017, 10, 2, 0, 0), '', ''],
 [datetime.datetime(2017, 10, 3, 0, 0), 'a', 'b'],
 [datetime.datetime(2017, 10, 4, 0, 0), 'a', 'b']]

A small explanation:

  • set(sub_l[0] for sub_l in lista_2) creates a set with the first elements of the second list, that is, creates a set with the dates of each sublist of lista_2 .

  • set(a) - set(b) returns the elements that are in a but not in b .

  • faltan is a generator that returns a list [fecha, "", ""] for each date that is in lista_1 but not in lista_2 .

  • lista_2.extend runs through the previous generator and adds each item to the list.

  • list.sort() sort the list.
answered by 20.10.2017 / 19:45
source
0

The solution of @FJSevilla is undoubtedly the most optimal for the use of set . For information purposes I add the solution using "list comprehension", it is much less optimal in terms of performance and resources:

lista_1 = ['2017-10-01', '2017-10-02', '2017-10-03', '2017-10-04']  
lista_2 = [['2017-10-01', '1', '2'], ['2017-10-03', '3', '4'], ['2017-10-04', '5', '6']]

lista_2.extend([[f,'',''] for f in lista_1 if f not in [d[0] for d in lista_2]])
print(lista_2)

Exit:

[['2017-10-01', '1', '2'], ['2017-10-03', '3', '4'], ['2017-10-04', '5', '6'], ['2017-10-02', '', '']]

Explanation:

  • With [d[0] for d in lista_2] we build a list of Lista_2 with dates only
  • With for f in lista_1 if f not in [d[0] for d in lista_2]] we are left with dates that were not in lista_2
  • With [f,'',''] we build the element as we want it
  • With lista_2.extend(..) we add all the missing elements
answered by 20.10.2017 в 20:10