列表,元组,集合和字典之间有什么区别? [英] What is the difference between lists,tuples,sets and dictionaries?

查看:1512
本文介绍了列表,元组,集合和字典之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对有人给我明确想法的列表,元组,集合和字典感到困惑.根据您的理解,请给我带来的区别,不要给出教科书的定义.

I have confused with lists, tuples, sets and dictionaries someone give me clear cut idea. Give me the difference from your understanding don't give the text book definitions.

推荐答案

列表是按特定顺序排列的元素序列.您可以使用数字索引访问元素,例如the_list[3].进行多项操作(例如测试列表中是否包含元素)所花费的时间为O(n),即与列表的长度成比例.

A list is a sequence of elements in a specific order. You can access elements with a numerical index, e.g. the_list[3]. The time taken for several operations such as testing if the list contains an element is O(n), i.e. proportional to the length of the list.

元组基本上是一个不可变的列表,这意味着您不能添加,删除或替换任何元素.

A tuple is basically an immutable list, meaning you can't add, remove, or replace any elements.

一个集合没有顺序,但是相对于列表而言,优点是测试该集合是否包含元素的速度要快得多,而几乎与集合的大小无关.它还具有一些方便的操作,例如联合和相交.

A set has no order, but has the advantage over a list that testing if the set contains an element is much faster, almost regardless of the size of the set. It also has some handy operations such as union and intersection.

字典是从键到值的映射,其中键可以是各种不同的对象,而列表中的键"只能是数字.因此,您可以拥有the_dict = {'abc': 3, 'def': 8},然后the_dict['abc']3.字典中的键很像一组键:它们没有顺序,您可以快速测试它们的存在.

A dictionary is a mapping from keys to values where the keys can be all sorts of different objects, in contrast to lists where the 'keys' can only be numbers. So you can have the_dict = {'abc': 3, 'def': 8} and then the_dict['abc'] is 3. They keys of a dict are much like a set: they have no order and you can test for their existence quickly.

集合的元素和字典的键必须是可哈希的.数字,字符串,元组和许多其他东西都是可哈希的.列表,集合和字典不可散列.

The elements of a set and the keys of a dict must be hashable. Numbers, strings, tuples, and many other things are hashable. Lists, sets, and dicts are not hashable.

这篇关于列表,元组,集合和字典之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆