Home  >  Article  >  Backend Development  >  python sequence basics - tuples

python sequence basics - tuples

巴扎黑
巴扎黑Original
2017-07-18 13:54:361464browse

Tuple, like a list, is a member of the sequence. The difference is that it is an immutable sequence

Tuple declaration:

1. Empty tuple: ()

2. Tuple with 1 member: (1,) or 1,

3. Multiple members: (1,2) or 1,2

Note: When declaring a tuple, parentheses are not necessary, but commas are important. A tuple declaring one member must have a comma

tuple() method

Method explanation: You can convert other sequences into Tuple, the usage is consistent with list()

Other common operations on sequences, please see

Using basic functions, tuples can be replaced by lists

Tuples exist Meaning:

1. Tuples can be used as keys in mappings

2. Tuples are used as return values ​​by many built-in functions and methods

Tuples

Tuples are immutable(=String), that means you can't do with tuples like this:

tuple.sort()

tuple.append (5)

tuple.reverse()

These are all built-in methods (methods like object.function) that will actually change themselves.

Comma is the symbol of tuple:

x = 4,5,6

print x

print 3*(40+2),3 *(40+2,)


The biggest use of Tuple is to act as a temporary, fixed-length variable (just like wanting the values ​​in a dictionary to be sorted by value rather than key ):

Suppose there is a dict: {'csev': 2, 'zqian': 1, 'cwen': 4}


[python] view plain copy

temp = list()

for k,v in dict.items():

temp.append( (v,k) ) # notice there is a tuple

temp.sort(reverse = True)

print temp

This way you can achieve the purpose of finding the maximum value (count the numbers with the highest frequency)

tuples can not only contain constant, the following code:

a = 1

b = 99.0

c = 'hello'

tuple0 = (a, b, c, 1)

print tuple0

tuples can also contain variables, combinations of variables and constants, of which tuple0 itself is a variable.

List

List are mutable, everything that can be done with sequences also applies to lists.

Give a list for subsequent operations:

[python] view plain copy

list0 = [1, 2, 'joe', 99.0]

1. Convert lists and strings to each other:

[python] view plain copy

lst = list('hello')

print lst, ''.join( lst)

2. Change the list - you need to specify the list subscript

element assignment:

[python] view plain copy

list0 = [1, 2, 'joe', 99.0]

list0[1] = 3

print list0

list0[99] = 'error' # index out of range

Delete elements at specific positions:

list0 = [1, 2, 'joe', 99.0]

del list0[1]

print list0

Selective assignment - sharding



change value

name = list( 'Perl')

name[2:] = list('ar')

print name

# change list length and value

name[ 1:] = list('ython')

print name

# insert

numbers = [1,5]

numbers[1:1 ] = [2,3,4]

numbers[0:0] = [0]

print numbers

# delete

numbers[1 :5] = []

print numbers

The replacement value of the fragment must be a list


3. Append a new object (an element, pay attention to the following extend) at the end append()

list0 = [1, 2, 'joe', 99.0]

list0.append([1, 2])

print list0


The above is the detailed content of python sequence basics - tuples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn