• 1
    Entrada e saída
    • Tarefas
  • 2
    Condições
    • Tarefas
  • 3
    Loop for
    • Tarefas
  • 4
    Strings
    • Tarefas
  • 5
    Loop while
    • Tarefas
  • 6
    Listas
    • Tarefas
  • 7
    Matrizes 2D
    • Tarefas
  • 8
    Dicionários
    • Tarefas
  • 9
    Conjuntos
    • Tarefas
  • 10
    Funções e recursão
    • Tarefas
  • к

Lição 9. Sets

Dificuldade:

Tarefa«Combinação de dados»

💻 Python
Você está planejando uma festa e deseja fazer uma lista de compras final. Você já tem uma lista preliminar, mas alguns convidados sugeriram adicionar suas bebidas e petiscos favoritos. Escreva um programa que ajude você a formar uma lista de compras final, excluindo repetições.

Formato de entrada

Lista de compras preliminar dividida por espaços. Lista de ofertas dos convidados também dividida por espaços

Formato de saída

a lista final de compras ordenada em ordem alfabética, onde cada elemento é exibido em uma linha separada

Exemplo

Entrada

suco água batatas fritas biscoitos
água bife batatas fritas fruta

Saída

Água
Cola
biscoitos
suco
Frutas
Batatas fritas

Dica

Sets in python

Sets in Python are unordered collections of unique elements. They are a powerful tool for working with data when the speed of checking whether an item belongs to a collection and the absence of duplicates is important. They have a number of useful properties and methods that make it possible to efficiently perform mathematical operations on data, such as union, intersection, difference, and others.

Main characteristics of sets

  • Unique elements: There can be no duplicate elements in the set. If you try to add an element that already exists, the array simply won't change. This is their key feature, ideal for removing duplicates.
  • Disorder: The elements of the set do not have a specific order. This means that you cannot access an element by index (for example, my_set[0]), as is done in lists. When iterating over a set, the order of the elements is not guaranteed.
  • Changeability: You can add and remove multiple items. However, the elements of the set themselves must be immutable (hashed) objects. These can be numbers, strings, tuples. You cannot add mutable objects to a set, such as lists or other sets, because their contents may change, which violates the internal logic of sets.

Creating sets

Empty set

To create an empty set , be sure to use the set() function, because the syntax {} creates an empty dictionary. This is a common mistake of novice programmers.

# Correct
my_set = set()

# Wrong (this will create a dictionary)
my_dict = {} 

A set with elements

You can create a set by listing the elements in curly brackets or by passing an iterable object (such as a list) to the set() function..

my_set = {1, 2, 3, 4, 5}

A useful tip: Using the set() functionThis is the easiest way to remove duplicates from the list.

my_list = [1, 2, 2, 3, 4, 3, 5, 5]
unique_elements = set(my_list)
print(unique_elements)  # Output: {1, 2, 3, 4, 5}

Basic operations with sets

Adding elements

To add one element, use the add() method.

my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}

# Trying to add an existing element will not change
anything my_set.add(2)
print(my_set) # Output: {1, 2, 3, 4}

Deleting elements

There are three ways to delete:

  • remove(element): Deletes the specified element. If the element is not found, it causes the KeyError error. Use this method when you are sure that an element should exist, and its absence is an error in the program logic.
  • discard(element): Deletes the specified element, but does not cause an error if the element is not found. This is a safe way to delete when you are not sure if an item is in the set.
  • pop(): deletes and returns an arbitrary element of the set. Since the sets are unordered, you cannot predict which element will be deleted. The method will cause a KeyError if the set is empty.
my_set = {1, 2, 3}
my_set.remove(2)
print(my_set) # Output: {1, 3}

# my_set.remove(4) # This will cause a KeyError error

my_set.discard(4) # There will be no error, the set will remain {1, 3}

popped_element = my_set.pop()
print(popped_element) # Output: 1 or 3 (depends on implementation)
print(my_set) # Output: remaining element

Clearing the set

The clear() method removes all elements from the set, making it empty.

my_set = {1, 2, 3}
my_set.clear()
print(my_set) # Output: set()

Operations on sets

These operations do not change the original sets, but create and return a new one.

Union

Returns a new set containing all the unique elements from both sets. The | operator or the union() method is used. When to use: When you need to collect all the unique items from multiple collections into one.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
# or
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}

Intersection

Returns a new set containing only the elements that are in both sets. The & operator or the intersection() method is used. When to use: When you need to find common elements between two groups.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2
# or
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}

Difference

Returns a new set containing the elements that are in the first set but are missing from the second. The - operator or the difference() method is used. When to use: When you need to find what is in one group but not in another. The order is important: set1 - set2 is not the same as set2 - set1.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}

difference_set_2 = set2 - set1
print(difference_set_2) # Output: {4, 5}

Symmetrical Difference

Returns a new set containing elements that are in one of the sets, but not both at once. The ^ operator or the symmetric_difference() method is used. When to use: When you need to find elements unique to each group, excluding the common ones.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1 ^ set2
# or
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
main.py
Teste 1
Teste 2
Teste 3
Teste 4
Teste 5
Teste 6
Teste 7
Teste 8
Teste 9
Teste 10
Solução do desenvolvedor

🎉 Parabéns! 🎉

Você resolveu a tarefa brilhantemente! Foi difícil, mas você encontrou a solução certa. Você está um passo mais perto de dominar a programação! Continue assim.

AD

Publicidade

red-snake blue-snake green-snake

Executando seu código...

Assistente de IA

Olá! Sou seu assistente de programação. Faça qualquer pergunta sobre Python — posso explicar funções, métodos e ajudá-lo com a tarefa atual!