I. Naming of the Patriarchs
In python basics, when we learned about tuples, the elements inside the tuples were fetched by index. But this way of fetching is not friendly enough, so we introduce named tuples to fetch tuples in a dictionary style, which takes less memory than dictionary storage. If the data does not need to change, you can use named tuples instead of dictionaries.
Regular meta-ancestor fetching method:
info = ("flora", 28, "Female.") name = 0 age = 1 gender = 2 print(info[name]) # Get name print(info[age]) # Get age print(info[gender]) # Get Gender
Name the metazoan way:
# namedtuple: takes two arguments: the first is the name of the type being created, the second is the list from collections import namedtuple info = namedtuple("info_key", ["name", "age", "gender"]) info_01 = info("flora", 28, "Female.") print(info_01.name) # Get name print(info_01.age) # Get age print(info_01.gender) # Get the gender print(info_01) # Print result: info_key(name='flora', age=28, gender='female')
II. Application in automated testing scenarios
We read the use case data from excel the first row header is key, the value of each row is value. if stored as a dictionary format the format is as follows:
case = [ {"case_id": 1, "case_title": "Normal login", "data": "test", "expected": "pass"}, {"case_id": 2, "case_title": "Login failed.", "data": "test", "expected": "pass"}, ]
We can store the conversion to named meta-ancestors as follows:
# namedtuple: takes two arguments: the first is the name of the type being created, the second is the list from collections import namedtuple case = [ {"case_id": 1, "case_title": "Normal login", "data": "test01", "expected": "pass"}, {"case_id": 2, "case_title": "Login failed.", "data": "test02", "expected": "pass"}, ] cases = namedtuple("case", case[0].keys()) for i in case: result = cases(*()) print() # Print results: test01 test02
III. Trimetric operators
The ternary operator in python is equivalent to the ternary operator in java.
- basic grammar
The result of the execution if the condition is valid else the result of the execution if the condition is not valid.
- practical application
Let's say we want to write a Python program that takes two numbers as input, compares their sizes and outputs the larger of the two. The trinomial operator is more concise and straightforward to write than the regular way.
Regular Writeup:
x = int(input("Please enter the first positive integer:")) y = int(input("Please enter the second positive integer:")) if x == y: print("The larger number is:", x) elif x > y: print("The larger number is:", x) else: print("The larger number is:", y)
Mitsume operator writing:
x = int(input("Please enter the first positive integer:")) y = int(input("Please enter the second positive integer:")) print("The larger number is:{}".format(x if x > y else y))
- Extension: Nesting of trinomial operators
Python trinomial operators support nesting, which allows you to form more complex expressions. You need to pay attention to the pairing of if and else when nesting.
For example, we need to determine the relationship between two numbers.
Regular Writeup:
a = int(input("Please enter a:")) b = int(input("Please enter b:")) if a > b: print("a is greater than b") else: if a < b: print("a is less than b") else: print("a equals b")
Nested writing of trinomial operators:
a = int(input("Please enter a:")) b = int(input("Please enter b:")) print("a is greater than b") if a > b else (print("a is less than b") if a < b else print("a equals b"))
IV. Deductive
derivational comprehensions(also known as the analytic formula),be python A unique characteristic of。derivationalbe可以从一个数据序列构建另一个新的数据序列。 derivational的作用:Fast data generation。
list-deductive formula
- Regular List Derivative
Basic syntax: [iterate through the contents added to the list one at a time for x in xxx]
Example: Outputs a list of numbers from 0-100.
Regular Writeup:
li = [] for i in range(101): (i) print(li)
Deductive writing:
li = [i for i in range(101)] print(li)
- List-deductive nested if
Basic syntax: [Iterate through the contents added to the list one at a time for x in xxx if filter criteria]
Example: Output a list of even numbers from 0-100.
Regular Writeup:
li = [] for i in range(101): if i % 2 == 0: (i) print(li)
Deductive writing:
li = [i for i in range(101) if i % 2 == 0] print(li)
- List Derivatives Combined with the Trinomial Operator
Basic syntax: [if filter else condition iterate through contents added to list one at a time for x in xxx ]
Example: 0-100 numbers, if it is an even number then return even 0, if it is an odd number then return odd 1.
Regular Writeup:
li = [] for i in range(101): if i % 2 == 0: ("Even 0.") else: ("Odd 1") print(li)
List derivatives + trinomial operator:
li = ["Even 0." if i % 2 == 0 else "Odd 1" for i in range(101)] print(li)
V. Dictionary derivatives
Basic syntax: {key: value Expression to get key value}
Example: Have a list li, convert it to a dictionary format with element subscripts as keys and values as elements.
Regular Writeup:
li = ["id", "title", "url", "data", "expected"] dic = {} for i, j in enumerate(li): dic[i] = j print(dic)
Dictionary Deductive Writing:
li = ["id", "title", "url", "data", "expected"] dic = {i: j for i, j in enumerate(li)} print(dic)
to this article on python data types related to knowledge expansion of the article is introduced to this, more related python data types content please search my previous articles or continue to browse the following related articles I hope that you will support me more in the future!