首页 > 编程语言 >Python - Creating jump tables using lambda functions

Python - Creating jump tables using lambda functions

时间:2024-07-31 17:20:57浏览次数:11  
标签:tables functions Creating ... Python jump expressions lambda

We can place lambda function inside list and dictionary literals. This way we can use lambda expressions to create jump tables.

>>> L = [lambda s: s.strip().lower(),

...  lambda s: s.strip().upper(),

...  lambda s: s.lstrip().title(),

...  lambda s: s[::-1].lower(),

...  ]

Here, we have stored these lambda expressions in a list:

>>> L[1]('Python')

PYTHON

>>> L[3]('Python')

nohtyp

Here, we have used lambda expressions as values of a dictionary:

>>> d = {'add': lambda x, y: x + y,

...  'subtract': lambda x, y: x - y,

...  'multiply': lambda x, y: x * y,

...  'divide': lambda x, y: x // y,

...  'power': lambda x, y: x ** y,

...  'double': lambda x: x * 2,

...  'square': lambda x: x ** 2,

...  'table': lambda x: [x * i for i in range(1, 11)],

...  'summation': lambda x: sum(range(1, x + 1)),

...  }

>>> d['summation'](4)

10

>>> d['power'](3,2)

9

So, when you have to write a lot of small functions that are used only once, you can use lambda expressions instead of defining lots of one-off def statements. 

 

标签:tables,functions,Creating,...,Python,jump,expressions,lambda
From: https://www.cnblogs.com/zhangzhihui/p/18335038

相关文章

  • Python - Creating your own Iterator
    Inourfirstexample,wewillcreateiterableobjects,which,wheniteratedover,willgiveoutcubesofnumbers,andtheseobjectswillsupportmultipleiterations.classCubes:def__init__(self,start,stop):self.start=startsel......
  • Python - Creating Managed Attributes using properties
    CreatingManagedAttributesusingpropertiesPropertiescanbeusedtocreatedataattributeswithspecialfunctionality.Ifyouwantsomeextrafunctionality(liketypechecking,datavalidationortransformation)whilegettingorsettingadataattribut......
  • Python - Creating alternative initializers using class Methods
    Classmethodsallowustodefinealternativeinitializers(alsoknownasfactorymethods)inaclass.Thesemethodshelpuscreateinstanceobjectsfromdifferenttypesofinputdata.Letusunderstandthiswiththehelpofanexample.Again,wetakethe......
  • Python - Using a list with functions from the random module
    Toselectarandomitemfromthelistorshufflethelist,youcanusethechoiceandshufflefunctionsfromtherandommoduleofthestandardlibrary.Therandom.choice()functionreturnsarandomlyselectedelementfromthelist.>>>importran......
  • mysqldump: Got error: 1066: Not unique table/alias: 'act_evt_log' when using LOC
    先说解决办法:执行下面语句mysqldump-ushooter-p123123--single-transactionfd>fd.sql  lower_case_table_names区分大小写设置注意:此参数不可以动态修改,必须重启数据库 12341、参数含义:lower_case_table_names=1  表名存储在磁盘是小写的,但是比......
  • 在 M1 Mac 上安装 pytables 时出现问题
    我正在尝试在M1Mac上安装pytables(使用自制软件安装的MacOS12.6.1、python3.11和hdf51.12.2)。按照https://stackoverflow.com/a/74276925中的建议我执行了以下操作:pipinstallcythonbrewinstallhdf5brewinstallc-bloscexportHDF5_DIR=/opt/homebrew......
  • 防火墙——iptables实验
    [root@localhost~]#yuminstalliptables-y[root@localhost~]#systemctlstopfirewalld[root@localhost~]#systemctlstartiptables[root@server~]#iptables-F#清空所有的规则表,清空之后客户端可以访问ssh和http服务(2)实验实验......
  • CentOS6.3安装xtables-addons,实现流量复制&镜像克隆
    一、版本要求CentOS6.3,64位(内核版本2.6.32-279),iptables版本是自带的v1.4.7CentOS6.9--6.10也可以,其它版本没有测试。ubuntu22.04上没有安装成功。二、依赖安装yuminstallgccyuminstallgcc-c++yuminstalliptables-devel三、下载安装包下载地址:https://inai.d......
  • 使用iptables管控docker容器
    docker与iptables说明某些项目考虑到安全问题,需要启用iptables来进行加固。根据官方文档介绍(https://dockerdocs.cn/network/iptables/):在Linux上,Docker操纵iptables规则以提供网络隔离。尽管这是实现的详细信息,并且您不应修改Docker在iptables策略中插入的规则,但是如果您想要......
  • 使用 Python 通过逻辑应用运行长时间运行的 Azure Functions
    我已经尝试解决这个问题有一段时间了,但我似乎找不到解决方案。因此,正如标题所示,我试图通过函数在逻辑应用程序中运行长时间运行的操作。我有一个python代码,可以比较2个excel文件并进行一些转换。它工作正常,但是,Excel文件包含近20k行(它是动态的,将来会添加更多行),因此......