将多个变量结果组合到Python中的单个列表中

时间:2015-04-14 02:48:52

标签: python list variables

我有一个名为记录的列表,其中包含记录[1]中的mw值。然后我用四个if语句测试这些mw值,并根据mw的值更改输出结果,如下所示。

mw = records[1]
if mw <= 100:
  output1 =(records[1],"valueA", records[4], "moreItems")
if 100< mw <= 300:
  output2 =("records[1]","valueB", records[4], "moreItems")
if 300< mw <= 600:
 output3 =("records[1]","valueC", records[4], "moreItems")
if mw > 600:
  output4 =("records[1]","valueD", records[4] , "moreItems")

if语句背后的主要思想是我需要根据mw值更改一些列表值。这意味着output1变量与output2略有不同。

如何将这四种输出结合起来?理想情况下,我希望输出1的所有结果先是2然后是3,如下所示。

98.7,valueA,False,moreItems  #from output1
50,valueA,False,moreItems    #from output1
210,valueB,True,moreItems    #From output2
400,valueC,True,moreItems    #From output3
498,valueC,True,moreItems    #From output3
580,valueC,True,moreItems    #From output3
800,valueD,False,moreItems   #From output4

2 个答案:

答案 0 :(得分:1)

我不知道你是如何只用4个变量获得所有输出的。或者用另一种方式,使用大量编号的兄弟&#34;变量只会让你头疼 - 你需要一个可以反复调用的函数。类似的东西:

def process_record(record):
    mw = record[1]
    if mw <= 100:
        output = (record[1], "valueA", record[4], "moreItems")
    elif 100 < mw <= 300:
        output = (record[1], "valueB", record[4], "moreItems")
    elif 300 < mw <= 600:
        output = (record[1], "valueC", record[4], "moreItems")
    elif 600 < mw:
        output = (record[1], "valueD", record[4], "moreItems")
    else:
        raise ValueError("Illegal record: %s" % repr(record))
    return output

这将返回您分配给output1output2等的相同元组,但可以根据需要多次使用。 (请注意,由于它一次只对一个记录进行操作,因此我将其名称及其参数更改为单数&#34;记录&#34;。然后我将单独的if语句更改为系列{{1带有默认情况的语句。)

你的输出元组以数字开头......并且在Python元组中自动按第一个元素排序,然后按第二个元素排序等等。所以你可以使用内置的sorted函数产生你想要的输出:

elif

如果在您的真实代码中,您没有偶然位于元组开头的数字,或者您的价值类别字符串(def main(): records = [ # You know how you're acquiring these records; I don't. ] processed_records = [process_record(record) for record in records] for record in sorted(processed_records): print(",".join(str(dat) for dat in record)) return 等)不会发生要按字母顺序对数字进行排序,你仍然可以使用"valueA",但是你必须为它提供一个关键功能来产生你想要的排序。

键功能会自动应用于要排序的列表的每个元素,将其转换为更容易排序的内容,通常是简单的整数或字符串。如果您不关心数字顺序,您可以返回sorted - 或 - 任意字符串。下面我的例子对该字符串进行排序,然后是数字,然后是其他所有字符串,因为我无法判断其他字段是否重要。

"valueA"

现在,你只需要使用它:

def processed_record_key(record):
    int_val, value_category, unknown_boolean, unknown_string = record
    return value_category, int_val, unknown_boolean, unknown_string

检查Sorted HowTo以获取更多排序技巧。

最后,我建议您考虑使用namedtuple作为数据记录 - 它会使您的元组(以及使用它们的代码)更具自我描述性并且更易于使用。

答案 1 :(得分:0)

由于您希望按顺序输出,我不认为在一个循环中很容易做到这一点 - 它可能最好有四个独立的循环。这可能是list comprehensions的好地方。我们可以生成四个列表,每个列表对应一种输出格式,然后将它们粘合在一起并返回合成列表。假设您有一个您正在阅读的记录列表:

#records_list is a list of records defined somewhere above

output1_list = [(records[1],"valueA", records[4], "moreItems") for records in records_list if records[1] <= 100]
output2_list = [(records[1],"valueB", records[4], "moreItems") for records in records_list if 100 < records[1] <= 300]
output3_list = [(records[1],"valueC", records[4], "moreItems") for records in records_list if 300 < records[1] <= 600]
output4_list = [(records[1],"valueD", records[4], "moreItems") for records in records_list if 600 < records[1]]

final_output = output1_list + output2_list + output3_list + output4_list

# Later on, print all the results
for output in final_output:
    print output
相关问题