簡體   English   中英

以只有第一個元素被彼此不同的第二個元素替換的方式組合兩個列表

[英]Combine two lists in a way that only those elements of first are replaced with elements of second which are different from each other

我想以一種方式加入兩個列表,即只有第一個元素被彼此不同的第二個元素替換。

[
   ['19-12-2022', 'MATH161: A', '-', '-', '-', '-', '-', '-', '-'],
   ['19-12-2022', '-', 'MATH111: P', '-', '-', '-', '-', '-', '-'],
   ['19-12-2022', '-', '-', 'HU108: P', '-', '-', '-', '-', '-'],
   ['21-12-2022', 'CS110lab: P', '-', '-', '-', '-', '-', '-', '-']
]

這是列表列表。 我想將它們組合起來,以便將具有相同日期的那些組合成一個列表。

def getAttendanceTableFor(id: str) -> pd.DataFrame:
    """This function takes a student's cms Id, then creates a table for his attendance in all courses and returns it as a DataFrame object.

    Args:
        id (str): Id of student

    Returns:
        pd.DataFrame: DataFrame object for attendance of student.
    """
    db = sqlite3.connect(databaseName)
    cur = db.cursor()
    tableOfAttendance = []

    timeTableWeekDays = timeTable.to_dict('tight')['index']
    timeTableTimes = timeTable.to_dict('tight')['columns']
    timeTableClasses = timeTable.to_dict('tight')['data']

    for course in courses:
        cur.execute(f"SELECT `dayTime`, `{id}` FROM `{course}`;")
        records = cur.fetchall()

        # dayTime is in form of date-month-year-time
        # So first 3 are date and last one is time
        dates = list(["-".join(date.split('-')[:3])
                      for date, _ in records])
        times = list([date.split('-')[-1]
                      for date, _ in records])
        attendances = list([attendance
                           for _, attendance in records])
        for time in times:
            if len(time) == 3:
                times[times.index(time)] = '0' + time

        for date in dates:
            record = [date] + ["-"] * len(
                timeTableTimes)

            for time, attendance in zip(times, attendances):
                weekDayIndex = timeTableWeekDays.index(getWeekDay(date))
                timeIndex = timeTableTimes.index(time)
                record[timeIndex +
                       1] = f"{timeTableClasses[weekDayIndex][timeIndex]}: {attendance}"
            tableOfAttendance.append(record)

    [print(d) for d in tableOfAttendance]

    cur.close()
    db.close()
    attendanceDataFrame = pd.DataFrame(tableOfAttendance,
                                       columns=(['Date'] + timeTableTimes))
    return attendanceDataFrame

這是我為生成該列表列表而編寫的代碼。

你可以試試:

lst = [
    ["19-12-2022", "MATH161: A", "-", "-", "-", "-", "-", "-", "-"],
    ["19-12-2022", "-", "MATH111: P", "-", "-", "-", "-", "-", "-"],
    ["19-12-2022", "-", "-", "HU108: P", "-", "-", "-", "-", "-"],
    ["21-12-2022", "CS110lab: P", "-", "-", "-", "-", "-", "-", "-"],
]

tmp = {}
for subl in lst:
    tmp.setdefault(subl[0], []).append(subl[1:])

out = []
for k, v in tmp.items():
    out.append([k])
    for t in zip(*v):
        out[-1].append(next((i for i in t if i != "-"), "-"))

print(out)

印刷:

[
    ["19-12-2022", "MATH161: A", "MATH111: P", "HU108: P", "-", "-", "-", "-", "-"],
    ["21-12-2022", "CS110lab: P", "-", "-", "-", "-", "-", "-", "-"],
]

我的建議是使用itertools.groupby根據日期進行分組:

from itertools import groupby
from operator import itemgetter

data = [
   ['19-12-2022', 'MATH161: A', '-', '-', '-', '-', '-', '-', '-'],
   ['19-12-2022', '-', 'MATH111: P', '-', '-', '-', '-', '-', '-'],
   ['19-12-2022', '-', '-', 'HU108: P', '-', '-', '-', '-', '-'],
   ['21-12-2022', 'CS110lab: P', '-', '-', '-', '-', '-', '-', '-']
]

s = sorted(data, key=itemgetter(0))
g = groupby(data, key=itemgetter(0))

d = {k: list(v) for k, v in g}
# {
#   '19-12-2022': [
#     ['19-12-2022', 'MATH161: A', '-', '-', '-', '-', '-', '-', '-'], 
#     ['19-12-2022', '-', 'MATH111: P', '-', '-', '-', '-', '-', '-'], 
#     ['19-12-2022', '-', '-', 'HU108: P', '-', '-', '-', '-', '-']], 
#   '21-12-2022': [
#     ['21-12-2022', 'CS110lab: P', '-', '-', '-', '-', '-', '-', '-']
#   ]
# }

現在使用functools.reduce來替換列表的內容。

from functools import reduce

{
  k: reduce(lambda acc, x: [a if b == '-' else b for a, b in zip(acc, x)], v[1:], v[0]) 
  for k, v in d.items()
}
# {
#   '19-12-2022': ['19-12-2022', 'MATH161: A', 'MATH111: P', 'HU108: P', '-', '-', '-', '-', '-'], 
#   '21-12-2022': ['21-12-2022', 'CS110lab: P', '-', '-', '-', '-', '-', '-', '-']
# }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM