[Python] – 4-tuple list and dictionary

Time:2021-5-11

introduction

This article introduces three kinds of combined data types in Python, and the second half of the article describes how to use “fragmentation” to extract data from these combined data types.

Contents of articles

0 × 1. Tuple
0 × 2. List
0 × 3. Dictionary
0 × 4. Examples of data fragmentation

0 × 1. Tuple

In Python, tuples are created with brackets. If there is only one element in brackets, you need to add a comma at the end of the element (if you don’t add a comma, the data will be created as string or numeric data type). After tuples are created, you can’t add or delete the elements in the tuples, and the value direction is fixed. Please see the following example:

#Create three tuples, B tuple contains only one string element, so you need to add a comma at the end, otherwise B will be created as a string object, C tuple is a multidimensional tuple, including a and B tuples
>>> a=(1,2,3,4,5)
>>> b=("www.qingsword.com",)
>>> c=(a,b)

#Print out the elements in each of the three tuples
>>> print(a)
(1, 2, 3, 4, 5)
>>> print(b)
('www.qingsword.com',)
>>> print(c)
((1, 2, 3, 4, 5), ('www.qingsword.com',))

#Print out the first element of a tuple, and the index of Python starts from 0
>>> print(a[0])
1

#Print out the last element in a-tuple, and len() function will get the number of elements in a-tuple. Because the index starts from 0, the number of elements minus one is the index number of the last element
>>> print(a[len(a)-1])
5

#Another simple way to get the last element in the tuple is to use - 1 directly, and so on. To get the penultimate element, use - 2 as the index value
>>> print(a[-1])
5
>>> print(a[-2])
4

#Get the value of 2 in the index position of the 0 th element (a-tuple) in the multidimensional tuple, that is, 3 in the a-tuple
>>> print(c[0][2])
3

#Let's look at the following example. A list is included in the tuple, and then the index is used to modify the values of the two elements in the list. The immutable element of the tuple mentioned above refers to the unchanged reference location (memory address). In this example, the element with index position 2 of tuple T1 points to the memory address of a list, We only changed the element in the list, but did not change the direction of the tuple. If we change the direction to another list or element, we will have an error
>>> t1=(1,2,["x","y"])
>>> print(t1)
(1, 2, ['x', 'y'])
>>> t1[2][0]="a"
>>> t1[2][1]="b"
>>> print(t1)
(1, 2, ['a', 'b'])

#You can even use the list method to add and delete data from the list contained in the tuple. The related operations of the list will be introduced in the second part of this article
>>> t1[2].append("c")
>>> print(t1)
(1, 2, ['a', 'b','c'])

0 × 2. List

In Python, you can use brackets to create lists. Most of the operations in the tuple instance above are applicable to lists. You just need to replace the parentheses with brackets. The difference is that when the list contains only one element, you don’t need to add a comma at the end. In addition, you can add or delete elements from the list. See the following example:

#Create three lists, of which C is a multidimensional list, including a and B
>>> a=[1,2,3,4,5]
>>> b=["www.qingsword.com"]
>>> c=[a,b]

#Print out the elements in three lists
>>> print(a)
[1, 2, 3, 4, 5]
>>> print(b)
['www.qingsword.com']
>>> print(c)
[[1, 2, 3, 4, 5], ['www.qingsword.com']]

#Use the list. Append () method to add an element 6 to the a list
>>> a.append(6)
>>> print(a)
[1, 2, 3, 4, 5, 6]

#The append() method can only add a single element at a time, while the list. Extend() method can add multiple elements at a time
>>> a.extend([7,8,9])
>>> print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#Using the feature of extend() method, you can copy all the elements in one list to another. Next, you create an empty list D, and then copy the elements in the C list to d. There is a strange thing here. When you create C, the a list contains only the first five digits. Now the first element in the C list has nine digits, This is because the first element of C list only points to the memory address of a list. When a list changes, the data read by C will also change
>>> d=[]
>>> d.extend(c)
>>> print(d)
[[1, 2, 3, 4, 5, 6, 7, 8, 9], ['www.qingsword.com']]

#Use the list. Remove() method to delete the elements in the list. What this method receives is not the index value, but the element value (in this example, elements 1 and 2 in the a list are deleted directly)
>>> a.remove(1)
>>> a.remove(2)
>>> print(a)
[3, 4, 5, 6, 7, 8, 9]

#The list. Pop() method receives an index value. If no index value is specified, it defaults to the index value of the last element. This method will take out and return the corresponding element to the caller, and then delete the element from the list
>>> print(a.pop())
9
>>> print(a)
[3, 4, 5, 6, 7, 8]

#Delete element with index value 0
>>> print(a.pop(0))
3
>>> print(a)
[4, 5, 6, 7, 8]

Python provides a set() function to handle repeated elements in the list and merge them into one element. For example:

#Use set() to remove duplicate values from the list
>>> lst1=set([1,2,3,3,4,4])
>>> lst2=set([3,4,5,5,6,6])
>>> lst3=set(["a","a","b","c","c"])

>>> print(list(lst1))
[1, 2, 3, 4]
>>> print(list(lst2))
[3, 4, 5, 6]
>>> print(list(lst3))
['a', 'c', 'b']

#Because there are no duplicate values in the set type list, it can be used for scientific calculation, such as intersection and union
>>> print(list(lst1&lst2))
[3, 4]
>>> print(list(lst1|lst2))
[1, 2, 3, 4, 5, 6]

In addition to manually entering the elements of each list, python also provides an advanced list feature – “list generation”, which can automatically generate lists with for loop

#"List generative" syntax: [element result expression for loop judgment statement or nested loop]
#The execution order is as follows: first run "for loop", then execute "judgment statement or nested loop", put each value obtained from "judgment statement or nested loop of" for loop "into" element result expression "for calculation, and finally save the result to the list

#Generate a list of the squares of each number from 1 to 10
>>> L0=[x*x for x in range(1,11)]
>>> print(L0)
[1, 4, 9, 16, 25, 36, 49, 64, 81,100]

#Read the numbers one by one from 0 to 9. If the number read can be divided by 2, execute x * x and save the result to the list
>>> L1=[x*x for x in range(0,10) if x%2==0]
>>> print(L1)
[0, 4, 16, 36, 64]

#Nested loop, to achieve full combination, X is equivalent to the outer loop, y is equivalent to the nested sub loop, each x will get three Y values, each result will execute x + y and save the results to the list
>>> L2=[x+y for x in "ABC" for y in "abc"]
>>> print(L2)
['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Bc', 'Ca', 'Cb', 'Cc']

#Case conversion
>>> L3=[x.upper() for x in "abc"]
>>> print(L3)
['A', 'B', 'C']
>>> L3=[x.lower() for x in "ABC"]
>>> print(L3)
['a', 'b', 'c']

#Use a and B variables to read the key and value of each element in the dictionary {"one": 1, "two": 2}, and finally save them to the list with an equal sign connection
>>> L4=[a+"="+str(b) for a,b in {"one":1,"two":2}.items()]
>>> print(L4)
['two=2', 'one=1']

#Sometimes when we generate a group of lists, if we use case conversion functions, but the generated formula contains numeric types, an exception will be triggered, such as
 L5=[x.lower() for x in ["A","B",1,2]]

#You can use the following method to solve this problem. Isinstance function receives two parameters. The first parameter is the incoming value and the second parameter is the comparison type. When the incoming value matches the comparison type, it returns true. In the following list generation formula, according to the return principle of and and or in Python, when isinstance (x, STR) is true, it returns X. lower(), That is, if x is a string, it will be converted to lowercase, and when isinstance (x, STR) is false, "isinstance (x, STR) and X. lower()" is false, return x after or, that is, add x to the list without any modification
>>> L5=[isinstance(x,str) and x.lower() or x for x in ["A","B",1,2]]
>>> print(L5)
['a', 'b', 1, 2]

When creating a list, the “list generation formula” used above has calculated all the members of the list and stored them in memory. This is not scientific. Sometimes the generation formula we created can calculate many elements. If there are too many elements, it will occupy a lot of memory, and we only need to access some elements in the list, At this time, you can use another way to create a list – “generator”;

Unlike “list generator”, what “list generator” saves is only the list generation algorithm itself. Only when we iterate the “list generator” object, can we execute the algorithm to calculate the value we visit. Please see the following example:

#To create a list generator instead of a list generator, you just need to change the brackets to parentheses
>>> L6=(x*x for x in range(0,10) if x%2==0)
>>> print(L6)
<generator object <genexpr> at 0x7faec4938410>

#There are two ways to let the generator execute the algorithm. The first way is to use the next() function. If next() has obtained all the values, it will throw a stopiteration exception
>>> print(next(L6))
0
>>> print(next(L6))
4
>>> print(next(L6))
16
>>> print(next(L6))
36
>>> print(next(L6))
64
>>> print(next(L6))
Traceback (most recent call last):
    print(next(L6))
StopIteration

#But the next () function is seldom used in practical application, and the for loop iteration is used instead
>>> L6=(x*x for x in range(0,10) if x%2==0)
>>> for a in L6:
        print(a)   
0
4
16
36
64

0 × 3. Dictionary

In Python, you can create a dictionary by using braces. Each element in the dictionary is stored in the form of “key value pair”. Please see the following example:

#There are two ways to create a dictionary. The first is to create an empty dictionary directly, and then add key values one by one
>>> a={}
>>>A ["breakfast"] = "milk and eggs"
>>>A ["lunch"] = "coke Steak"
>>>A ["dinner"] = fruit salad
>>> print(a)
{breakfast ':  ' Milk and eggs',  ' Dinner ':  ' Fruit salad ',  ' Lunch ':  ' Coke steak

#The second method is to create a dictionary, add all the key values at one time, and separate each group of elements with a colon. Before the colon is "key", and after the colon is "value"
>>> a={breakfast ':  ' Milk and eggs',  ' Dinner ':  ' Fruit salad ',  ' Lunch ':  ' Coke steak

#Python allows different keys to have the same value, so the following syntax is correct
b={"one":"qing","two":"qing"}

#There are two ways to read the value in the dictionary, using "DICTIONARY [key]" directly to read the value, or using "dictionary. Get (key)" to read the value. When using the get () method to get the value, if the get key does not exist, the get () method will return a "None", or the value we specified
>>>   Print (a ["breakfast"])
Milk and eggs
>>>   Print (A.get ("lunch"))
Coke steak
>>> print(a.get("233"))
None
#If there is no "233" in the dictionary, return to the website address
>>> print(a.get("233","www.qingsword.com"))
www.qingsword.com

#To determine whether there is a key value pair in the dictionary, in addition to using the get() method to see whether the return is "None", you can also use the keyword in. If it exists, it will return true, otherwise it will return false
>>>  " Breakfast“   in   a
TRUE

#Read the dictionary keys and values list
>>> print(a.keys())
dict_ Keys (['breakfast ',  ' Dinner ',  ' Lunch '])
>>> print(a.values())
dict_values(['Milk and eggs', '水果沙拉', 'Coke steak'])

#In addition, you can use the items () method to read each set of "key value pairs" in the dictionary
>>> for z in a.items():
        print(z)
('dinner ',  ' Fruit salad ')
('早餐', 'Milk and eggs')
('午餐', 'Coke steak')

#If you use two parameters, you can get the single key and value data in each set of key value pairs
>>> for x,y in a.items():
        print(x,y)
午餐 Coke steak
早餐 Milk and eggs
Fruit salad for dinner

#If you want to delete a "key value pair" in the dictionary, you can use the pop () method to pass a key name in the dictionary to this method, and then you can retrieve and delete the corresponding "key value pair"
>>>A. pop ("breakfast")
>>> print(a)
{ '晚餐': '水果沙拉', '午餐': 'Coke steak'}

0 × 4. Examples of data fragmentation

In Python, the purpose of reading a single character in a string can be realized by data fragmentation. This idea is also applicable to tuples and lists. If the list is stored as a string, the first character of an element can be extracted by fragmentation technology. This method is very practical in some environments. Please see the following example:

#First of all, let's take a look at Python's segmentation method for extracting string data. This example takes out a single character with index position 2 in the string data pointed to by A
>>> a="abcdefg"
>>> print(a[2])
c

#In the list data, each string element can be regarded as a sublist, and the value of the corresponding index position in the sublist can be extracted by using the idea of multidimensional list
>>> b=["www.qingsword.com","abcdefg","12345678"]
>>> print(b[0][4])
q

#Extract the last character of the element with index position 2 in B
>>> print(b[2][-1])
8

Using string slicing technology, the first letter of each element in a group of lists is extracted as the key of the dictionary, and corresponding to the value of this element. The following is a relatively complete program:

#!/usr/bin/env python3
#Create a list of names
a=["Qingsword","John","Tom","George","Kali","Eva"]
x=0
#Create an empty dictionary
b={}
#When x value is less than the number of a list elements, the loop
while x<len(a):
    b[a[x][0]]=a[x]
    x+=1  
print(b)   

#Program output
{'E': 'Eva', 'G': 'George', 'Q': 'Qingsword', 'K': 'Kali', 'J': 'John', 'T': 'Tom'}

In addition to the simple slicing technology described above, python also provides a special slicing technology – “semicolon slicing”. Please see the following example:

#Semicolon slicing is also applicable to string or tuple lists. The syntax is "[start index: end index (not included)]"

#Start slicing from the position where the index of string a is 2 (3) to the position where the index is 6 (7), and intercept the data output (including the starting index value and excluding the ending index value)
>>> a="123456789"
>>> print(a[2:6])
3456

#From the position of index 3 (d) in B to the position of index 5 (f)
>>> b=["a","b","c","d","e","f","g"]
>>> print(b[3:5])
['d', 'e']

#Let's look at a few examples to create a list of elements 1-9
>>> L=list(range(1,10))
>>> print(L)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#Take out all the elements in the list from the penultimate element to the end
>>> print(L[-3:])
[7, 8, 9]

#Take out the list from the penultimate element to the penultimate element (excluding the last element)
>>> print(L[-3:-1])
[7, 8]

#If you start with the first element, you can omit the 0 before the colon
>>> print(L[:3])
[1, 2, 3]

#Take every other element from the first to the sixth elements (in steps of 2)
>>> print(L[:6:2])
[1, 3, 5]

#All list elements, one for every other element
>>> print(L[::2])
[1, 3, 5, 7, 9]

#Nothing is written in brackets. There is only one colon, which takes all the elements
>>> print(L[:])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

#The same fragmentation applies to tuples and strings
>>> T=tuple(range(1,10))
>>> print(T[::2])
(1, 3, 5, 7, 9)
>>> s="www.qingsword.com"
>>> print(s[::2])
'wwqnsodcm'

Recommended Today

Kafka source learning: Kafka APIs leader_ AND_ ISR

Link to the original text:https://fxbing.github.io/2021…This source code is based on Kafka 0.10.2 Whenever the controller changes state, it will call thesendRequestsToBrokersMethod sendleaderAndIsrRequestThis paper mainly introduces the logic and process of Kafka server processing the request. LEADER_AND_ISR Overall logical process case ApiKeys.LEADER_AND_ISR => handleLeaderAndIsrRequest(request) Receive leader on server side_ AND_ After ISR request, thehandleLeaderAndIsrRequestThe processing flow […]