019 yuan type built-in method group

Ancestral type built-in method (tuple)


A tuple type built-in method (tuple)

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. Definitions: In () the plurality of values ​​may have any type of elements separated by commas


# my_girl_friend = tuple(('jason','tank','sean'))
my_girl_friend = ('jason', 'tank', 'sean')

print(f"my_girl_friend: {my_girl_friend}")
###输出结果:
my_girl_friend: ('jason', 'tank', 'sean')
name_str = ('egon')  # ()只是普通包含的意思
name_tuple = ('egon',)

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:

1.1 Priority control (*****)

  1. Index values
  2. Sections (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

1. index value

# tuple之索引取值
name_tuple = ('reed', 'jason', 'tank', 'sean')
# name_tuple[0] = 'reed handsom'  # 报错

print(f"name_tuple[0]: {name_tuple[0]}")
##输出结果:
name_tuple[0]: reed

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

# tuple之切片
name_tuple = ('reed', 'jason', 'tank', 'sean')

print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")
###输出结果:
name_tuple[1:3:2]: ('jason',)

3. length

# tuple之长度
name_tuple = ('reed', 'jason', 'tank', 'sean')

print(f"len(name_tuple): {len(name_tuple)}")
###输出结果:
len(name_tuple): 4

4. The members of the operation

# tuple之成员运算
name_tuple = ('reed', 'jason', 'tank', 'sean')

print(f"'reed' in name_tuple: {'reed' in name_tuple}")
##输出结果:
'reed' in name_tuple: True

5. cycle

# tuple之循环
name_tuple = ('reed', 'jason', 'tank', 'sean')

for name in name_tuple:
    print(name)
##输出结果:
reed
jason
tank
sean

6.count()

# tuple之count()
name_tuple = ('reed', 'jason', 'tank', 'sean')

print(f"name_tuple.count('reed'): {name_tuple.count('reed')}")
##输出结果:
name_tuple.count('reed'): 1

7.index()

# tuple之index()
name_tuple = ('reed', 'jason', 'tank', 'sean')

print(f"name_tuple.index('reed'): {name_tuple.index('reed')}")
##输出结果:
name_tuple.index('reed'): 0

4. The stored value or a plurality of values: a plurality of values

5. orderly or disorderly: Ordered

name_tuple = ('reed',)
print(f'first:{id(name_tuple)}')
##输出结果:
first:4394454152

6. The variable or non-variable: immutable data type

Second, 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/FirstReed/p/11745908.html