基于模板合并列表的多个列表

时间:2017-10-16 15:42:34

标签: python python-3.x list tuples

我有3个DB调用返回一个元组元组,其名称,代码和计数如下:

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1))

month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))

week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))

我需要将它们合并在一起,以便它们具有年度,月度和周度的名称,代码和计数。

我的问题是,如果没有记录,我需要在AND和0中插入名称和代码进行计数。最终产品应该是这样的:

result = (('Windham', '0905', 1, 1, 0), ('Windsor Windham', '7852', 0, 0, 0), ('Washington', '3292', 0, 0, 0),
            ('Orleans Essex', '44072', 1, 1, 0), ('Addison', '2853', 0), ('Bennington', '3778', 0),
            ('Fanklin Grand Isle', '5560', 0, 0 0), ('Caledonia', '1992', 0, 0, 0),
            ('Rutland', '2395', 1, 0, 0), ('Chittendon', '3367', 1, 1, 0), ('Lamoille', '5229',0, 0 0))

我试图嵌套一个循环来检查数据库调用和模板中是否存在该名称。如果是将DB值附加到列表中,如果没有追加0

i = 0
for p in newlist:
    try:
        if p[0] == mlist[i][0]:
            print("HERE: {} {}".format(p[0], mlist[i][0]))
            p.append(mlist[i][-1])
            i += 1
        else:
            p.append(0)
    except IndexError:
        continue

这是附加DB值但不是零。我相信必须有一个更好的方法来实现这一目标并使其真正发挥作用。

修改

以下是根据收到的答案更新的代码。对我来说,它仍然用0替换每个year值。

数据:

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '0905', 0), ('Windsor Windham', '7852', 0), ('Washington', '3292', 0), ('Orleans Essex', '44072', 0), ('Chittendon', '18028633367', 1), ('Addison', '12853', 0), ('Bennington', '3778', 0), ('Caledonia', '11992', 0), ('Rutland', '1895', 0), ('Chittendon', '18367', 0), ('Lamoille', '1809', 0), ('Windham', '180905', 0), ('Windsor Windham', '180852', 0), ('Waston', '18022623292', 0), ('Orleans Essex', '18072', 0), ('Addison', '1853', 0), ('Bennington', '1778', 0), ('Fanklin Grand Isle', '18560', 0), ('Caledonia', '180292', 0), ('Rutland', '195', 0), ('Lamoille', '18229', 0))

month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '18028633367', 1))

week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '18367', 1))

代码:

from collections import defaultdict
joined_data = defaultdict([0, 0, 0].copy)

for entry in year:
    # we create the default entry by calling the defaultdict with a key
    # and immediately grab the newly created list
    count = joined_data[(entry[0],entry[1])]
    # we swap *inplace* the value given by the DB query
    count[0] = entry[2]

# rinse and repeat with the rest of the data
for entry in month:
    count = joined_data[(entry[0], entry[1])]
    count[1] = entry[2]

for entry in week:
    count = joined_data[(entry[0], entry[1])]
    count[2] = entry[2]

# Finally we format the data to the required format
result = tuple(key+tuple(value) for key,value in joined_data.items() )
print(result)

结果:

(('Fanklin Grand Isle', '5560', 0, 1, 1), ('Windham', '0905', 0, 0, 0), ('Windsor Windham', '7852', 0, 0, 0), ('Washington', '3292', 0, 0, 0), ('Orleans Essex', '1072', 0, 0, 0), ('Chittendon', '13367', 0, 1, 1), ('Addison', '2853', 0, 0, 0), ('Bennington', '1878', 0, 0, 0), ('Caledonia', '1992', 0, 0, 0), ('Rutland', '2395', 0, 0, 0), ('Lamoille', '5229', 0, 0, 0))

2 个答案:

答案 0 :(得分:0)

我不确定你理解你想要实现什么,因为你的示例输入与你的输出不匹配,但我认为你可以使用列表推导来构造结果,检查这些项是否在{{1 }},yearsmonths列表,分别添加weeks1

0

如果这些计数实际上与>>> year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1)) >>> month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) >>> week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) >>> [(x[0], x[1], int(x in year), int(x in month), int(x in week)) for x in set(year + week + month)] [('Chittendon', '3367', 0, 1, 1), ('Windham', '3457', 1, 0, 0), ('Fanklin Grand Isle', '5560', 1, 1, 1)] 不同,您应首先创建一些字典,将城市映射到各自的年/月/周计数,然后使用与上面类似的列表推导:

1

答案 1 :(得分:0)

以下是使用defaultdict处理问题的方法,以避免丢失丢失的条目:

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1))
month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))
week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))

#I'm using a defaultdict to deal with the missing entries
from collections import defaultdict
joined_data = defaultdict([0,0,0].copy)

for entry in year:
    #we create the default entry by calling the defaultdict with a key
    #and immediately grab the newly created list
    count = joined_data[(entry[0],entry[1])]
    #we swap *inplace* the value given by the DB query
    count[0] = entry[2]

#rinse and repeat with the rest of the data
for entry in month:
    count = joined_data[(entry[0],entry[1])]
    count[1] = entry[2]

for entry in week:
    count = joined_data[(entry[0],entry[1])]
    count[2] = entry[2]

#Finally we format the data to the required format
result = tuple(key+tuple(value) for key,value in joined_data.items() )
print(result)

输出:

>>>(('Chittendon', '3367', 0, 1, 1), ('Fanklin Grand Isle', '5560', 1, 1, 1), ('Windham', '3457', 1, 0, 0))