如何将打印输出格式化为固定宽度

如何将打印输出格式化为固定宽度

本文介绍了如何将打印输出格式化为固定宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



$ p $ def $ splitter(str):
(1,len(str)):
start = str [0:i]
end = str [i:]
yield(start,end)
for split splitter(end):
result = [start]
result.extend(split)
yield result

el = [];

string =abcd
for splitter(abcd):
el.extend(b);

unique = sorted(set(el));

作为唯一前缀:
如果前缀!=:
printvalue,前缀 - num = occurrences =,string.count(str ));

我想打印所有在字符串变量中出现的排列。

由于排列的长度不一样,所以我想修正宽度并打印出来,不像这样:

 值a  - 出现次数= 1 
值ab - 出现次数= 1
值abc - 出现次数= 1
值b - num出现次数= 1
值bc - 出现次数= 1
值bcd - 出现次数= 1
值c - 出现次数= 1
值cd - 出现次数= 1
值d - 出现次数= 1

如何使用格式来做到这一点?



我发现了这些帖子,但是对于字母数字字符串却不太合适:



解决方案

编辑12.11.2013 - 这个答案是非常古老的。它仍然是有效和正确的,但是看这个的人应该更喜欢。

您可以使用像这样:

 >>>打印'%5s'%'aa'
aa
>>>打印'%5s'%'aaa'
aaa
>>>打印'%5s'%'aaaa'
aaaa
>>>打印'%5s'%'aaaaa'
aaaaa

基本上:




  • 字符通知python,它必须将某些东西替换为一个标记。 $ b
  • s 字符通知python令牌将是一个字符串
  • 5
  • code>(或任何你想要的数字)通知python填充空格最多5个字符的字符串。


    在你的一个可能的实现可能如下所示:

     >>> dict_ = {'a':1,'ab':1,'abc':1} 
    >>>对于dict_.items()中的项目:
    ... print'value%3s - 发生次数=%d'%item#%d是整数标记
    ...
    值a发生次数= 1
    值发生次数= 1
    值abc - 发生次数= 1

    边注:只是想知道您是否知道。例如,您可以在一行中获得所有组合的列表:

     >>> [''.join(perm)for i在范围内(1,len(s))为perm in it.permutations(s,i)] 
    ['a','b','c',' d','ab','ac','ad','ba','bc','bd','ca','cb','cd','da','db','dc' ,abc,abd,acb,acd,adb,adc,bac,bad,bca,bcd,bda,bdc, cab,cad,cba,cbd,cda,cdb,dab,dac,dba,dbc,dca,dcb] b

    你可以通过使用组合结合 count()


    I have this code (printing the occurrence of the all permutations in a string)

    def splitter(str):
        for i in range(1, len(str)):
            start = str[0:i]
            end = str[i:]
            yield (start, end)
            for split in splitter(end):
                result = [start]
                result.extend(split)
                yield result
    
    el =[];
    
    string = "abcd"
    for b in splitter("abcd"):
        el.extend(b);
    
    unique =  sorted(set(el));
    
    for prefix in unique:
        if prefix != "":
            print "value  " , prefix  , "- num of occurrences =   " , string.count(str(prefix));
    

    I want to print all the permutation occurrence there is in string varaible.

    since the permutation aren't in the same length i want to fix the width and print it in a nice not like this one:

    value   a - num of occurrences =    1
    value   ab - num of occurrences =    1
    value   abc - num of occurrences =    1
    value   b - num of occurrences =    1
    value   bc - num of occurrences =    1
    value   bcd - num of occurrences =    1
    value   c - num of occurrences =    1
    value   cd - num of occurrences =    1
    value   d - num of occurrences =    1
    

    How can I use format to do it?

    I found these posts but it didn't go well with alphanumeric strings:

    python string formatting fixed width

    Setting fixed length with python

    解决方案

    EDIT 12.11.2013 - This answer is very old. It is still valid and correct, but people looking at this should prefer the new format syntax.

    You can use string formatting like this:

    >>> print '%5s' % 'aa'
       aa
    >>> print '%5s' % 'aaa'
      aaa
    >>> print '%5s' % 'aaaa'
     aaaa
    >>> print '%5s' % 'aaaaa'
    aaaaa
    

    Basically:

    • the % character informs python it will have to substitute something to a token
    • the s character informs python the token will be a string
    • the 5 (or whatever number you wish) informs python to pad the string with spaces up to 5 characters.

    In your specific case a possible implementation could look like:

    >>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
    >>> for item in dict_.items():
    ...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
    ...
    value   a - num of occurances = 1
    value  ab - num of occurances = 1
    value abc - num of occurances = 1
    

    SIDE NOTE: Just wondered if you are aware of the existence of the itertools module. For example you could obtain a list of all your combinations in one line with:

    >>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
    ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
    

    and you could get the number of occurrences by using combinations in conjunction with count().

    这篇关于如何将打印输出格式化为固定宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:20