简体   繁体   English

字典中有多个对象

[英]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.

相关问题 如何从具有多个对象的python字典中获取重复对象和相应键的总数? - How to get total number of repeated objects and respective keys from a python dictionary having multiple objects? Python将多个对象变成字典 - Python turning multiple objects into a dictionary Python 中的字典中的一个键具有多个值 - Having Multiple Values to One Key in a Dictionary in Python python字典,其值具有多个参数 - python dictionary with value having multiple parameters 使用 for 循环制作具有多个键的字典 - Making a dictionary having multiple keys using for loop Python - 将多个 Pickle 对象加载到单个字典中 - Python - Load multiple Pickle objects into a single dictionary 在 p 中的 QTablewidget 中插入具有单键多值的字典 - Insert dictionary having single key multiple value in QTablewidget in p 通过拥有多个列表将数据框转换为字典? - Converting data-frame into a dictionary by having the multiple lists? Python Flask:具有多个定义或将所有内容存储在字典中 - Python Flask: Having multiple definitions or storing everything in a dictionary Python:具有多个值的键的字典。 如何将字典打印为分隔值列表的字典集 - Python: dictionary with keys having multiple values. How to print dictionary as set of dictionaries separating the list of values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM