2017-07-25 92 views
2

我有一个数据库查询返回三组数据。它采用这种格式。结合不同长度的多个列表中的数据

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

+0

你在哪个python版本上? –

+0

我正在使用3.6。 – Joe

+0

Python元组是不可变的,所以他们没有'.append'方法。你必须做一些像'new_tuple = old_tuple +('more','data')' –

回答

0

首先,一键关字典前两个元素,采用零为默认月份和星期。然后填写月份和星期,如有必要:

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()) 
+1

工作完美Thankyou @wim – Joe

2

你可以建立从monthweek元组类型的字典,并获取从这些值创建新元组追加新的值。使用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)] 
+0

我不知道我在这里弄错了但它只是将0附加到每个不实际使用值更新的列表 – Joe

+0

@Joe确保字典键实际上包含与“year”中相同的字符串。 –

0

这个有用吗?

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) 

[ '笛声', '355',4,2,9]

[ '温莎Windham的', '455',6]

[ 'Windham的',” 655' ,2,1]

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

0

下面第一代码转换的数据类型的字典,并然后组合数据从&这一个月的词典到今年的词典。最后,它将组合数据转换为元组列表。

我使用的是字典,因为通过字典中的按键查找项目要比通过列表或元组逐条扫描查找匹配的速度快得多。只有少数几个项目没有太大区别,但如果您的真实数据有几十个或几百个城镇,则速度差异会很大。

我假设城镇名称后面的数字是某种类型的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,字典形式可能比列表更有用。

相关问题