Python:打印列表并排

时间:2016-12-21 17:04:51

标签: python

我想知道如何打印两个相同尺寸的列表。

我从嵌套的词典中得到了这些列表。

以下是清单:

def convolution(self):
    result = np.zeros((self.mat_width, self.mat_height))
    print(self.mat_width)
    print(self.mat_height)

    for i in range(0, self.mat_width-self.window_width):
        for j in range(0, self.mat_height-self.window_height):
            # deflate both mat and mask 
            # if j+self.window_height >= self.mat_height:
            #   row_index = j+self.window_height + 1
            # else:

            row_index = j+self.window_height                
            col_index = i+self.window_width 

            mat_masked = self.mat[j:row_index, i:col_index]
            # pixel position 
            index_i = i + int(self.window_width / 2) 
            index_j = j + int(self.window_height / 2) 


            prod = np.sum(mat_masked*self.mask)


            if prod >= 255:
                result[index_i, index_j] = 255
            else:
                result[index_i, index_j] = 0

    return result

基本上,我希望列表为:

切尔西:44 利物浦:37

等...

这是我的python代码:

[
  [
    "Chelsea", 
    "Liverpool", 
    "ManCity", 
    "Arsenal", 
    "Spurs", 
    "ManU", 
    "Southampton", 
    "West Bromwich", 
    "Everton", 
    "Bournemouth", 
    "Stoke", 
    "Watford", 
    "West Ham", 
    "Middlesbrough", 
    "Foxes", 
    "Burnley", 
    "Crystal", 
    "Sunderland", 
    "Swans", 
    "Hull"
  ], 
  [
    43, 
    37, 
    36, 
    34, 
    33, 
    30, 
    24, 
    23, 
    23, 
    21, 
    21, 
    21, 
    19, 
    18, 
    17, 
    17, 
    15, 
    14, 
    12, 
    12
  ]
]

我尝试使用%进行格式化,但没有得到任何内容!

1 个答案:

答案 0 :(得分:4)

您可以在此处使用zip

#               v  unpack content of list
for a, b in zip(*my_list):
    print '{}: {}'.format(a, b)

其中my_list是问题中提到的列表。要以dict的形式映射这些条目,您必须将zip ed值类型转换为dict

dict(zip(*my_list))

将返回:

{'ManU': 30, 'Liverpool': 37, 'Burnley': 17, 'West Bromwich': 23, 'Middlesbrough': 18, 'Chelsea': 43, 'West Ham': 19, 'Hull': 12, 'Bournemouth': 21, 'Stoke': 21, 'Foxes': 17, 'Arsenal': 34, 'Southampton': 24, 'Watford': 21, 'Crystal': 15, 'Sunderland': 14, 'Everton': 23, 'Swans': 12, 'ManCity': 36, 'Spurs': 33}