RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1284342
Accepted
Kadenza
Kadenza
Asked:2022-05-21 03:00:08 +0000 UTC2022-05-21 03:00:08 +0000 UTC 2022-05-21 03:00:08 +0000 UTC

我无法理解编程基础的基础知识。即,在对对象一无所知的情况下如何处理对象?

  • 772

我完全无法理解如何在 python 中使用对象。我知道在 python 中一切都是一个对象(嗯,他们是这样写的)。在大多数情况下,如何使用它们是很清楚的。假设这个对象是一个字典,那么我知道你可以遍历它的键,通过键访问它的值,给它加值等等。

但是,由于某些操作,我得到如下信息时该怎么办:

<pandas.core.groupby.generic.SeriesGroupBy object at 0x7f064fd517f0>

或者这是一个具体的例子:

r = [1, 2, 3, 4, 5]
print(map(lambda x: x*3, r)) 
<map object at 0x7fc3861bbee0>

我知道 map 返回一个迭代器,您可以使用 next() 或 for 循环或立即将所有内容传输到列表以查看值。但是直到最后我仍然无法理解可以用它做什么以及如何应用它,以及如何不应用它。

大多数时候,我根本不明白返回的是什么,那些字符背后是什么,是否可以迭代等等。

如何使用它?

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    CrazyElf
    2022-05-21T03:13:47Z2022-05-21T03:13:47Z

    假设您已被 Google 禁止。它发生了。让我们尝试处理 python 本身。让我们看一下关于对象的简短文档,以及可用对象方法的列表。为此,我们将根据原则创建一个小函数,以免重复代码DRY(请勿重复):

    def object_info(obj):
        print(type(obj))
        print()
        print(obj.__doc__)
        print()
        print(dir(obj))
        print()
    

    现在让我们尝试探索一些对象,从列表开始:

    r = [1, 2, 3, 4, 5]
    object_info(r)
    

    结论:

    <class 'list'>
    
    Built-in mutable sequence.
    
    If no argument is given, the constructor creates a new empty list.
    The argument must be an iterable if specified.
    
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',
     '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',
     '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
     '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
     '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
     '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',
     'remove', 'reverse', 'sort']
    

    好吧,我们被告知这是一个可变序列之类的东西。您还可以看到此对象具有方法append、pop和sort其他方法。

    让我们看看“字典”对象的实例会告诉我们什么:

    d = {1: 2, 3: 4}
    object_info(d)
    

    结论:

    <class 'dict'>
    
    dict() -> new empty dictionary
    dict(mapping) -> new dictionary initialized from a mapping object's
        (key, value) pairs
    dict(iterable) -> new dictionary initialized as if via:
        d = {}
        for k, v in iterable:
            d[k] = v
    dict(**kwargs) -> new dictionary initialized with the name=value pairs
        in the keyword argument list.  For example:  dict(one=1, two=2)
    
    ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',
     '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',
     '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__',
     '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__',
     '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items',
     'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
    

    哇,我们被告知如何创建字典以及如何使用不同的方法填充它。keys字典有很多方法,例如items.

    m = map(lambda x: x*3, r)
    object_info(m)
    

    结论:

    <class 'map'>
    
    map(func, *iterables) --> map object
    
    Make an iterator that computes the function using arguments from
    each of the iterables.  Stops when the shortest iterable is exhausted.
    
    ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
     '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__',
     '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__',
     '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
    

    好吧,一切也都写好了。如果你懂英语很方便。虽然大多数对象的方法或多或少都是标准的,但不是全部。了解在大多数对象上找到的方法(例如__init__、__str__和__doc__特定于例如集合和其他类型对象的方法)很有用。然后,即使只是一组方法,也很可能猜出它是什么类型的对象以及可以用它做什么。

    • 14

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5