【问题标题】:Python print format of integers and string mix整数和字符串混合的Python打印格式
【发布时间】:2020-12-17 15:13:08
【问题描述】:
line = [1,2,3,4,5,6,7,9,10,11,12,14,15,16,17,18,19,300]


GroupCount = 0
FormatDesignators =line[0]

for i in range(1 ,len(line)):
    if line[i] != line[i - 1] + 1:
        if GroupCount >= 1:
            FormatDesignators = FormatDesignators,'-', line[i - 1]
            FormatDesignators = FormatDesignators,',',line[i]
            GroupCount = 0
    else:
        GroupCount = GroupCount + 1
print(FormatDesignators)


if GroupCount >= 1:
    FormatDesignators = FormatDesignators,"-",line[i]
    print (FormatDesignators)

现在正在输出:

(((((((1, '-', 7), ',', 9), '-', 12), ',', 14), '-', 19), ',', 300) 我希望它是 1-7,9-12,14-19,300

【问题讨论】:

    标签: python python-3.x printing format


    【解决方案1】:

    试试这个

    line = [1,2,3,4,5,6,7,9,10,11,12,14,15,16,17,18,19,300]
    
    
    GroupCount = 0
    FormatDesignators =line[0]
    
    for i in range(1 ,len(line)):
        if line[i] != line[i - 1] + 1:
            if GroupCount >= 1:
                FormatDesignators = f'{FormatDesignators} - {line[i - 1]},'
                FormatDesignators = f'{FormatDesignators} {line[i]}'
                GroupCount = 0
        else:
            GroupCount = GroupCount + 1
    print(FormatDesignators)
    
    
    if GroupCount >= 1:
        FormatDesignators = f'{FormatDesignators} - {line[i]},'
        print (FormatDesignators)
    

    【讨论】:

    • 此代码有效 - 适用于原始“作业”(问题)。但是,它不是非常 Pythonic 并且没有使用 Python 的最佳特性。我觉得很有可能不是学习语言的好方法。 (只是我的 .02 美元!)
    • 我懒得更改代码只是想获得正确的输出语法,所以只是编辑了原始代码以确保其工作
    • 无意冒犯。就从“学习的角度”来说吧。那是给原作者的。在学习一门新语言时,最好选择“最好的”功能来完成这项工作。毕竟,它有很多“电池”;-))
    • 谢谢,我是新手,但如果你们有更好的解决方案,请继续。
    【解决方案2】:

    这对您有帮助吗:(最后,您可以根据自己的喜好修改最终的连接格式...)

    
        >>> line = [1,2,3,4,5,6,7,9,10,11,12,14,15,16,17,18,19,300]
        >>> starts = [x for x in line if x-1 not in line]
        >>> ends = [y for y in line if y+1 not in line]
        >>> ranges = list((a,b) for a, b in zip(starts, ends))
        >>> ranges
        [(1, 7), (9, 12), (14, 19), (300, 300)]
        >>> results = [str(a)+'-'+str(b)  for a, b in ranges]
    

    【讨论】:

      【解决方案3】:

      以下方法似乎有效。我试图做一些只遍历数据一次的东西:

      c = 0
      result = ''
      current = None
      run = False
      
      while c < len(line):
      
          if current == None:
              result += f'{line[c]}'
          elif line[c] - current == 1:
              run = True
              if c == len(line) - 1:
                  result += f'-{line[c]}'
          elif run:
              result += f'-{current},{line[c]}'
              run = False
          else:
              result += f',{line[c]}'
      
          current = line[c]
          c += 1
      
      print(result)
      # 1-7,9-12,14-19,300
      

      第一个if 检查是否没有看到任何数字,即仅针对第一个数字。第二个if 指出当前整数比前一个大一,也处理到达line 的末尾并继续序列的情况。第三个指出一个序列已被破坏,并按此添加输出。最后一个else 块在非序列之后添加当前值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-15
        • 2016-01-20
        • 2016-06-21
        • 2018-02-17
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        相关资源
        最近更新 更多