Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use collections and dictionaries in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use collections and dictionaries in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use collections and dictionaries in Python.

1 set

A collection can be created using curly braces ({}) or the set () function, but an empty collection must be created using the set () function, not {}. Curly braces are used to create an empty dictionary

How to view collection objects:

Print (dir (set))

The 17 built-in methods of the collection

Set.add (x): add elements to the collection

Set.clear (): clear the collection

Set.copy (): returns a shallow copy of the collection

Set.difference (set1 [,...]): returns the difference between two or more sets (set remains the same)

Set.difference_update (set1 [,...]): removes duplicate elements in set with collections such as set1

Set.discard (x): deletes the specified element x from the collection

Set.intersection (set1,set2,...,etc): returns the intersection of several sets (set remains the same)

Set.intersection_update (set1,set2,...,etc): the intersection of several sets and set

Set.isdisjoint (set1): determines whether two sets contain the same element

Set.issubset (set1): determines whether set is a subset of set1

Set.issuperset (set1): determines whether set1 is a subset of set

Set.pop (): randomly removes an element

Set.remove (x): removes the specified element

Set.symmetric_difference (set1): returns a collection of elements that are not duplicated in two sets (set remains the same)

Set.symmetric_difference_update (set): removes the same elements in the collection set that are in the collection set1, and inserts different elements in the collection set1 into the collection set

Set.union (set1,set2,...): returns the union of several sets (set remains the same)

Set.update (set1): adds elements that are in the collection set1 and not in the set to the collection set

2 Dictionary (key:value) d = {key1;value1,key2:value2,...}

Where keys must use immutable data types (numbers, strings, tuples); values can use any data type

Dictionaries access elements through keys, so keys must be unique in a dictionary

Dictionary creation: assignment command = or built-in function dict ()

Dict1 = {} or dict1 = dict ()

Delete a dictionary: delete a dictionary or delete an element of the dictionary (you need to specify a key) you can use the del delete command

There are 11 built-in methods of the dictionary:

Dict.clear (): clear the dictionary

Dict.copy (): returns a shallow copy of the dictionary

Dict.fromkeys (seq [, value]): create a new dictionary

Dict.get (key.default = None): returns the value of the specified key, or default if the value is not in the dictionary

Dict.items (): returns a traverable (key, value) tuple array

Dict.keys (): returns an iterator that can be converted to a list using list (), which contains all the keys in the dictionary.

Dict.setdefault (key,default = None): similar to get (), if the key does not exist in the dictionary, the key is added and the value is set to default

Dict.update (dict1): updates the key-value pair of the dictionary dict1 to dict

Dict.values (): returns an iterator that can be converted to a list with list (), which contains all the values in the dictionary.

Dict.pop (key [, default]): delete the value corresponding to the given key of the dictionary, and the returned value is the deleted value. The key value must be given, otherwise the default value is returned

Dict.popitem (): randomly returns and deletes a pair of keys and values in the dictionary (usually delete the end pair)

Thank you for your reading, the above is the content of "how to use collections and dictionaries in Python". After the study of this article, I believe you have a deeper understanding of how to use collections and dictionaries in Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report