如何在不包含列表且需要字符串的嵌套列表中使用列表?

How to use a list in a nested list that don't except list and wants string?

我有这段有效的代码,但我想使用 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 : 

但是线对 a 在哪里我想要 scorelist1

中的数字

已编辑:

    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 旁边需要 scorelist2

编辑 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 为文本文件分行?

尝试使用内置的 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)) 

最简单的方法是只检查 "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:]))

但是,这种方法很容易变得笨拙,而且如果要处理的数量超过几个,也会变得相对缓慢 — 因此,使过程 "table-driven"(使用什么被称为 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:]))