Python's dict and set

Source: Internet
Author: User

Dict

Dict is the abbreviation of dictionary, Python built-in dictionary, in other languages, also known as map, using key values to store, with a very fast search speed.
If you are using list only, you will need two lists, look it up in the first list, find it in the other list, and it's obviously slow. And the list reaches a very long, time-consuming long ...

names = [‘Michael‘, ‘Bob‘, ‘Tracy‘]scores = [95, 75, 85]

Because a key only corresponds to one value, if you assign a value to a key more than once, the value of the preceding key is overwritten.
And if the key does not exist, it will be an error, so we need to check in advance if the key exists.
Delete a key: Using the Pop (key) method, the corresponding value is also deleted.

Dict can be used in many places where you need to tell the search, remember that the Dict key must be an immutable object, that is, string,tuple,numbers.
Dict find the hash algorithm that is actually used.

Set

A set is a collection of keys, but no value is stored. Because key cannot be duplicated, there is no duplicate key in set.

Create a set

Must pass in a list as an input collection

s = set([1,2,3]);s//{1,2,3}

The set receives a parameter that is a list or tuple or other object that can be iterated

L = [x,y,z];s = set(L);

But the elements in L x, y, z have any variable that will cause an error.

adding elements

Can be added repeatedly, but not effective

s.add(4);
Delete Element
s.remove(4);

Set can be seen as a set of unordered and non-repeating elements in mathematical sense, so the two set can Do & (intersection) and | (set) operations.
The only difference between set and Dict is that the set does not store the corresponding alue,set in the same way as dict, nor can it be placed in a mutable object

Python internal pass-through value

Put a piece of code first

# 对list进行测试list0 = [1,2,"tt",["ryr","hr"]];list1 = list0;list1[0] = 11;list1[1] = 22;list1[3][0] = "aa";list1[3][1] = "bb";# //对数字和字符串还有tuple进行测试a = "yrr";b = a;b = "rer";c = ("gr",12,["grr",45]);d = c;d = ("grgr",22,["grr",455]);# c[0] = "grggr";# c[1] = 22;c[2][0] = "ggrgr";c[2][1] = "help";print(c);print(d);print(a);print(b);print(list0);print(list1);


In Python, String,tuples,numbers is a non-modifiable object, and List,dict is an object that can be modified, and I understand that modification and non-modification refer to whether it can be modified on this body.

nfoo = 1nfoo = 2lstFoo = [1]lstFoo[0] = 2

Modify: Although the string,tuples,numbers can be modified, but after the change, the original memory of the numbers is not there, it was "discarded", in memory and re-create a section to store another value.
But for list and tuples, the only change is the content changes, the original block of memory is not discarded, and no new memory is created.

Python's dict and set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.