合并来自多个不同长度列表的数据

Combine data from multiple lists of different length

我有一个返回三组数据的数据库查询。它采用这种格式。

year = (('Adison', '355', 4), ('windsor windham', '455', 6), ('windham', '655', 2), ('btown', '233', 5))
month = (('Adison', '355', 2), ('windham', '655', 1))
week = (('btown', '233', 8), ('Adison', '355', 9))

年份列表总是最长且值最多的。我需要从月和周列表中的每个元素中获取最后一个值,并将它们附加到基于城镇的适当位置的年列表中。

如果月或周没有相应的值,我需要附加一个 0。理想情况下使其看起来像这样:

year = (('Adison', '355', 4, 2, 9), ('windsor windham', '455', 6, 0, 0), ('windham', '655', 2, 1, 0), ('btown', '233', 5, 0, 8))

我曾尝试将两个列表放在一个 for 循环中并使用 if in 条件来检查值,但我认为我忽略了一些东西并且出现了索引错误。我试过这样的事情:

for each in year:
    for part in month:
        if part in each:
            each.append(part[-1])
        else:
            each.append(0)

我知道必须有更好的方法,并且确实可以实现这一目标。有没有我应该研究的工具或模块?我玩过 zip 但因为它们的长度不一样,所以我遇到了麻烦。谢谢!

编辑

我知道我上面有元组,在我的代码中,在修改之前将它们全部转换为列表对象。我也在 Python 3.6

您可以从 monthweek 元组构建字典并从中获取值以创建新的 sub-元组附加新值。使用 dict.get(..., 0) 允许为没有月或周数据的城市设置默认值 0:

dct_mth = {k: v for k, _, v in month}
dct_week = {k: v for k, _, v in week}

year = list(year)  # make container mutable
for i, yr in enumerate(year):
    year[i] += (dct_mth.get(yr[0], 0), dct_week.get(yr[0], 0))

print(year)
# [('Adison', '355', 4, 2, 9), ('windsor windham', '455', 6, 0, 0), ('windham', '655', 2, 1, 0), ('btown', '233', 5, 0, 8)]

首先,从前两个元素中键入一个命令,使用零作为月份和星期的默认值。然后根据需要填写月和周:

data = {(name, n): [y, 0, 0] for name, n, y in year}

for name, n, m in month:
    data[name, n][1] = m

for name, n, w in week:
    data[name, n][2] = w

data = tuple(tuple([*k, *v]) for k, v in data.items())

这个有用吗?

year = [['Adison', '355', 4],['windsor windham', '455', 6],
        ['windham', '655', 2],['btown', '233', 5]]

month = [['Adison', 355, 2],['windham', '655', 1]]

week = [['btown', '233', 8],['Adison', '355', 9]]

for y in year:
  for m in month:
    if y[0] == m[0]:
      y.append(m[-1])
  for w in week:
    if y[0] == w[0]:
      y.append(w[-1])

for each in year:
  print(each)

['Adison', '355', 4, 2, 9]

['windsor windham', '455', 6]

['windham','655', 2, 1]

['btown', '233', 5, 8]

下面的代码首先将数据转换为字典,然后将月和周字典中的数据合并到年字典中。最后,它将组合数据转换为元组列表。

我正在使用字典,因为在字典中通过键查找项目比逐项扫描列表或元组以查找匹配项要快得多。当只有少数项目时,差别不大,但如果你的真实数据有几十个或数百个城镇,速度差异将是巨大的。

我假设城镇名称后面的数字是某种 ID 代码,这样我们就可以拥有多个名称相同但 ID 编号不同的城镇。因此我的字典使用名称和那个数字作为键。

year = (('Adison', '355', 4), ('windsor windham', '455', 6), ('windham', '655', 2), ('btown', '233', 5))
month = (('Adison', '355', 2), ('windham', '655', 1))
week = (('btown', '233', 8), ('Adison', '355', 9))

# Convert a nested tuple to a dict, using the first 2 fields 
# in the tuple as the key and the last field as the value.
def make_dict(seq):
    return {u[:-1]: [u[-1]] for u in seq}

year = make_dict(year)
month = make_dict(month)
week = make_dict(week)

# Add the month & week data to the year data
for k in year:
    year[k] += month.get(k, [0]) + week.get(k, [0])
print(year)

# Convert the updated year dict to a list of tuples
data = []
for k, v in year.items():
    data.append(k + tuple(v))
for row in data:
    print(row)

输出

{('Adison', '355'): [4, 2, 9], ('windsor windham', '455'): [6, 0, 0], ('windham', '655'): [2, 1, 0], ('btown', '233'): [5, 0, 8]}
('Adison', '355', 4, 2, 9)
('windsor windham', '455', 6, 0, 0)
('windham', '655', 2, 1, 0)
('btown', '233', 5, 0, 8)

如果你想让 data 成为一个元组的元组,就做 data = tuple(data)。 OTOH,dict 形式可能比列表更有用。