我想把几个列表写成字典。 powerlist是24个条目的元组列表(8个)。 placelist是一个包含8个条目的列表。 现在我只在字典中获得第一组powerlist数据。
for i in self.powerlist:
self.dictionary = {}
self.dictionary = dict(zip(self.placelist, self.powerlist))
我希望字典显示如下:
Placelist1: (powerlistuple1, powerlist tuple9, powerlist tuple17),
placelist2: (powerlisttupe2, powerlist tuple10, powerlist tuple 18), etc etc
我该怎么做?我尝试使用self.powerlist(i)
运行上面的代码,但它给了我一个类型错误。我该怎么做?
powerlist[0]
[(0, 0, 0, 0, 0, 0, 0, 17, 34, 51), ...for 24]
placelist[0]
["A place", "A different place", ..etc for 8]
答案 0 :(得分:0)
self.dictionary = {}
for ii, place in enumerate(self.placelist):
self.dictionary[place] = (self.powerlist[ii],
self.powerlist[ii + 8], self.powerlist[ii + 16])
答案 1 :(得分:0)
你在循环中一遍又一遍地设置self.dictionary
同样的东西,你需要这样的东西:
self.dictionary = {}
for ind,tup in enumerate(self.placelist):
self.dictionary["Placelist{}".format(ind)] = (self.powerlist[:ind+8], tup)
答案 2 :(得分:0)
我可以在这里看到一个模式,你每次都会添加8个项目(i + 8)* j
# generate an example
l= [x for x in range(100)]
res= [[y for y in l if y%8==i] for i in range(8)]
print res
<强>输出:强>
res = [[0,8,16,24,32,40,48,56,64,72,80,88,96],[1,9, 17,25,33,41,49,57,65,73,81,89,97],[2,10,18,26,34, 42,50,58,66,74,82,90,98],[3,11,19,27,35,43,51,59,67, 75,83,91,99],[4,12,20,28,36,44,52,60,68,76,84,92], [5,13,21,29,37,45,53,61,69,77,85,93],[6,14,22,30,38, 46,54,62,70,78,86,94],[7,15,23,31,39,47,55,63,71,79, 87,95]]