[英]Having multiple objects in a dictionary
I have a couple of lists which I want to make into a dictionary. 我有几个要编入字典的清单。 The powerlist is a list of tuples(8long) for 24 entries.
电源列表是24个条目的元组(8long)的列表。 placelist is a list with 8 entries.
placelist是一个包含8个条目的列表。 Right now i'm only getting the first set of powerlist data in the dictionary.
现在,我只在字典中获得第一组Powerlist数据。
for i in self.powerlist:
self.dictionary = {}
self.dictionary = dict(zip(self.placelist, self.powerlist))
I want the dictionary to display like this: 我希望字典显示如下:
Placelist1: (powerlistuple1, powerlist tuple9, powerlist tuple17),
placelist2: (powerlisttupe2, powerlist tuple10, powerlist tuple 18), etc etc
How do I do this? 我该怎么做呢? I have tried running the above code with
self.powerlist(i)
but it gives me a type error. 我尝试使用
self.powerlist(i)
运行上面的代码,但是它给了我一个类型错误。 How would I do this? 我该怎么做?
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])
You keep setting self.dictionary
to the same thing over and over in your loop, you need something like: 您不断在循环
self.dictionary
设置为相同的内容,您需要以下内容:
self.dictionary = {}
for ind,tup in enumerate(self.placelist):
self.dictionary["Placelist{}".format(ind)] = (self.powerlist[:ind+8], tup)
I can see a pattern here, you are adding 8 items every-time (i+8)*j 我可以在这里看到一个模式,您每次要添加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
Output: 输出:
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]]
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.