zoukankan      html  css  js  c++  java
  • [Python] Tuples

    Python provides another useful built-in type: tuples. Tuples are used to store related pieces of information. Consider this example involving latitude and longitude:

    >>> AngkorWat = (13.4125, 103.866667)
    >>> print(type(AngkorWat))
    <class 'tuple'>
    >>> print("Angkor Wat is at latitude: {}".format(AngkorWat[0]))
    Angkor Wat is at latitude: 13.4125
    >>> print("Angkor Wat is at longitude: {}".format(AngkorWat[1]))
    Angkor Wat is at longitude: 103.866667

    Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indexes (for example AngkorWat[0] and AngkorWat[1]). Unlike lists, tuples are immutable. You can't add and remove items from tuples, or sort them in place. 

    Why Tuples?

    Why do we have tuples if they're like lists with less features? Tuples useful when you have two or more values that are so closely related that they will always be used together, like latitude and longitude coordinates.

    Tuples can be used to assign multiple variables in a compact way:

    >>> dimensions = 52, 40, 100 
    >>> length, width, height = dimensions 
    >>> print("The dimensions are {}x{}x{}".format(length, width, height))
    The dimensions are 52x40x100
    world_heritage_locations = {(13.4125, 103.866667): "Angkor Wat",
                                (25.73333, 32.6): "Ancient Thebes",
                                (30.330556, 35.4433330): "Petra",
                                (-13.116667, -72.583333): "Machu Picchu"}

    Notice that the values assigned to the tuple dimensions aren't surrounded with parentheses as previous examples were. The parentheses are optional when making tuples, and programmers frequently omit them if parentheses don't clarify the code.

    Tuple Unpacking

    In the second line, three variables are assigned from the content of the tuple dimensions. This is called tuple unpacking. You can use tuple unpacking to assign the information from a tuple into multiple variables without having to access them one by one and make multiple assignment statements.

    In this example, if we won't need to use dimensions directly, we could shorten those two lines of code into a single line that assigns three variables in one go!

    length, width, height = 52, 40, 100

    Returning Tuples

    def first_and_last(sequence):
        """returns the first and last elements of a sequence"""
        return sequence[0], sequence[-1]
    
    >>> first_and_last(["Spam", "egg", "sausage", "Spam"])
    ('Spam', 'Spam')

    A function that returns a tuple can also be used to assign multiple variables:

    >>> start, end = first_and_last(["Spam", "egg", "sausage", "Spam"])
    >>> print(start)
    Spam
    >>> print(end)
    Spam
    def hours2days(_hours):
        days = _hours//24
        hours = _hours%24
        return days, hours
         
    hours2days(24) """(1, 0)"""
    hours2days(25) """(1,1)"""
    hours2days(10000)  """(416, 16)"""
  • 相关阅读:
    VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
    VS2010/MFC编程入门之二(利用MFC向导生成单文档应用程序框架)
    VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)
    Getmemory问题
    计算后缀表达式
    0-1背包问题
    不抛异常的swap函数
    输出n*n矩阵
    字符串全排列输出
    判断主机字节
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7906353.html
Copyright © 2011-2022 走看看