繁体   English   中英

字典中有多个对象

[英]Having multiple objects in a dictionary

我有几个要编入字典的清单。 电源列表是24个条目的元组(8long)的列表。 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]
self.dictionary = {}
for ii, place in enumerate(self.placelist):
    self.dictionary[place] = (self.powerlist[ii],
            self.powerlist[ii + 8], self.powerlist[ii + 16])

您不断在循环self.dictionary设置为相同的内容,您需要以下内容:

  self.dictionary = {}  
  for ind,tup in enumerate(self.placelist):
        self.dictionary["Placelist{}".format(ind)] = (self.powerlist[:ind+8], tup)

我可以在这里看到一个模式,您每次要添加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]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM