簡體   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