如何在嵌套列表中使用除列表和想要字符串之外的列表?

时间:2017-10-04 20:19:05

标签: python python-3.6

我有一些有效的代码,但我想使用ScoreList1作为嵌套列表的一部分,但要求的字符串不是列表。

我需要它来处理列表,因为我有一个附加到列表的输入。

ScoreList1= ['04', '05', '01', '07', '08']

nestedList = [["Judge","01","02","03","04","05"],
              ["Couple A","10","06","03","04","05"],
              ["Couple B","01","02","03","04","05"],
              ["Couple C","07","10","03","04","05"],
              ["Couple D","01","02","10","04","05"],]

for item in nestedList:
    print(
    ": "+item[0] + " "*(9-len(item[0]))+": "+
         item[1] + " "*(3-len(item[1]))+": "+
         item[4] + " "*(3-len(item[4]))+": "+
         item[2] + " "*(3-len(item[2]))+": "+
         item[3] + " "*(3-len(item[3]))+": "+
         item[5] + " "*(3-len(item[5]))+": ")

这是我的预期输出:

: Judge    : 01 : 04 : 02 : 03 : 05 : 
: Couple A : 10 : 04 : 06 : 03 : 05 : 
: Couple B : 01 : 04 : 02 : 03 : 05 : 
: Couple C : 07 : 04 : 10 : 03 : 05 : 
: Couple D : 01 : 04 : 02 : 10 : 05 : 

但是,我想要得分列表中的数字

编辑:

    ScoreList1= ['04', '05', '01', '07', '08']
ScoreList2= ['07', '02', '01', '02', '08']

nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"],
              ["Couple B","01","02","03","04","05"],
              ["Couple C","07","10","03","04","05"],
              ["Couple D","01","02","10","04","05"],]

for item in nestedList:
    row = item[:1] + ScoreList1 if item[0] == "Couple A" else item
    print(": {:<8} ".format(row[0])
          + "".join(": {:<2} ".format(field) for field in row[1:]))

在B夫妇旁边需要得分列表

编辑2:

    ScoreList1= ['04', '05', '01', '07', '08']
ScoreList2= ['07', '02', '01', '02', '08']
ScoreList3= ['02', '01', '01', '10', '08']
ScoreList4= ['01', '10', '02', '10', '09']
ScoreList5= ['02', '08', '01', '10', '01']
ScoreList6= ['01', '07', '01', '01', '01']

nestedListOfNames = [["Couple A"],
                     ["Couple B"],
                     ["Couple C"],
                     ["Couple D"],
                     ["Couple E"],
                     ["Couple F"]]
print(": Judge    : 01 : 02 : 03 : 04 : 05")
print("")
substitutions = {"Couple A": ScoreList1, "Couple B": ScoreList2, "Couple C": ScoreList3, "Couple D": ScoreList4, "Couple E" : ScoreList5, "Couple F" : ScoreList6}
with open("myfile.txt",'w') as outfile:
    for item in nestedListOfNames:
        row = item[:1] + substitutions.get(item[0], item[1:],)
        outfile.write(": {:<8} ".format(row[0])
        + "".join(": {:<2} ".format(field) for field in row[1:]))
outfile.close()

如何使用\ n打破文本文件的行?

2 个答案:

答案 0 :(得分:1)

最简单的方法是检查"Couple A"行,并在适当时使用ScoreList1代替其值:

ScoreList1= ['04', '05', '01', '07', '08']

nestedList = [["Judge",    "01", "02", "03", "04", "05"],
              ["Couple A", "10", "06", "03", "04", "05"],
              ["Couple B", "01", "02", "03", "04", "05"],
              ["Couple C", "07", "10", "03", "04", "05"],
              ["Couple D", "01", "02", "10", "04", "05"],]

for item in nestedList:
    row = item[:1] + ScoreList1 if item[0] == "Couple A" else item
    print(  ": " + row[0] + " "*(9-len(row[0]))
          + ": " + row[1] + " "*(3-len(row[1]))
          + ": " + row[2] + " "*(3-len(row[2]))
          + ": " + row[3] + " "*(3-len(row[3]))
          + ": " + row[4] + " "*(3-len(row[4]))
          + ": " + row[5] + " "*(3-len(row[5])))

由于您表示您现在希望按顺序打印每个子列表的元素,因此可以简化print()参数的构造:

for item in nestedList:
    row = item[:1] + ScoreList1 if item[0] == "Couple A" else item
    print(": {:<8} ".format(row[0])
          + "".join(": {:<2} ".format(field) for field in row[1:]))

要扩展此项以处理两个或更多替换,而可以执行以下操作:

ScoreList1= ['04', '05', '01', '07', '08']
ScoreList2= ['07', '02', '01', '02', '08']

for item in nestedList:
    row = (item[:1] + ScoreList1 if item[0] == "Couple A" else
           item[:1] + ScoreList2 if item[0] == "Couple B" else item) # etc, etc
    print(": {:<8} ".format(row[0])
          + "".join(": {:<2} ".format(field) for field in row[1:]))
然而,这种方法很容易变得笨拙,如果还有不止一些处理,它也变得相对缓慢 - 因此,使流程“由表驱动”会更好更快(使用已知的方法)作为Control Table)和写代码处理所有案例一次(而不是为每个案例写下它的小片段):

substitutions = {"Couple A": ScoreList1, "Couple B": ScoreList2}

for item in nestedList:
    row = item[:1] + substitutions.get(item[0], item[1:])
    print(": {:<8} ".format(row[0])
          + "".join(": {:<2} ".format(field) for field in row[1:]))

答案 1 :(得分:0)

尝试使用内置的zip功能

ScoreList1 = ['04', '05', '01', '07', '08']



nestedList = [["Judge", 1, 2, 3, 4, 5],
             ["Couple A", 10, 6, 3, 4, 5],
              ["Couple B", 1, 2, 3, 4, 5],
              ["Couple C", 7, 10, 3, 4, 5],
              ["Couple D", 1, 2, 10, 4, 5],]

for item, score in zip(nestedList, ScoreList1):
    print(": {:9} : {:02d} : {:02d} : {:02d} : {:02d} : {:02d} : {}".format(item[0], 
                                                                            item[1], 
                                                                            item[4], 
                                                                            item[2], 
                                                                            item[3], 
                                                                            item[5], 
                                                                            score))