Based on the use of tuples

Tuple --tuple

List is very suitable for running may change during storage of the data set.

The list can be amended, but tuples can not be modified

The list can not be modified Python called invariable value, the variable is not referred to as a tuple

1. Create and delete tuples

  1. Use the assignment operator to directly create a tuple

    grammar:

    tuplename = (element1, element2, Element3, ....)

    In Python tuple using a pair of parentheses to enclose all the elements, but the parentheses are not necessary , as long as a set of values with a comma- separated, it can be Python tuple.

     verse = "渔舟唱晚", "高山流水", "出水莲", "汉宫秋月"   # 元组

    If you want to create a tuple of only one element , it is necessary after the element plus the comma , Python otherwise it is a string .

    verse1 = ('一片冰心在玉壶') # 字符串
    verse2 = ('一片冰心在玉壶',)    # 元组
  2. Create an empty tuple

    emptytuple = ()
  3. Creating value tuples

    May be used tuple () function to convert the direct result of the function cycle out of range () is a value-tuple

    tuple(data)

    data- iterable objects

  4. Delete tuple

    the tuplename

del statement in the actual development, not commonly used, because Python comes with garbage collection will not automatically destroy tuple, so even if we do not delete it manually, Python will automatically be recovered.

2. Access tuple element

  1. For loop

    coffee_name = ('蓝山', '卡布奇诺', '曼特宁', '摩卡', '麝香猫', '哥伦比亚')
    print('您好,欢迎光临 ~ 伊米咖啡馆 ~\n我店有:')
    for name in coffee_name:
        print(name, "咖啡", end=' ')
    '''
    result:
    您好,欢迎光临 ~ 伊米咖啡馆 ~
    我店有:
    蓝山 咖啡 卡布奇诺 咖啡 曼特宁 咖啡 摩卡 咖啡 麝香猫 咖啡 哥伦比亚 咖啡 
    '''
    
  2. for + enumerate()

    enumerate (): - Enumeration

    This function is used to traverse a data object (such as a list, a tuple) is a combination index sequence, while the data lists and data standard, generally used in a for loop.

    team = ('火箭', '勇士', '开拓者', '雷霆', '爵士', '马刺')
    for index, item in enumerate(team):
        if index % 2 == 0:
            print(item + '\t\t', end='')
        else:
            print(item)    
    '''
    result:
    火箭       勇士
    开拓者      雷霆
    爵士       马刺
    '''

3. Modify the element of the tuple

Tuples are immutable sequence, the individual elements of the value we can not modify it. By re-assignment of tuples to be modified.

Tuples and tuples can be connected, but both are connected must tuple. Otherwise, it is wrong to connect tuples is only one element, we must not forget the comma.

4. derivations tuple

>>> import random
>>> random_number = (random.randint(10,100) for i in range(10))
>>> random_number
<generator object <genexpr> at 0x0000021A177BDED0>  # 生成器对象
>>> tuple(random_number)            # 转换成元组
(28, 63, 34, 98, 96, 36, 87, 35, 25, 44)

As seen from the results of the above, derivation tuple generated result is not a group or list element, but a generator object , which is derived, and a listing of different type.

To convert the generated object into a tuple using a tuple () method, converted into a list, use the list () method.

Example 1:

# 通过__next()__方法遍历
# 在Python2.x中__next()__方法对应的是next()方法,也是用于遍历生成器对象的。
>>> number = (i for i in range(3))
>>> number
<generator object <genexpr> at 0x0000021A1781EA20>
>>> number.__next__()
0
>>> number.__next__()
1
>>> number.__next__()
2
>>> number.__next__()
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    number.__next__()
StopIteration       # 停止迭代

Example Two:

# 通过for循环遍历
>>> number = (i for i in range(4))
>>> number
<generator object <genexpr> at 0x0000021A1781EA98>
>>> for i in number:
    print(i, end='')
    
0123
>>> print(tuple(number))
()

As can be seen from the two examples above: That way, whether traversal, if you want to use to generate the object again, must recreate a builder object, because the object is traversing the producer does not exist.

5. The difference with the list of tuples

  1. Variable lists, tuples can not be changed unless the entire replacement.
  2. The list can append (), extend (), insert (), remove (), pop () to add and delete functions to achieve, but there is no tuple these methods.
  3. When the list of supported access and modification by trimming, but only supports tuple access, does not support the changes do not make any changes, we recommend using a tuple.
  4. Tuple access and processing speed faster than lists.
  5. The list can not be keys of a dictionary, and tuples can.

Guess you like

Origin www.cnblogs.com/blog-S/p/11249494.html