In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the difference between lists and tuples in python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Similarities: they are all sequence types.
Before answering their differences, let's talk about what they have in common. Both list and tuple are sequence type container objects, which can store any type of data and support slicing, iteration and other operations.
> foos = [0,1,2,3,4,5,6,7,8,9]
> foos [0:10:2]
[0, 2, 4, 6, 8]
> bars = (0,1,2,3,4,5,6,7,8,9)
> bars [1:10:2]
(1,3,5,7,9)
The two operations are so similar, why would Python design a type called tuple? It is necessary to find the answer from their differences.
One difference: immutable VS changeable
Apart from the literal difference between the two types (brackets and square brackets), the most important point is that tuple is an immutable type with a fixed size, while list is a variable type and the data can be changed dynamically. This difference makes the methods, application scenarios and performance provided by the two are very different.
List-specific methods:
>
> foo.sort () # sort
> foo.insert (5pc10) # insert
> foo.reverse () # reverse
> foo.extend ([- 1,-2]) # extension
> foo.remove (10) # remove
> foo.pop () # pops up the last element
> foo.append (5) # append
All operations are updated based on the original list, while tuple, as an immutable data type, initializes and iterates tuple faster than list for the same size of data.
> python-m timeit "[1pm 2pm 3pm 4je 5]"
10000000 loops, best of 3: 0.123 usec per loop
> python-m timeit "(1, 2, 3, 4, 5)"
100000000 loops, best of 3: 0.0166 usec per loop
With the same size of data, tuple takes up less memory space.
Foo = tuple (range (1000))
Bar = list (range (1000))
> > foo.__sizeof__ ()
8024
> > bar.__sizeof__ ()
9088
Atomic tuple objects can also be used as dictionary keys
> foo = (1, (2 ~ (3)
> d = {foo: 1}
> bar = (1, [2jue 3]) # non-atomic tuple, because the tuple contains list that cannot be hashed
> d = {bar: 1}
Traceback (most recent call last):
File "", line 1, in
TypeError: unhashable type: 'list' two different points: isomorphic VS isomerism
Tuple is used to store heterogeneous data as records without field names, such as using tuple to record a person's height, weight, and age.
Person = ("zhangsan", 20,180,80)
Such as recording a point in coordinates.
Point = (x, y)
Lists are generally used to store isomorphic data (homogenous), which is data with the same meaning, for example, the following are string types
["zhangsan", "Lisi", "wangwu"]
Another example is the multiple user records stored in list.
[("zhangsan", 20,180,80), ("wangwu", 20,180,80)]
The records queried in the database operation are the list structure composed of tuples.
Because there are some limitations in using tuple as an unnamed record in some scenarios, there is another namedtuple type, and namedtuple can specify a field name to use as a lightweight class.
This is the end of the article on "what is the difference between lists and tuples in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.