| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1427 人关注过本帖
标题:counter生成对象不能直接在屏幕打印出字典里的数据信息
只看楼主 加入收藏
glaciya
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2019-10-8
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:1 
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
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:243
专家分:2106
注 册:2019-8-16
收藏
得分:20 
回复 楼主 glaciya
程序代码:
>>> class A(object):
...     def __str__(self):
...         return "this is __str__"
...     def __repr__(self):
...         return "this is __repr__"
...
>>> a = A()
>>> a
this is __repr__
>>> print(a)
this is __str__
>>> 

程序代码:
object.__str__(self)

    Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

    This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

    The default implementation defined by the built-in type object calls object.__repr__().

object.__repr__(self)

    Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

程序代码:
>>> help(Counter.__repr__)
Help on function __repr__ in module collections:
__repr__(self)
    Return repr(self).
>>> help(Counter.__str__)
Help on wrapper_descriptor:

__str__(self, /)
    Return str(self).


repr(object)
返回包含一个对象的可打印表示形式的字符串。 对于许多类型来说,该函数会尝试返回的字符串将会与该对象被传递给 eval() 时所生成的对象具有相同的值,在其他情况下表示形式会是一个括在尖括号中的字符串,其中包含对象类型的名称与通常包括对象名称和地址的附加信息。 类可以通过定义 __repr__() 方法来控制此函数为它的实例所返回的内容。


我的理解是Counter并没有实现这个功能而list实现了.如果你想可以自己修改Counter

[此贴子已经被作者于2019-11-7 10:08编辑过]

2019-11-07 10:02
快速回复:counter生成对象不能直接在屏幕打印出字典里的数据信息
数据加载中...
 
   



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

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