| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1437 人关注过本帖
标题:counter生成对象不能直接在屏幕打印出字典里的数据信息
取消只看楼主 加入收藏
glaciya
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2019-10-8
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:0 
counter生成对象不能直接在屏幕打印出字典里的数据信息
最近在学python 发现一个问题。当使用counter功能时,感觉是得到了
一个对象地址,但是不明白为什么不能直接输出地址里的数据,反而是需要结合List才能看到里面的数据,请问这是什么原因?是否相当于java里的需要使用某种方法(将对象作为参数)才能显示对象的信息。
In [42]: from collections import Counter

In [43]: a=Counter(wordsplits)

In [44]: print(a)
Counter({'a': 4, 'long': 2, 'time': 2, 'ago,': 2, 'there': 2, 'was': 2, 'prince'                                                                             : 2, 'and': 2, 'princess': 2, 'living': 2, 'in': 2, 'different': 2, 'casles': 2}                                                                             )

In [45]: print(a.elements)
<bound method Counter.elements of Counter({'a': 4, 'long': 2, 'time': 2, 'ago,':                                                                              2, 'there': 2, 'was': 2, 'prince': 2, 'and': 2, 'princess': 2, 'living': 2, 'in                                                                             ': 2, 'different': 2, 'casles': 2})>

In [46]: print(a.elements())
<itertools.chain object at 0x7f37ea7430f0>

In [47]: a.elements()
Out[47]: <itertools.chain at 0x7f37ea7430b8>

In [48]: list(a.elements())
Out[48]:
['long',
 'long',
 'time',
 'time',
 'ago,',
 'ago,',
 'there',
 'there',
 'was',
 'was',
 'a',
 'a',
 'a',
 'a',
 'prince',
 'prince',
 'and',
 'and',
 'princess',
 'princess',
 'living',
 'living',
 'in',
 'in',
 'different',
 'different',
 'casles',
 'casles']




问题2:

为什么使用counter之后的对象不能直接输出,使用print打印出来的是个地址。

In [62]: for numvalue in a:
    ...:     print(numvalue,a[numvalue], end='  ')
    ...:
    ...:
    ...:
    ...:
long 2  time 2  ago, 2  there 2  was 2  a 4  prince 2  and 2  princess 2  living 2  in 2  different 2  casles 2
In [63]: copyword={}

In [64]: for numvalue in a:
    ...:     print(numvalue,a[numvalue], end='  ')
    ...:     copyword[numvalue]=a[numvalue]
    ...: print(copyword)
    ...:
    ...:
    ...:
    ...:
long 2  time 2  ago, 2  there 2  was 2  a 4  prince 2  and 2  princess 2  living 2  in 2  different 2  casles 2  
{'long': 2, 'time': 2, 'ago,': 2, 'there': 2, 'was': 2, 'a': 4, 'prince': 2, 'and': 2, 'princess': 2, 'living': 2, 'in': 2, 'different': 2, 'casles': 2}

In [65]: copyword
Out[65]:
{'long': 2,
 'time': 2,
 'ago,': 2,
 'there': 2,
 'was': 2,
 'a': 4,
 'prince': 2,
 'and': 2,
 'princess': 2,
 'living': 2,
 'in': 2,
 'different': 2,
 'casles': 2}
搜索更多相关主题的帖子: 对象 and time long counter 
2019-11-06 17:55
快速回复:counter生成对象不能直接在屏幕打印出字典里的数据信息
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012072 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved