如何在循环中对文本中的数字进行排序

huangapple go评论67阅读模式
英文:

How to sequence numbers in text while looping

问题

我试图找到一种以特定方式打印数字的方法,但我无法做到,我的代码如下:

for i in range(7): # 7 可以是字符串的长度,例如
    print("letter"+str(i)+",letter"+str(i+1)+"|letter"+str(i+2)+",letter"+str(i+3)+";")

将打印出以下内容:

letter0,letter1|letter1,letter2;
letter1,letter2|letter2,letter3;
letter2,letter3|letter3,letter4;
letter3,letter4|letter4,letter5;
letter4,letter5|letter5,letter6;
letter5,letter6|letter6,letter7;
letter6,letter7|letter7,letter8;

我需要的输出如下:

letter0,letter1|letter2,letter3;
letter4,letter5|letter6,letter7;
letter8,letter9|letter10,letter11;
letter12,letter13|letter0,letter1;

并且letter0; 应始终是最后一个。

英文:

I'm trying to reach a way to print numbers in a specific way but I can't, my code here

for i in range(7): # 7 it could be a string length for an example
    print("letter"+str(i)+",letter"+str(i+1)+"|letter"+str(i+1)+",letter"+str(i+2)+";")

will print:

letter0,letter1|letter1,letter2;
letter1,letter2|letter2,letter3;
letter2,letter3|letter3,letter4;
letter3,letter4|letter4,letter5;
letter4,letter5|letter5,letter6;
letter5,letter6|letter6,letter7;
letter6,letter7|letter7,letter8;

What output I need is like this:

letter0,letter1|letter1,letter2;
letter2,letter3|letter3,letter4;
letter4,letter5|letter5,letter6;
letter6,letter7|letter7,letter8;
letter8,letter9|letter9,letter10;
letter10,letter11|letter11,letter12;
letter12,letter13|letter13,letter0;

and letter0; should be the last one always

答案1

得分: 1

result:

    letter0,letter1|letter1,letter2
    letter2,letter3|letter3,letter4
    letter4,letter5|letter5,letter6
    letter6,letter7|letter7,letter0
英文:
r = range(7)

for a, b in [(i, i + 1) for i in r][::2]:
    if b + 1 <= len(r):
        c = b + 1
    else:
        c = 0
    print(f"letter{a},letter{b}|letter{b},letter{c}")

result:

letter0,letter1|letter1,letter2
letter2,letter3|letter3,letter4
letter4,letter5|letter5,letter6
letter6,letter7|letter7,letter0

答案2

得分: 0

你可以在使用 print 时使用 end 参数。

where = 0
for i in range(14):
    for _ in range(2):
        where += 1
        print(i, "|", end = "", sep = "")
        if where == 4:
            where = 0
            print(";")
英文:

You can use the end argument when using print.

where = 0
for i in range(14):
    for _ in range(2):
        where += 1
        print(i, "|", end = "", sep = "")
        if where == 4:
            where = 0
            print(";")

答案3

得分: 0

也许类似这样的代码会起作用:

for i in range(7): # 7 可以是一个字符串的长度示例
    e = i + 1
    f = i + 2
    g = i
    if i > 0:
        g += i
        f += i
        e += i
        if f == 14:
            f = 0
    print(f"letter{g}, letter{e}|letter{e},letter{f};")
英文:

Maybe something like that would work:

for i in range(7): # 7 it could be a string length for an example
    e = i + 1
    f = i + 2
    g = i
    if i > 0:
        g += i
        f += i
        e += i
        if f == 14:
            f = 0
    print(f"letter{g}, letter{e}|letter{e},letter{f};")

答案4

得分: 0

使用字符串格式化增加可读性。使用模除运算修复最后迭代的索引。

n = 7

template = 'letter{},letter{}|letter{},letter{};'
counter = 0
for _ in range(n):
    s = template.format(counter, counter+1, counter+1, (counter+2) % (2*n))
    print(s)
    counter += 2
英文:

Add readability with a string formatter. Use modulo arithmetic to fix the index of the last iteration.

n = 7

template = 'letter{},letter{}|letter{},letter{};'
counter = 0
for _ in range(n):
    s = template.format(counter, counter+1, counter+1, (counter+2) % 2*n)
    print(s)
    counter += 2

huangapple
  • 本文由 发表于 2023年1月8日 22:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定