简体   繁体   English

如何从一个ReportLab表的python中的元组列表中提取元素?

[英]How to extract elements from a list of tuples in python for a ReportLab table?

I have a problem with creating a reportlab table containing elements from a list of tuples. 我在创建一个包含元组列表中的元素的reportlab表时遇到问题。

Having the input: 有输入:

meta= [('#Instances (Test)', '250'), ('#Instances (Train)', '250')]

I intuitively thought of writing it that way: 我凭直觉想到了这样写:

for key, value in meta:
    data = [['Solver', '%s'%(solver_name)],
             ['%s'%(key), '%s'%(value)],
              ['%s'%(key), '%s'%(value)]]
meta_data = Table(data, colWidths=None, rowHeights=None, style=None, splitByRow=1,
                  repeatRows=0, repeatCols=0)

But it only considers the last tuple making ('#Instances (Train)', '250') appear in both rows. 但它仅考虑最后一个元组('#Instances (Train)', '250')出现在两行中。 Any ideas on what I did wrong? 关于我做错了什么的任何想法?

You are only getting the last key, value from your input because every time in the loop you are changing the whole data variable. 您仅从输入中获取最后一个key, value ,因为每次循环时,您都在更改整个data变量。 What you have meant is propably this 你的意思可能是这个

data = []
for key, value in meta:
    data.append([['Solver', solver_name],[key, value]])
meta_data = Table(data, colWidths=None, rowHeights=None, style=None, \
  splitByRow=1,repeatRows=0, repeatCols=0)

In the above code, I initialize data variable as an empty list, then go through each tuple in meta, assigning that tuple[0] as key and tuple[1] as value . 在上面的代码中,我将data变量初始化为一个空列表,然后遍历meta中的每个元组,并将该tuple[0]分配为key并将tuple[1]分配为value The only thing that is done with those variables is we append them to the list we initialized at the start. 这些变量唯一要做的就是将它们附加到开始时初始化的列表中。

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

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