📜 ⬆️ ⬇️

Python: collections, part 1/4: classification, general approaches and methods, conversion

Part 1Part 2Part 3Part 4
A collection in Python is a program object (container variable) that stores a set of values ​​of the same or different types, allowing you to access these values, as well as use special functions and methods depending on the type of collection.

A frequent problem when studying collections is that after examining each type in some detail, then usually not enough attention is paid to clarifying the picture as a whole, there are no clear similarities and differences between the types, not shown as one and the same task to solve for each of the collections in comparison .

This is exactly the problem I want to try to solve in this series of articles - to consider a number of approaches to working with standard collections in Python in comparison between collections of different types, rather than separately, as is usually shown in the training materials. In addition, I will try to touch on some points that cause difficulties and mistakes for beginners.
')
For whom: for Python learners who already have an initial understanding of the collections and work with them, who want to systematize and deepen their knowledge, put them into a coherent picture.

We will consider the standard built-in collection data types in Python: list (list), tuple (tuple), string (string), sets (set, frozenset), dictionary (dict). Collections from the collections module will not be considered, although much of the article should be applicable when working with them.

TABLE OF CONTENTS:


  1. Classification of collections;
  2. General approaches to working with collections;
  3. General methods for part of collections;
  4. Convert collections .

1. Classification of collections


image

image


Explanation of terminology:


Indexation - each element of the collection has its own sequence number - index. This allows you to access an element by its ordinal index, to carry out slicing (“slicing”) - to take part of the collection by choosing based on their index. These issues will be discussed in detail later in a separate article.

Uniqueness - each element of the collection can appear in it only once. This creates the requirement that the data types used for each element should not change; for example, the list cannot be such an element.

Collection mutability - allows you to add new members to the collection or delete them after creating the collection.

Dictionary Note (dict):



2 General approaches to working with any collection


Having understood the classification, consider what you can do with any standard collection regardless of its type (in the examples list and dictionary, but it works for all other standard collections of the types under consideration):

 #      (   ): my_list = ['a', 'b', 'c', 'd', 'e', 'f'] my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6} 

2.1 Printing collection items using the print () function


 print(my_list) # ['a', 'b', 'c', 'd', 'e', 'f'] print(my_dict) # {'a': 1, 'c': 3, 'e': 5, 'f': 6, 'b': 2, 'd': 4} #  ,        . 

2.2 Counting the number of members of a collection using the len () function


 print(len(my_list)) # 6 print(len(my_dict)) # 6 -    -   . print(len('ab c')) # 4 -     1  

2.3 Verifying the ownership of an item in this collection using the in operator


x in s - returns True if the item is in the s collection and False - if it is not.
There is also the option of checking not the affiliation: x not in s , where in fact there is simply adding a negative before the boolean value of the previous expression.

 my_list = ['a', 'b', 'c', 'd', 'e', 'f'] print('a' in my_list) # True print('q' in my_list) # False print('a' not in my_list) # False print('q' not in my_list) # True 

For the dictionary , the options are clear from the code below:

 my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6} print('a' in my_dict) # True -       print('a' in my_dict.keys()) # True -    print('a' in my_dict.values()) # False -   '' — ,   print(1 in my_dict.values()) # True 

Can I check pairs? Can!

 print(('a',1) in my_dict.items()) # True print(('a',2) in my_dict.items()) # False 

For a string, you can search for not only one character, but also a substring:

 print('ab' in 'abc') # True 

2.4 Traversing all elements of a collection in a for in loop


In this case, the elements of the collection will be sequentially looped through until all of them are enumerated.

 for elm in my_list: print(elm) 

Pay attention to the following points:



2.5 Functions min (), max (), sum ()



 print(min(my_list)) # a print(sum(my_dict.values())) # 21 

3 General methods for part of collections


A number of methods for collection types are used in more than one collection for solving problems of the same type.
image

UPD: Important additions in the third article: Adding and deleting elements of variable collections .

Explanation of the methods and examples:



Special methods for comparing sets (set, frozenset)



 set_a = {1, 2, 3} set_b = {2, 1} #    ! set_c = {4} set_d = {1, 2, 3} print(set_a.isdisjoint(set_c)) # True -    print(set_b.issubset(set_a)) # True - set_b    set_a,  set_b -  print(set_a.issuperset(set_b)) # True - set_b    set_a,  set_a -  

In case of equality of sets, they are both a subset and a superset for each other.
 print(set_a.issuperset(set_d)) # True print(set_a.issubset(set_d)) # True 

4 Converting one type of collection to another


Depending on the tasks at hand, one collection type can be converted to another collection type. For this, as a rule, it is enough to transfer one collection to the function of creating another (they are in the table above).

 my_tuple = ('a', 'b', 'a') my_list = list(my_tuple) my_set = set(my_tuple) #     ! my_frozenset = frozenset(my_tuple) #     ! print(my_list, my_set, my_frozenset) # ['a', 'b', 'a'] {'a', 'b'} frozenset({'a', 'b'}) 

Please note that when converting one collection to another, data loss is possible:



Additional details:



Note : The most powerful and flexible ways - collection generators will be considered separately in the fourth part of the cycle , since there are many nuances and use cases that are rarely focused on and detailed analysis is required.

UPD: ShashkovS in the comments posted links to important and useful information on the algorithmic complexity of operations with collections:
Part 1Part 2Part 3Part 4

I invite you to discuss:


Source: https://habr.com/ru/post/319164/


All Articles