How to print dict in separate line?

前端 未结 6 1768
猫巷女王i
猫巷女王i 2020-12-06 14:20
import re
sums = dict()
fh= open(\'wordcount.txt\',\'r\')
for line in fh:
    words = [word.lower() for word in re.findall(r\'\\b\\w+\\b\', line)]
    for word in (w         


        
6条回答
  •  天命终不由人
    2020-12-06 15:02

    Python 3 - method name and syntactical updates - nice collection of swizzles

    1 - lambdas are throw-away anonymous functions mainly used with filter, map, reduce etc.
    Here were are creating a function containing a print and key,value iteration.

    f = lambda *x: None; 
    f( *( print( x,":",y ) for x,y in genre_counting.items() ) )
    
    Games : 3862
    Productivity : 178
    Weather : 72
    Shopping : 122
    Reference : 64
    Finance : 104
    

    2 - repr() returns the canonical string representation of the object.

    for x in genre_counting:
    print(repr(x),":",genre_counting[x])
    
    'Games' : 3862
    'Productivity' : 178
    'Weather' : 72
    'Shopping' : 122
    'Reference' : 64
    'Finance' : 104
    

    3 - %s is a string placeholder; %i is an integer placeholder

    for k, v in genre_counting.items():
    print( '%s : %i' % (k, v) )
    
    Games : 3862
    Productivity : 178
    Weather : 72
    Shopping : 122
    Reference : 64
    Finance : 104
    

提交回复
热议问题