【问题标题】:Why does my program add additional an additional blank space at the beginning of the row?为什么我的程序在行首添加了额外的额外空格?
【发布时间】:2020-12-02 22:28:50
【问题描述】:

您好,希望您能帮到我,

我正在尝试制作一种算法,该算法采用包含名称列表的 txt 文件,随机重新排序并将它们分配给团队。

它有效,我唯一的问题是由于某种原因它在列表的所有行中添加了一个额外的空间,除了第一个元素。不是 txt 文件,那里的列表很好。

例如,我想要这样的输出:

team 1
dude1
dude2
team 2
dude3
dude4

而是这样做:

team 1
 dude1
 dude2
 team 2
 dude3
 dude4

所以第 1 队在没有空间的情况下进行,但其他所有东西在开始时都有空间。

代码如下:

import random

raw_list = open("list.txt","r")

people_list = []
for line in raw_list.readlines():
    people_list.append(line)
    
raw_list.close()

team_list = []

team_num = int(input("enter the number of teams you want "))

len_people = int(len(people_list))
teams = len_people / team_num
len_people=int(len_people+team_num)

j = 1
i = 1
for n in range(len_people):
    if i == 1:
        if j>team_num:
            popper = random.randint(0, (len(people_list)) - 1)
            team_list.append(people_list.pop(popper))
            
        else:
            team_list.append(f"Team {j}\n")
            i = i + 1
            
    elif i < teams:
        i=i+1
        popper = random.randint(0, (len(people_list)) - 1)
        team_list.append(people_list.pop(popper))
        
    else:
        i = 1
        j = j + 1
        popper = random.randint(0, (len(people_list)) - 1)
        team_list.append(people_list.pop(popper))
           
    
print (*team_list)

谢谢

【问题讨论】:

  • 请提供预期的minimal, reproducible example。显示中间结果与您的预期不同的地方。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。
  • 张贴你的整个节目是多余的。格式化输出描述以显示错误。您当前的代码失败,因为我们没有您的输入文件。通过在发布的示例中简单地对测试数据进行硬编码来替换该输入。
  • 你确定你用你的代码创建了你的例子吗?该示例包含小写的“团队”,但包含大写的代码。

标签: python list spaces txt


【解决方案1】:

print(*team_list) 就像将team_list 的每个元素作为位置参数传递。默认情况下,print 在每个参数之间放置一个空格。将其更改为 print(*team_list, sep='') 以取消默认的空格分隔符。

【讨论】:

  • 太棒了!不知道。谢谢!
【解决方案2】:

当您在对print() 的一次调用中打印多个值时,它会在它们之间放置一个空格。例如。如果你写

a = "abc"
b = "def"
print(a, b)

打印出来

abc def

因此,当您致电 print(*team_list) 时,它会在每个团队成员之间放置一个空格。

您可以使用sep 参数来更改此设置:

print(team_list, sep="")

【讨论】:

    【解决方案3】:

    如果在从文件中读取名称时使用 strip 从名称中删除换行符,格式化会更容易。一般来说,如果您将数据与您想要的显示方式分开,那么生活会更轻松;稍后添加格式很容易,但是当它全部混合到所有内容中时很难删除/修改它。

    import random
    
    # Get complete list of people.
    with open("list.txt", "r") as raw_list:
        people_list = [line.strip() for line in raw_list.readlines()]
    
    # Build list of (empty) teams.
    team_list = [[] for _ in range(int(
        input("enter the number of teams you want ")
    ))]
    
    # Shuffle people and assign them to each team in turn.
    random.shuffle(people_list)
    for i, person in enumerate(people_list):
        team_list[i % len(team_list)].append(person)
    
    # Output.
    for i, team in enumerate(team_list):
        print(f"Team {i+1}")
        for person in team:
            print(person)
    

    打印:

    enter the number of teams you want 2
    Team 1
    dude2
    dude3
    Team 2
    dude4
    dude1
    

    每个team 仅包含一个名称列表,没有额外的换行符等,这意味着如果您决定要更改显示团队的方式,这非常简单:

    for i, team in enumerate(team_list):
        print(f"Team {i+1}:", ", ".join(team))
    
    enter the number of teams you want 2
    Team 1: dude2, dude3
    Team 2: dude1, dude4
    

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多