In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the operator library in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
When programming with iterators in Python, you usually need to create small functions for simple expressions. Sometimes you can do this through lambda expressions, but for some operations you don't need to create new functions. The operator module defines functions corresponding to built-in operations, such as arithmetic operations, comparison operations, and operations corresponding to standard API.
Logical operation
These functions are used to determine whether a given value is Boolean equal, to invert it to create an opposite Boolean value, and to compare operations to determine whether it is equal.
Def logical_operations ():
A =-1
B = 5
Print ('a =', a)
Print ('b =', b)
Print ()
Print ('not_ (a):', not_ (a))
Print ('truth (a):', truth (a))
Print ('is_ (a, b):', is_ (a, b))
Print ('is_not (a, b):', is_not (a, b)) comparison operation
Support for rich comparison operators
Def comparison_operations ():
A = 1
B = 5.0
Print ('a =', a)
Print ('b =', b)
For func in (lt, le, eq, ne, gt, ge):
Print ('{} (a, b): {} '.format (func.__name__, func (a, b)
Arithmetic operation
Support arithmetic operators between values: absolute values, addition, subtraction, multiplication and division, bit operations (and, OR, non, XOR, left shift, right shift)
Def arithmetic_operations ():
A =-1
B = 5.0
C = 2
D = 6
Print ("Positive/Negative")
Print ('abs (a):', abs (a))
Print ('neg (b):', neg (b))
Print ('pos (c):', pos (c))
Print ("\ nArithmetic")
Print ("add (a, b):", add (a, b))
Print ("sub (b, a):", sub (b, a))
Print ("mul (a, b):", mul (a, b))
Print ("floordiv (a, b):", floordiv (a, b))
Print ("truediv (a, b):", truediv (a, b))
Print ("floordiv (d, c):", floordiv (d, c))
Print ("truediv (d, c):", truediv (d, c))
Print ("mod (a, b):", mod (a, b))
Print ("pow (c, d):", pow (c, d))
Print ("\ nBitwise")
Print ("and_ (c, d)", and_ (c, d))
Print ("invert (c)", invert (c))
Print ("lshift (c, d)", lshift (c, d))
Print ("or_ (c, d)", or_ (c, d))
Print ("rshift (d, c)", rshift (d, c))
Print ("xor (c, d)", xor (c, d))
Floordiv integer division; truediv floating point division
Sequence operation
Sequence operators can be divided into four categories: sequence creation, sequence item search, sequence access, and sequence search.
Def sequence_operations ():
A = [1, 2, 3]
B = ['averse,' baked, 'crested,' d']
Print ("Constructive")
Print ("concat (a, b):", concat (a, b))
Print ("\ nSearching")
Print ("contains (a, 1):", contains (a, 1))
Print ("countOf (b,'c'):", countOf (b, "c"))
Print ("countOf (b,'d'):", countOf (b, "d"))
Print ("indexOf (a, 1):", indexOf (a, 1))
Print ("\ nAccess Items")
Print ("getitem (b, 1):", getitem (b, 1))
Print ("getitem (b, slice (1,3)):", getitem (b, slice (1,3)
Print ("setitem (b, 1,'d'):", end ='')
Setitem (b, 1, "d")
Print (b)
Print ("\ nDestructive")
Print ("delitem (b, 1):", end= "")
Delitem (b, 1)
Print (b)
Both setitem and delitem modify the sequence in place and do not return a value
In-situ operation
In addition to the standard operator, many types of objects support "in-place" modification through special operators, such as + =. There are also functions equivalent to in-situ modification.
Def inplace_operations ():
A =-1
B = 5.0
C = [1, 2, 3]
D = ['await,' baked,'c']
A = iadd (a, b)
Print ('a = iadd (a, b) = >', a)
C = iconcat (c, d)
Print ('c = iconcat (c, d) = >', c) Getters operation
One of the features that are not commonly used in operator modules is the Getters concept. At run time, callable objects are constructed to retrieve object or content properties from a sequence; Getters is particularly useful when using iterators or generator sequences: it costs less than lambda or Python functions.
Class MyObj:
Def _ _ init__ (self, arg):
Super (). _ _ init__ ()
Self.arg = arg
Def _ repr__ (self):
Return 'MyObj ({})' .format (self.arg)
Def getters_operations ():
# attrgetter
L = [MyObj (I) for i in range (5)]
Print ('objects:', l)
G = attrgetter ('arg')
Vals = [g (I) for i in l]
Print ('arg values:', vals)
L.reverse ()
Print (l)
Print ('sorted:', sorted (l, key=g))
# itemgetter
L = [dict (val=-1 * I) for i in range (4)]
Print ('original:', l)
G = itemgetter ('val')
Vals = [g (I) for i in l]
Print (values)
Other: functions in the operator module (lt, etc.) operate through the standard Python interface, so these functions can act on user-defined classes, consistent with built-in types.
On how to use the operator library in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.