What is a tuple useful for?

后端 未结 11 796
天涯浪人
天涯浪人 2021-02-01 12:45

I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type wo

11条回答
  •  不知归路
    2021-02-01 13:31

    Tuples make good dictionary keys when you need to combine more than one piece of data into your key and don't feel like making a class for it.

    a = {}
    a[(1,2,"bob")] = "hello!"
    a[("Hello","en-US")] = "Hi There!"
    

    I've used this feature primarily to create a dictionary with keys that are coordinates of the vertices of a mesh. However, in my particular case, the exact comparison of the floats involved worked fine which might not always be true for your purposes [in which case I'd probably convert your incoming floats to some kind of fixed-point integer]

提交回复
热议问题