Home > Software engineering >  how to merge and sort a list with datetime and lambda in Python3?
how to merge and sort a list with datetime and lambda in Python3?

Time:09-27

I need to get info from two separate txt files, merge and sort them based on the first index of the list (date). I am having trouble using Lambda and DateTime together. I am currently stuck on this issue and have run out of ideas on how to implement this. I would also appreciate an explanation in order for me to comprehend where I am making a mistake.

  • I think that I am possibly creating various lists of lists, however, when I print the Len of my last List it says it contains the same amount of items as intended. Hence I believe that I am not mistakenly creating lists with extra values.

Note: I am 2 months into my programming journey and have searched for different methods to resolve my problem, but I am still coming up short. And I implemented the same concept yesterday on a much simpler problem where the first index was a single-digit INT, which was a lot easier. With the Date being the first index I am having trouble understanding the syntax of DateTime and lambda together in my code. I appreciate all help and opinions!

from datetime import datetime
from typing import Any, List


def get_list1(file_name: str) -> Any:
    list1 = []

    with open(file_name, newline='') as inputFile:
        lines = inputFile.readlines()
        for line in lines:
            line = line.replace("\r\n", '')
            seperate = line.split(";")
            list1.append(seperate)
    return list1

def get_list2(file_name: str) -> Any:
    list2 = []

    with open(file_name, newline='') as inputFile:
        lines = inputFile.readlines()
        for line in lines:
            line = line.replace("\r\n", '')
            seperate = line.split(";")
            list2.append(seperate)
    return list2


def mergeLists(list1:List, list2:List):
    newList = []

    for item in list1:
        newList.append(list1)
    for item in list2:
        newList.append(list2)
    print(len(newList))
    return(newList)


def sortedList(newList:List):
    orderedList = sorted(newList, key=lambda x: datetime.datetime.strptime(x[0]))
    print(orderedList)


list1 = get_list1("bitcoin.txt")
list2 = get_list2("exchange.txt")
newList =mergeLists(list1, list2)
sortedList(newList)
bitcoin.txt = 
2022-08-01;buy;100
2022-08-04;buy;50
2022-08-06;buy;10

exchange.txt = 
2022-08-02;buy;200
2022-08-03;sell;300
2022-08-05;sell;25

Desired output sorted by date = [2022-08-01;buy;100, 2022-08-02;buy;200, 2022-08-03;sell;300, 2022-08-04;buy;50, 2022-08-05;sell;25, 2022-08-06;buy;10]

CodePudding user response:

Since your dates are in the format yyyy-mm-dd (sortable as string) and the first text of every line, you can sort them without converting them to datetime. This should do it:

def file_to_list(file: str) -> list:
    out = []
    with open(file) as f:
        for line in f:
            out.append(line.strip())
    return out

bitcoin  = file_to_list("bitcoin.txt")
exchange = file_to_list("exchange.txt")
combined = bitcoin   exchange

sorted(combined)

Out:

['2022-08-01;buy;100',
 '2022-08-02;buy;200',
 '2022-08-03;sell;300',
 '2022-08-04;buy;50',
 '2022-08-05;sell;25',
 '2022-08-06;buy;10']
  • Related