如何在嵌套列表中使用除了列表之外不需要字符串的列表?



我有可行的代码,但我想将 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:]))

需要Scorelist2近对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来分解文本文件的行?

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

然而,这种方法很容易变得笨拙,并且如果要处理的是多个,也会变得相对较慢 - 因此,将过程"表驱动"变得更快,更快(使用已知的内容作为控制表)和写代码处理所有情况(与每个单独情况的相反,编写小片段):

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:]))

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

最新更新