繁体   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