简体   繁体   English

Python 中的集合和列表有什么区别?

[英]What is the difference between sets and lists in Python?

Is the only difference between sets and lists in Python the fact that you can use the union, intersect, difference, symmetric difference functions to compare two sets? Python 中集合和列表之间的唯一区别是您可以使用并集、相交、差值、对称差值函数来比较两个集合吗? Why can't these functions simply be applied to lists?为什么这些函数不能简单地应用于列表? In what situations are sets more useful than lists?在什么情况下集合比列表更有用?

There's a huge difference.有很大的不同。

  1. Sets can't contain duplicates集合不能包含重复项
  2. Sets are unordered集合是无序的
  3. In order to find an element in a set, a hash lookup is used (which is why sets are unordered).为了在集合中查找元素,使用了哈希查找(这就是集合无序的原因)。 This makes __contains__ ( in operator) a lot more efficient for sets than lists.这使得__contains__in运算符)对于集合比列表更有效。
  4. Sets can only contain hashable items (see #3).集合只能包含可散列的项目(参见 #3)。 If you try: set(([1],[2])) you'll get a TypeError .如果你尝试: set(([1],[2]))你会得到一个TypeError

In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.在实际应用中,列表非常适合排序和排序,而当您不想要重复并且不关心顺序时,集合很适合使用。

Also note that if you don't care about order, etc, you can use另请注意,如果您不关心订单等,则可以使用

new_set = myset.intersection(mylist)

to get the intersection between a set and a list .获取setlist之间的交集。

sets — Unordered collections of unique elements sets — 唯一元素的无序集合

lists - ordered collections of elements lists - 元素的有序集合

sets allows you to do operations such as intersection , union , difference , and symmetric difference , ie operations of math's set theory. sets允许您执行诸如intersectionuniondifferencesymmetric difference之类的运算,即数学集合论的运算。 Sets doesn't allow indexing and are implemented on hash tables.集合不允许索引并在哈希表上实现。

lists are really variable-length arrays, not Lisp-style linked lists. lists实际上是变长数组,而不是 Lisp 风格的链表。 In lists the elements are accessed by indices.在列表中,元素通过索引访问。

Set

A set is a collection which is unordered and unindexed, and doesnt allow duplicates.集合是无序和无索引的集合,不允许重复。 In Python, sets are written with curly brackets.在 Python 中,集合是用大括号编写的。

# example set
newset = {"one", "two", "three"}
  • You cannot access items in a set by referring to an index您不能通过引用索引来访问集合中的项目
  • Sets are mutable集合是可变的
  • They are useful for checking for duplicates它们可用于检查重复项

List列表

A list is a collection which is ordered and changeable.列表是有序且可变的集合。 In Python lists are written with square brackets.在 Python 中,列表是用方括号编写的。

# example list
newlist =["one", "two", "three"]
  • You access the list items by referring to the index number您可以通过参考索引号访问列表项
  • Lists are mutable.列表是可变的。

Some more differences are:还有一些区别是:

  1. List can be 2-D whereas a set can't.列表可以是二维的,而集合不能。
  2. As list are ordered (IE. have serial number) list are comparatively slow to execute whereas sets are fast.由于列表是有序的(即有序列号),列表执行起来相对较慢,而集合则很快。
  3. List in python is like Array of java or c. python中的列表就像java或c的数组。
  4. Printing a set almost always provide different sequence of output.打印一组几乎总是提供不同的输出序列。
  5. Set uses hash function to find an element whereas list is an array. Set 使用hash function来查找元素,而 list 是一个数组。 Hence finding element in Set is faster than in list .因此在Set查找元素比在list更快。

Set represents a collection of distinct elements. Set 表示不同元素的集合。 In python, sets are mainly used for two reasons (Book: Data Science from Scratch, Joel Gruce):在python中,使用集合主要有两个原因(书籍:Data Science from Scratch,Joel Gruce):

  1. For faster operation: in is a very fast operation on sets.为了更快的操作: in是对集合的非常快的操作。 If we have a large collection of elements and if we wish to perform membership test, in that case it is appropriate to use set instead of a list.如果我们有大量元素并且希望执行成员资格测试,在这种情况下,使用 set 而不是列表是合适的。

  2. To find a distinct items in a collections.在集合中查找不同的项目。 Programmers use sets much less frequently than dicts and lists.程序员使用集合的频率远低于字典和列表。

Actually there are four collection data types of in python:实际上python中有四种集合数据类型:

List is a collection which is ordered and changeable. List是一个有序且可变的集合。 Allows duplicate members.允许重复成员。

Tuple is a collection which is ordered and unchangeable.元组是一个有序且不可更改的集合。 Allows duplicate members.允许重复成员。

Set is a collection which is unordered and unindexed. Set是一个无序且无索引的集合。 No duplicate members.没有重复的成员。

Dictionary is a collection which is unordered, changeable and indexed.字典是一个无序、可变和索引的集合。 No duplicate members.没有重复的成员。

You can access a list item by referring to its index.您可以通过引用其索引来访问列表项。 however, in sets , you need to loop through the set items in order to access it.但是,在set 中,您需要遍历 set 项才能访问它。

source: https://www.w3schools.com/python/python_sets.asp来源: https : //www.w3schools.com/python/python_sets.asp

Difference Between Sets and Lists Here we will discuss the difference between Sets and List in Python. Sets 和 Lists 的区别这里我们将讨论 Python 中 Sets 和 List 的区别。

Lists 1) Lists save elements in the order they are inserted.列表1) 列表按元素插入的顺序保存元素。 2) Lists support indexing. 2) 列表支持索引。 3) We can change the value of the element stored in the lists. 3) 我们可以改变存储在列表中的元素的值。 4) Lists can store duplicate values. 4) 列表可以存储重复值。 5) Lists are declared using square brackets. 5) 列表使用方括号声明。 6) Example: A = [1, 2, 3, 4, 5, 1, 2, 3] 6) 例子:A = [1, 2, 3, 4, 5, 1, 2, 3]

Sets 1) Sets do not save elements in the order they are inserted.集合1) 集合不会按照元素插入的顺序保存元素。 2) Sets do not support indexing. 2) 集合不支持索引。 3) We cannot change the value of the element stored in the sets. 3) 我们不能改变存储在集合中的元素的值。 4) Sets cannot store duplicate values. 4) 集合不能存储重复值。 5) Sets are declared using curly brackets. 5) 使用大括号声明集合。 6) Example: A = {1, 2, 3, 4, 5} 6) 例子:A = {1, 2, 3, 4, 5}

Learn more on Sets with Example on the link given below https://tutorialsimpact.com/python/sets-in-python在下面给出的链接上了解有关 Sets with Example 的更多信息https://tutorialsimpact.com/python/sets-in-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM