python basis - tuples built-in method

Tuple type built-in method

Tuples are immutable list, i.e., value tuples can not be changed, and therefore generally used only for only the tuple memory requirements are not taken. Thus tuples may also be substituted out list, so compared to the list of tuples used rarely. Tuple is a list of advantages compared to: modify the value list, the list structure will change, but only need to store tuples, in some extent, so the list require more memory. But now the industry is not a memory problem, so the industry group generally does not use Spring.

  • 1. Usage: more equipment, more loving, and more courses, and even more girlfriend

  • 2. Definition: () can have the values ​​of a plurality of any type, a comma-separated elements
# my_girl_friend = tuple(('dilireba','liuyifei','yangmi'))
my_girl_friend = ('dilireba','liuyifei','yangmi')

print(f"my_girl_friend: {my_girl_friend}")

my_girl_friend: ('dilireba','liuyifei','yangmi')

name_str = ('nash')  # ()只是普通包含的意思
name_tuple = ('nash',)

print(f"type(name_str): {type(name_str)}")
print(f"type(name_tuple): {type(name_tuple)}")

type(name_str): <class 'str'> type(name_tuple): <class 'tuple'>

  • 3. The method of common operations built +: built-in method and common operations:

    Priority control (*****)

     1. index value

 2. Section (care regardless of the end, step)

 3. length len

 4. Members and not in operation in

 5. cycle

 6.count

 7.index
1. index value

# tuple之索引取值
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')
# name_tuple[0] = 'nick handsom'  # 报错

print(f"name_tuple[0]: {name_tuple[0]}")

name_tuple[0]: nash
2. Section (care regardless of the end, step)

# tuple之切片
name_tuple = ('nash', 'langyigang', 'fujiacheng', 'jinyi')

print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")

name_tuple[1:3:2]: ('langyigang',)
3. length

# tuple之长度
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')

print(f"name_tuple: {len(name_tuple)}")

name_tuple: 4
4. The members of the operation

# tuple之成员运算
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')

print(f"'nash' in name_tuple: {'nash' in name_tuple}")

'nash' in name_tuple: True

5. cycle

# tuple之循环
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')

for name in name_tuple:
    print(name)

nash langyigang fujiachen jinyi
6.count()

# tuple之count()
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')

print(f"name_tuple.count('nash'): {name_tuple.count('nash')}")

name_tuple.count('nash'): 1

7.index()

# tuple之index()
name_tuple = ('nash', 'langyigang', 'fujiachen', 'jinyi')

print(f"name_tuple.index('nash'): {name_tuple.index('nash')}")

name_tuple.index('nash'): 0

Stored value or a plurality of values:

A value

Ordered or disordered:

Ordered

name_tuple = ('nash',)
print(f'first:{id(name_tuple)}')

first:4394454152

Variable or invariable:

Immutable data type

The difference between tuples and lists of

l = ['a', 'b', 'c']
print(f"id(l[0]): {id(l[0])}")
l[0] = 'A'
print(f"id(l[0]): {id(l[0])}")

id(l[0]): 4357367208 id(l[0]): 4357775176
Variable is a list of reasons: the memory address corresponding to the index value may be varied

Tuples can not become the reason is: the index of the corresponding memory address values ​​can not change, or conversely, as long as the memory address corresponding to the index value has not changed, then the tuple is never changed.

t1 = (['a', 'b', 'c'], 'wc', 'office')

print(f"id(t1[0]): {id(t1[0])}")
print(f"id(t1[1]): {id(t1[1])}")
print(f"id(t1[2]): {id(t1[2])}")

t1[0][0] = 'A'
print(f"t1[0][0]: {t1[0][0]}")
print(f"id(t1[0]): {id(t1[0])}")
print(f"t1: {t1}")

id(t1[0]): 4394709960 id(t1[1]): 4374626968 id(t1[2]): 4394453568 t1[0][0]: A id(t1[0]): 4394709960 t1: (['A', 'b', 'c'], 'wc', 'office')

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374802.html