Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What does tuple mean in python?

↑↑↑ Click on the blue word above to reply to the message. 10 G surprise.

Author: Chen tooyoung@ Alibaba Cloud Python Training Camp blog address:/weixin _ 43

What does tuple mean in python?

↑↑↑ Click on the blue word above to reply to the message. 10 G surprise.

Author: Chen tooyoung@ Alibaba Cloud Python Training Camp blog address:/weixin _ 43

What does tuple mean in python?

↑↑↑ Click on the blue word above to reply to the message. 10 G surprise.

Author: Chen tooyoung@ Alibaba Cloud Python Training Camp blog address:/weixin _ 43509371/article/details/108522941.

Python is a general programming language, which is widely used in scientific computing and machine learning. If we want to use Python for machine learning, it is very important to have some basic understanding of Python. This Python introductory series experience is carefully prepared for such beginners.

Definition of list

Creation of list

Add an element to the list

Delete an element from the list

Get the elements in the list

General operator of list

Other list methods

tuple

Creating and accessing tuples

Update and delete tuples

Tuple correlation operator

Built in method

Decompression tuple

List simple data types

integer

Floating point type

boolean type

Container data type

Tabulation/listing

tuple

dictionary

gather

character string

The definition list of a list is an ordered collection with no fixed size, which can store any number of Python objects of any type. The syntax is, there are three pointers and three integer objects.

In the operation of x = [a] * 4, only four references to list are created, so once A changes, four A's in X will also change.

x = [[0] * 3] * 4

print(x,type(x))

# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

x[0][0] = 1

print(x,type(x))

# [[ 1, 0, 0], [ 1, 0, 0], [ 1, 0, 0], [ 1, 0, 0]]

a = [0] * 3

x = [a] * 4

print(x,type(x))

# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

x[0][0] = 1

print(x,type(x))

# [[ 1, 0, 0], [ 1, 0, 0], [ 1, 0, 0], [ 1, 0, 0]]

Create a mixed list mix = [1,' lsgo', 3. 14, [1, 2,3]]

Print (Mixed, Type (Mixed))

# [ 1,' lsgo ',3. 14,[ 1,2,3]]

Create an empty list empty = []

print(empty,type(empty)) # []

Unlike tuples, the contents of lists are variable, so you can use operations such as append, expand, insert, delete and pop them.

Add an element to the list

List.append(obj) adds a new object at the end of the list, accepting only one parameter, which can be any data type. The appended elements keep the original structure type in the list.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

X.append ('Thursday')

Print (x)

# ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday',' Thursday' ]print(len(x)) # 6

If this element is a list, then the list will be appended as a whole. Note the difference between append () and extend ().

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

X.append([' Thursday',' Sunday'])

Print (x)

# ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday', ['Thursday',' Sunday']]

Print (lens (x)) # 6

List.extend(seq) Appends multiple values to another sequence at the end of the list (extending the original list with a new list).

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

X.extend([' Thursday',' Sunday'])

Print (x)

# ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday',' Thursday',' Sunday' ]print(len(x)) # 7

Strictly speaking, append is addition, which adds a whole thing to the linked list, and extend is extension, which adds all the elements in one thing to the linked list.

List.insert(index, obj) inserts an obj at the numerical index.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

X.insert(2, "Sunday")

Print (x)

# ['Monday',' Tuesday',' Sunday',' Wednesday',' Thursday',' Friday' ]print(len(x)) # 6

Delete an element from the list

List.remove(obj) removes the first value in the list.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

X.remove ('Monday')

Print(x) # ['Tuesday',' Wednesday',' Thursday',' Friday']

List.pop([index=- 1]) deletes an element from the list (the last element by default) and returns the value of the element.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

y = x.pop()

print(y) # Fridayy = x.pop(0)

Print # Monday

y = x.pop(-2)

Print (y) # Wednesday

Print(x) # ['Tuesday',' Thursday']

Both remove and pop can delete elements, the former is to specify the specific elements to be deleted, and the latter is to specify the index.

Delvar 1 [,var 2...] Delete single or multiple objects.

If you know the position of the element to be deleted in the list, you can use the del statement.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

del x[0:2]

Print(x) # ['Wednesday',' Thursday',' Friday']

Get the elements in the list

Gets a single element from the list by its index value. Note that the index value of the list starts from 0.

By specifying the index as-1, Python can return the last list element, index -2 returns the penultimate list element, and so on.

X = ['Monday',' Tuesday',' Wednesday', ['Thursday',' Friday']]

Print(x[0], type(x[0])# Monday

Print(x[- 1], type(x[- 1])#[' Thursday',' Friday']

Print(x[-2], type(x[-2])# Wednesday

The common writing of slicing is start: stop: step by step.

"Start:"

Slice in step 1 (default) from the beginning of the number to the end of the list.

X = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

Print(x[3:]) # ['Thursday',' Friday']

Print(x[-3:]) # ['Wednesday',' Thursday',' Friday']

":stop" starts slicing from the top of the list and stops numbering in steps of 1 (the default value).

Week = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

Print(week[:3]) # ['Monday',' Tuesday',' Wednesday']

Print(week[:-3]) # ['Monday',' Tuesday']

"Start: Stop" starts with numbers and stops slicing with steps of 1 (the default).

Week = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

Print(week[ 1:3]) # ['Tuesday',' Wednesday']

Print(week[-3:- 1]) # ['Wednesday',' Thursday']

-"Start: Stop: Step" slices from digital to digital in specific steps. Note that setting step to-1 at the end is equivalent to reversing the list.

Week = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

Print(week[ 1:4:2]) # ['Tuesday',' Thursday']

Print(week[:4:2]) # ['Monday',' Wednesday']

Print(week[ 1::2]) # ['Tuesday',' Thursday']

Print (Week [:-1])

# ['Friday',' Thursday',' Wednesday',' Tuesday',' Monday']

":"Copies all elements in the list (simple copy).

Week = ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

Print (Week [:])

# ['Monday',' Tuesday',' Wednesday',' Thursday',' Friday']

List of light copy and deep copy1= [123,456,789,213]

list2 = list 1

list 3 = list 1[:]print(list 2)#[ 123,456,789,2 13]

Print (Listing 3) # [123,456,789,213]

list 1.sort()

Print (Listing 2) # [123,213,456,789]

Print (Listing 3) # [123,456,789,213]

list 1 = [[ 123,456],[789,2 13]]

list2 = list 1

list3 = list 1[:]

print(list2) # [[ 123,456],[789,2 13]]

print(list3) # [[ 123,456],[789,2 13]]

list 1[0][0]= 1 1 1

Print (Listing 2) # [[11,456], [789,213]]

print(list 3)#[[ 1 1 1,456],[789,2 13]]

General operator of list

Equal sign operator: = =

Join operator+

Repeat operator *

The membership operator is in, not in.

"Equal sign = =" returns True only when the member and its position are the same.

There are two ways to splice lists, using "plus sign+"and "multiply sign *". The former is end-to-end splicing, and the latter is copy splicing.

list 1 = [ 123,456]

list2 = [456, 123]

list3 = [ 123,456]

print(list 1 = = list 2)# False

print(list 1 == list3) # True

list4 = list 1 + list2 # extend()

Print (Listing 4) # [123,456,456, 123]

Listing 5 = Listing 3 * 3

Print (Listing 5) # [123,456,123,456,123,456]

Listing 3 *= 3

Print (Listing 3) # [123,456,123,456,123,456]

Print (123 in Listing 3) # true

Print(456 is not in Listing 3) # False

The first three methods (append, extend, insert) can add elements to the list. Instead of returning a value, they directly modify the original data object. When adding two lists, you need to create a new list object, which will consume extra memory. Especially when the list is large, try not to use "+"to add the list.

Other list methods

List.count(obj) counts the number of times an element appears in the list.

list 1 = [ 123,456] * 3

print(list 1) # [ 123,456, 123,456, 123,456]

num = list 1 . count( 123)

Print (No.) # 3

List.index(x[, start[, end]]) finds the index position of the first matching value in the list.

list 1 = [ 123,456] * 5

print(list 1 . index( 123))# 0

print(list 1 . index( 123, 1)) # 2

print(list 1 . index( 123,3,7)) # 4

List.reverse () invert that elements in the list.

x = [ 123,456,789]

X. inversion ()

Print (x) # [789,456, 123]

List. Sort (key = none, reverse = false) sorts the original list.

Key-an element mainly used for comparison, with only one parameter. Specific function parameters are taken from the iterated object, and an element in the iterated object is specified for sorting.

Reverse sort rule, reverse = True in descending order, and reverse = False in ascending order (default).

This method does not return a value, but sorts the objects in the list.

x = [ 123,456,789,2 13]

x.sort()

Print (x)

# [ 123,2 13,456,789]x.sort(reverse=True)

Print (x)

# [789, 456, 2 13, 123]

# Get the second element of the list

def takeSecond(elem):

return elem[ 1]

x = [(2,2),(3,4),(4, 1),( 1,3)]

x . sort(key = take sec)

Print (x)

# [(4, 1), (2, 2), ( 1, 3), (3, 4)]

x . sort(key =λa:a[0])

Print (x)

# [( 1, 3), (2, 2), (3, 4), (4, 1)]

tuple

The definition syntax of "tuple" is: (element 1, element 2, ... element n)

Parentheses bind all elements together.

Commas separate each element one by one.

Creating and accessing tuples Python's tuples are similar to lists, but they cannot be modified after being created, similar to strings.

Use parentheses for tuples and square brackets for lists.

A tuple is similar to a list, and it is also indexed and sliced by integers.

t 1 = ( 1, 10.3 1,' python ')

t2 = 1, 10.3 1,' python '

Print (t 1, type (t 1))

# ( 1, 10.3 1,' python')print(t2,type(t2))

# ( 1, 10.3 1,' python ')

Tuple 1 = (1, 2,3,4,5,6,7,8)

Print (1[ 1]) # 2

print(tuple 1[5:]) # (6,7,8)

print(tuple 1[:5])#( 1,2,3,4,5)

tuple2 = tuple 1[:]

Print (a set of 2) # (1, 2, 3, 4, 5, 6, 7, 8)

You can create a tuple with parentheses () or not. For readability, it is recommended to use ().

When a tuple contains only one element, you need to add a comma after the element, otherwise parentheses will be used as operators.

x = ( 1)

Print (type (x)) #

x = 2,3,4,5

Print (type (x)) #

x = []

Print (type (x)) #

x =()

Print (type (x)) #

x = ( 1,)

Print (type (x)) #

Print (8 * (8)) # 64

print(8 * (8,)# (8,8,8,8,8,8,8,8)

Create a two-dimensional tuple.

x = ( 1, 10.3 1,' python '),(' data ', 1 1)

Print (x)

# (( 1, 10.3 1,' python '),(' data ', 1 1))

Print (x[0])

# ( 1, 10.3 1,' python ')

print(x[0][0],x[0][ 1],x[0][2])

# 1 10.3 1 python

print(x[0][0:2])

# ( 1, 10.3 1)

Update and delete tuples week = ('Monday',' Thursday',' Thursday',' Friday')

Week = week[:2]+('Wednesday',)+week[2:]

Print (Week) # ('Monday',' Tuesday',' Wednesday',' Thursday',' Friday')

Tuples are immutable, so we can't directly assign values to the elements of tuples, but as long as the elements in tuples are mutable, we can directly change their elements. Note that this is different from assigning values to their elements.

t 1 = ( 1,2,3,[4,5,6])

print(t 1) # ( 1,2,3,[4,5,6])

t 1[3][0] = 9

print(t 1) # ( 1,2,3,[9,5,6])

Tuple correlation operator

Equal sign operator: = =

Join operator+

Repeat operator *

The membership operator is in, not in.

"Equal sign = =" returns True only when the member and its position are the same.

There are two ways to splice tuples, using "plus sign+"and "multiplication sign *". The former is end-to-end splicing, and the latter is copy splicing.

t 1 = ( 123,456)

t2 = (456, 123)

t3 = ( 123,456)

print(t 1 == t2) # False

print(t 1 == t3) # True

t4 = t 1 + t2

Print (T4) # (123,456,456,123)

t5 = t3 * 3

Print (t5) # (123,456,123,456,123,456)

t3 *= 3

Print (T3) # (123,456,123,456,123,456)

Print (123 in T3) # true

Print (456 is not in t3) # False

Built in method

The size and content of tuples cannot be changed, so there are only two methods: counting and indexing.

t = ( 1, 10.3 1,' python ')

print(t . count(' python '))# 1

print(t . index( 10.3 1))# 1

Count('python') is a record of how many times this element appears in tuple T, which is obviously 1 time.

Index( 10.3 1) is the index to find the elements in tuple t, which is obviously 1.

Decompression tuples unpack one-dimensional tuples (the left parenthesis of several elements defines several variables).

t = ( 1, 10.3 1,' python ')

(a,b,c) = t

Print (a, b, c)

# 1 10.3 1 python

Decompress two-dimensional tuples (define variables according to the tuple structure in tuples)

t = ( 1, 10.3 1,(' OK ',' python '))

(a,b,(c,d)) = t

Print (a, b, c, d)

# 1 10.3 1 OK python

If you only want a few elements in a tuple, use the wildcard "*", which is called a wildcard in English, and use computer language to represent one or more elements. The following example is to throw multiple elements to a rest variable.

t = 1,2,3,4,5

A, b, * rest, c = t

print(a,b,c) # 1 2 5

Print (Remaining) # [3,4]

If you don't care about the rest variable at all, then use the wildcard "*" and underline "_".

t = 1,2,3,4,5

a,b,*_ = t

print(a,b) # 1 2