Python operator.itemgetter(item) and operator.itemgetter(*items)

Python operator.itemgetter{item} and operator.itemgetter{*items}

  • [1. `operator.itemgetter(item)` and `operator.itemgetter(*items)`](#1. operator.itemgetter(item) and operator.itemgetter(*items))
  • References

operator - Standard operators as functions
https://docs.python.org/3/library/operator.html

The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y.
operator 模块提供了一套与 Python 的内置运算符对应的高效率函数。例如,operator.add(x, y) 与表达式 x+y 相同。

Many function names are those used for special methods, without the double underscores. For backward compatibility, many of these have a variant with the double underscores kept. The variants without the double underscores are preferred for clarity.

许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数。为了表述清楚,建议使用没有双下划线的函数。

The functions fall into categories that perform object comparisons, logical operations, mathematical operations and sequence operations.

比较运算、逻辑运算、数学运算以及序列运算。

1. operator.itemgetter(item) and operator.itemgetter(*items)

operator.itemgetter(item)
operator.itemgetter(*items)

Return a callable object that fetches item from its operand using the operand's __getitem__() method. If multiple items are specified, returns a tuple of lookup values.

返回一个使用操作数的 __getitem__() 方法从操作数中获取 item 的可调用对象。 如果指定了多个条目,则返回一个查找值的元组。

For example:

  • After f = itemgetter(2), the call f(r) returns r[2].
  • After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3]).

Equivalent to:

复制代码
def itemgetter(*items):
    if len(items) == 1:
        item = items[0]
        def g(obj):
            return obj[item]
    else:
        def g(obj):
            return tuple(obj[item] for item in items)
    return g

The items can be any type accepted by the operand's __getitem__() method.

复制代码
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import operator


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f"Hi, {name}")  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi("PyCharm")

    # See PyCharm help at https://www.jetbrains.com/help/pycharm/

    str = "ABCDEFG"

    fun1 = operator.itemgetter(1)
    fun2 = operator.itemgetter(1, 3, 5)
    print("fun1(str):", fun1(str))
    print("fun2(str):", fun2(str))

    soldier = dict(rank="captain", name="yongqiang")
    fun3 = operator.itemgetter("name")
    print("fun3(soldier):", fun3(soldier))

    inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
    getcount = operator.itemgetter(1)
    result = sorted(inventory, key=getcount)
    print('\n', type(result))
    print("result:", result)

/home/yongqiang/miniconda3/bin/python /home/yongqiang/langchain_work/yongqiang/main.py 
Hi, PyCharm
fun1(str): B
fun2(str): ('B', 'D', 'F')
fun3(soldier): yongqiang

 <class 'list'>
result: [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

Process finished with exit code 0

References

1\] Yongqiang Cheng,

相关推荐
Mikhail_G30 分钟前
Python应用函数调用(二)
大数据·运维·开发语言·python·数据分析
weixin_472339461 小时前
使用Python提取PDF元数据的完整指南
java·python·pdf
QQ676580081 小时前
基于 PyTorch 的 VGG16 深度学习人脸识别检测系统的实现+ui界面
人工智能·pytorch·python·深度学习·ui·人脸识别
木木黄木木1 小时前
Python制作史莱姆桌面宠物!可爱的
开发语言·python·宠物
胖哥真不错2 小时前
Python基于方差-协方差方法实现投资组合风险管理的VaR与ES模型项目实战
python·毕业设计·课程设计·方差-协方差方法·投资组合风险管理·var与es模型
慧一居士2 小时前
flask功能使用总结和完整示例
python
大模型铲屎官3 小时前
【深度学习-Day 23】框架实战:模型训练与评估核心环节详解 (MNIST实战)
人工智能·pytorch·python·深度学习·大模型·llm·mnist
阿幸软件杂货间3 小时前
PPT转图片拼贴工具 v1.0
python·powerpoint
AIGC_北苏3 小时前
DrissionPage爬虫包实战分享
爬虫·python·drissionpage
YBCarry_段松啓4 小时前
uv:下一代 Python 包管理器
人工智能·python