带有多个项目/请求的Python输入

发布于 2025-02-13 03:14:45 字数 619 浏览 0 评论 0原文

我是在Python中编码的新手,我被困在以下内容:

我有一个作为输入的项目列表,该项目被空的newlines分开,例如:

input = """Andrew
Bob
Chanel
Danielle

Andrew Chanel 1 0
Danielle Bob 0 1

Andrew Danielle 0.5 0.5
Chanel Bob 1 0

Andrew Bob 0.5 0.5
Chanel Danielle"""

在此输入中,前4行应被视为名称在分类中的所有玩家中,称为结果,带有名为名称的字符串。但是,在第一个空的新线之后,它给出了两个玩家的名称,其次是他们的结果(1 = win,0 =损失,均为0.5 = tie)。每个新线都是一场新的战斗,直到新的空的 newline是一轮。

我的问题:我需要具体说明哪些步骤:

  1. 所有参与者都是空的新线之前的第一部分?
  2. 之后的“段落”是回合?
  3. 名字是“ player_1”,第二个名称'player_2',第一个值'score_player_1'和第二个值'score_player_2'?

如果有人愿意帮助初学者,我很感激!

I'm new to coding in Python and I am stuck on the following:

I have a list of items as inputs, which is seperated by empty newlines, like so:

input = """Andrew
Bob
Chanel
Danielle

Andrew Chanel 1 0
Danielle Bob 0 1

Andrew Danielle 0.5 0.5
Chanel Bob 1 0

Andrew Bob 0.5 0.5
Chanel Danielle"""

In this input, the first 4 lines should be the seen as the names of all players in a classed called result with a string called name. After the first empty newline, however, it gives the names of the two players followed by their result (1 = win, 0 = loss, both 0.5 = tie). Every newline is a new battle, and until a new empty newline is one round.

My question: what are the steps that I need to specify that:

  1. the first part before an empty newline is all players involved?
  2. the 'paragraphs' afterwards are rounds?
  3. that the first name is 'player_1', the second name 'player_2', the first value 'score_player_1' and the second value 'score_player_2'?

If anyone would be so kind to help a beginner out, I' appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2025-02-20 03:14:45

您可以使用以下方式:

x = """Andrew
Bob
Chanel
Danielle

Andrew Chanel 1 0
Danielle Bob 0 1

Andrew Danielle 0.5 0.5
Chanel Bob 1 0

Andrew Bob 0.5 0.5
Chanel Danielle 0 1"""


_names, *rounds = x.split('\n\n')
names = _names.splitlines() # ['Andrew', 'Bob', 'Chanel', 'Danielle']


for _round in rounds:
    print('NEW ROUND')
    for match in _round.splitlines():
        # Use these variables:
        name1, name2, score1, score2 = match.split()
        print('MATCH:', name1, 'vs', name2, '| SCORE:', score1, '-', score2)
    print('')

输出:

NEW ROUND
MATCH: Andrew vs Chanel | SCORE: 1 - 0
MATCH: Danielle vs Bob | SCORE: 0 - 1

NEW ROUND
MATCH: Andrew vs Danielle | SCORE: 0.5 - 0.5
MATCH: Chanel vs Bob | SCORE: 1 - 0

NEW ROUND
MATCH: Andrew vs Bob | SCORE: 0.5 - 0.5
MATCH: Chanel vs Danielle | SCORE: 0 - 1

You can use this:

x = """Andrew
Bob
Chanel
Danielle

Andrew Chanel 1 0
Danielle Bob 0 1

Andrew Danielle 0.5 0.5
Chanel Bob 1 0

Andrew Bob 0.5 0.5
Chanel Danielle 0 1"""


_names, *rounds = x.split('\n\n')
names = _names.splitlines() # ['Andrew', 'Bob', 'Chanel', 'Danielle']


for _round in rounds:
    print('NEW ROUND')
    for match in _round.splitlines():
        # Use these variables:
        name1, name2, score1, score2 = match.split()
        print('MATCH:', name1, 'vs', name2, '| SCORE:', score1, '-', score2)
    print('')

Output:

NEW ROUND
MATCH: Andrew vs Chanel | SCORE: 1 - 0
MATCH: Danielle vs Bob | SCORE: 0 - 1

NEW ROUND
MATCH: Andrew vs Danielle | SCORE: 0.5 - 0.5
MATCH: Chanel vs Bob | SCORE: 1 - 0

NEW ROUND
MATCH: Andrew vs Bob | SCORE: 0.5 - 0.5
MATCH: Chanel vs Danielle | SCORE: 0 - 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文