Python,如何使用格式正确缩进我的结果?

时间:2017-04-29 05:27:31

标签: python

目前我的结果显示如下:

||

我用来打印的代码是:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" type="text/css" href = "https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/semantic.min.css">
    <link rel="stylesheet" type="text/css" href = "https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/components/form.css">
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.js"> </script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/components/form.js"> </script>
  </head>
  <body>
  <script>
  $('.ui.form')
    .form({
      fields: {
        name: {
          identifier: 'name',
          rules: [
            {
              type   : 'empty',
              prompt : 'Please enter your name'
            }
          ]
        },
        skills: {
          identifier: 'skills',
          rules: [
            {
              type   : 'minCount[2]',
              prompt : 'Please select at least two skills'
            }
          ]
        },
        gender: {
          identifier: 'gender',
          rules: [
            {
              type   : 'empty',
              prompt : 'Please select a gender'
            }
          ]
        },
        username: {
          identifier: 'username',
          rules: [
            {
              type   : 'empty',
              prompt : 'Please enter a username'
            }
          ]
        },
        password: {
          identifier: 'password',
          rules: [
            {
              type   : 'empty',
              prompt : 'Please enter a password'
            },
            {
              type   : 'minLength[6]',
              prompt : 'Your password must be at least {ruleValue} characters'
            }
          ]
        },
        terms: {
          identifier: 'terms',
          rules: [
            {
              type   : 'checked',
              prompt : 'You must agree to the terms and conditions'
            }
          ]
        }
      }
    })
  ;
</script>

<form class="ui form segment">
  <p>Tell Us About Yourself</p>
  <div class="two fields">
    <div class="field">
      <label>Name</label>
      <input placeholder="First Name" name="name" type="text">
    </div>
    <div class="field">
      <label>Gender</label>
      <select class="ui dropdown" name="gender">
        <option value="">Gender</option>
        <option value="male">Male</option>
        <option value="female">Female</option>
      </select>
    </div>
  </div>
<div class="two fields">
  <div class="field">
    <label>Username</label>
    <input placeholder="Username" name="username" type="text">
  </div>
  <div class="field">
    <label>Password</label>
    <input type="password" name="password">
  </div>
</div>
<div class="field">
  <label>Skills</label>
  <select name="skills" multiple="" class="ui dropdown">
    <option value="">Select Skills</option>
    <option value="css">CSS</option>
    <option value="html">HTML</option>
    <option value="javascript">Javascript</option>
    <option value="design">Graphic Design</option>
    <option value="plumbing">Plumbing</option>
    <option value="mech">Mechanical Engineering</option>
    <option value="repair">Kitchen Repair</option>
  </select>
</div>
<div class="inline field">
  <div class="ui checkbox">
    <input type="checkbox" name="terms">
    <label>I agree to the terms and conditions</label>
  </div>
</div>
<div class="ui blue submit button">Submit</div>
<div class="ui error message"></div>
  </form>
</body>
</html>

我希望我的结果是:

Digit     Count     %
1         5738      29.4
2         3540      18.1
3         2342      12.0
4         1847      9.5
5         1559      8.0
6         1370      7.0
7         1166      6.0
8         1043      5.3
9         904      4.6

有什么建议吗?

5 个答案:

答案 0 :(得分:1)

您可以使用tabulate

>>> import tabulate 
>>> header = ['Digit', 'Count', '%']
>>> tabulate.tabulate([[i, num_count[i], num_count[i]/total_num*100] for i in range(1, 10)], headers=header, tablefmt='grid')

答案 1 :(得分:0)

为什么不使用标签分隔符?

print("Digit", "\t", "Count", "\t", "%")
for i in range(1,10):
    print(i, "\t", num_count[i], "\t",format(num_count[i]/total_num*100,".1f"))

答案 2 :(得分:0)

标签分隔符不适用于大数字。

解决方案是为print语句中的每个变量设置要打印的位数。就像在这个例子中一样:

for i in range(1,1000):
    print("%8d\t%s" % (i*10, i))

要说明选项卡分隔符失败的原因,如果运行此代码:

for i in range(1,1000):
   print("%s\t%s" % (i*10, i))

当它达到990时,你会看到格式化向右跳:

970 97
980 98
990 99
1000    100
1010    101

使用制表也是一种可能性,但这更直接,并提供了一个很好的解决方案。

答案 3 :(得分:0)

除了使用制表符之外,您还可以使用str的.ljust方法填充空格以获得总共某些字符:

print(str(i).ljust(7, " "), 
      str(num_count[i]).ljust(7, " "), 
      format(num_count[i]/total_num*100,".1f"))

答案 4 :(得分:0)

如果知道每列的宽度,可以使用字符串格式:

print("{0:<5s) {1:<5s} {2:<5s}".format("Digit", "Count", "%"))

for i in range(1, 10):
    print("{0:<5d) {1:<5d} {2:<3.2f}".format(i, num_count[i], format(num_count[i]/total_num*100,".1f")))

在此示例中,我们假设数字列最多包含5位数,计数列最多包含5位数,%列最多包含3位数字。小数点,小数点右边2位数。更多信息请访问:https://docs.python.org/2/library/string.html#format-string-syntax