【问题标题】:How to print dict in separate line?如何在单独的行中打印dict?
【发布时间】:2012-04-23 11:33:34
【问题描述】:
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 (words):
        if word in sums:
            sums[word] += 1
        else:
            sums[word] = 1
print sums
fh.close

结果展示

{'and': 1, 'heart': 1, 'love': 2, 'is': 1, 'pass': 1, 'rest': 1, 'wounded': 1, 'at': 3, 
'in': 3, 'lie': 1, 'winchelsea': 1, 'there': 1, 'easy': 1, 'you': 2, 'body': 1, 'be': 
1, 'rise': 1, 'shall': 4, 'may': 2, 'sussex': 1, 'montparnasse': 1, 'not': 3, 'knee': 
1, 'bury': 3, 'tongue': 1, 'champmedy': 1, 'i': 5, 'quiet': 1, 'air': 2, 'fresh': 1, 
'the': 1, 'grass': 1, 'my': 3}

代码打印所有单词和统计频率单词使用。

我想在单独的行中打印 dict。

'and': 1
'heart': 1
'love': 2
...

有什么可能的方法吗?

【问题讨论】:

    标签: python dictionary


    【解决方案1】:
    >>> from pprint import pprint
    >>> pprint(sums)
    {'air': 2,
     'and': 1,
     'at': 3,
     'be': 1,
     'body': 1,
     ....., # and so on...
     'you': 2}
    

    【讨论】:

      【解决方案2】:
      >>>for x in sums:
          print(repr(x),":",dic[x])
      
      
      'and' : 1
      'heart' : 1
      'sussex' : 1
      'rise' : 1
      'love' : 2
      'be' : 1
      'may' : 2
      'the' : 1
      'is' : 1
      'in' : 3
      'body' : 1
      'rest' : 1
      'at' : 3
      'pass' : 1
      'not' : 3
      'knee' : 1
      'air' : 2
      'bury' : 3
      'tongue' : 1
      'lie' : 1
      'winchelsea' : 1
      'i' : 5
      'there' : 1
      'grass' : 1
      'quiet' : 1
      'shall' : 4
      'montparnasse' : 1
      'fresh' : 1
      'easy' : 1
      'wounded' : 1
      'you' : 2
      'champmedy' : 1
      'my' : 3
      

      【讨论】:

        【解决方案3】:

        您可以使用iteritems 来遍历键和值,从而能够根据需要格式化输出。假设字符串作为键,整数作为值:

        for k, v in d.iteritems():
            print '%s: %d' % (k, v)
        

        【讨论】:

          【解决方案4】:

          另一种使用 lambda 的复杂方式

          f = lambda *x: null; 
          f( *( print( x,":",y ) for x,y in mydict.iteritems() ) )
          

          输出

          key2 : 2
          key1 : 1
          

          【讨论】:

            【解决方案5】:

            Python 3 - 方法名称和语法更新 - 不错的 swizzles 集合

            1 - lambdas 是一次性匿名函数,主要用于过滤器、映射、reduce 等。
            这里正在创建一个包含打印和键值迭代的函数。

            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() 返回对象的规范字符串表示。

            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 是一个字符串占位符; %i 是一个整数占位符

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

            【讨论】:

              【解决方案6】:

              不需要使用任何额外的功能,使用简单的for循环,

              student = {"Name":"Chandler Bing","Age":24,"Subject":["Sarcasm","Joke"]}
              print(student)
              for i in student:
                  print(i,":",student[i])
              
              Name : Chandler Bing
              Age : 24
              Subject : ['Sarcasm', 'Joke']
              

              【讨论】:

                猜你喜欢
                • 2019-09-01
                • 2018-12-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-12-16
                • 2021-11-06
                • 1970-01-01
                • 2020-08-14
                相关资源
                最近更新 更多