1 of 31

[1]

Programador de sistemas Python

Conjuntos

2 of 31

[2]

Entender o funcionamento da estrutura de dados set.

Objetivo Geral

3 of 31

[3]

  • Python 3
  • VSCode

Pré-requisitos

4 of 31

[4]

Percurso

Etapa 1

Como criar conjuntos

Etapa 2

Métodos da classe set

5 of 31

[5]

Etapa 1

Como criar conjuntos

6 of 31

[6]

Um set é uma coleção que não possui objetos repetidos, usamos sets para representar conjuntos matemáticos ou eliminar itens duplicados de um iterável.

Criando sets

7 of 31

[7]

Exemplo

set([1, 2, 3, 1, 3, 4]) # {1, 2, 3, 4}��set("abacaxi") # {"b", "a", "c", "x", "i"}��set(("palio", "gol", "celta", "palio")) # {"gol", "celta", "palio"}

8 of 31

[8]

Conjuntos em Python não suportam indexação e nem fatiamento, caso queira acessar os seus valores é necessário converter o conjunto para lista.

Acessando os dados

9 of 31

[9]

Exemplo

numeros = {1, 2, 3, 2}��numeros = list(numeros)��numeros[0]

10 of 31

[10]

A forma mais comum para percorrer os dados de um conjunto é utilizando o comando for.

Iterar conjuntos

11 of 31

[11]

Exemplo

carros = {"gol", "celta", "palio"}��for carro in carros:� print(carro)

12 of 31

[12]

Às vezes é necessário saber qual o índice do objeto dentro do laço for. Para isso podemos usar a função enumerate.

Função enumerate

13 of 31

[13]

Exemplo

carros = {"gol", "celta", "palio"}��for indice, carro in enumerate(carros):� print(f"{indice}: {carro}")

14 of 31

[14]

Percurso

Etapa 1

Criação e acesso aos dados

Etapa 2

Métodos da classe set

15 of 31

[15]

Etapa 2

Métodos da classe set

16 of 31

[16]

Percurso

Etapa 1

Criação e acesso aos dados

Etapa 2

Métodos da classe set

17 of 31

[17]

{}.union

conjunto_a = {1, 2}�conjunto_b = {3, 4}��conjunto_a.union(conjunto_b) # {1, 2, 3, 4}

18 of 31

[18]

{}.intersection

conjunto_a = {1, 2, 3}�conjunto_b = {2, 3, 4}��conjunto_a.intersection(conjunto_b) # {2, 3}

19 of 31

[19]

{}.difference

conjunto_a = {1, 2, 3}�conjunto_b = {2, 3, 4}��conjunto_a.difference(conjunto_b) # {1}�conjunto_b.difference(conjunto_a) # {4}

20 of 31

[20]

{}.symmetric_difference

conjunto_a = {1, 2, 3}�conjunto_b = {2, 3, 4}��conjunto_a.symmetric_difference(conjunto_b) # {1, 4}

21 of 31

[21]

{}.issubset

conjunto_a = {1, 2, 3}�conjunto_b = {4, 1, 2, 5, 6, 3}��conjunto_a.issubset(conjunto_b) # True�conjunto_b.issubset(conjunto_a) # False

22 of 31

[22]

{}.issuperset

conjunto_a = {1, 2, 3}�conjunto_b = {4, 1, 2, 5, 6, 3}��conjunto_a.issuperset(conjunto_b) # False�conjunto_b.issuperset(conjunto_a) # True

23 of 31

[23]

{}.isdisjoint

conjunto_a = {1, 2, 3, 4, 5}�conjunto_b = {6, 7, 8, 9}�conjunto_c = {1, 0}��conjunto_a.isdisjoint(conjunto_b) # True�conjunto_a.isdisjoint(conjunto_c) # False

24 of 31

[24]

{}.add

sorteio = {1, 23}��sorteio.add(25) # {1, 23, 25}�sorteio.add(42) # {1, 23, 25, 42}�sorteio.add(25) # {1, 23, 25, 42}

25 of 31

[25]

{}.clear

sorteio = {1, 23}��sorteio # {1,23}�sorteio.clear()�sorteio # {}

26 of 31

[26]

{}.copy

sorteio = {1, 23}��sorteio # {1, 23}�sorteio.copy()�sorteio # {1, 23}

27 of 31

[27]

{}.discard

numeros = {1, 2, 3, 1, 2, 4, 5, 5, 6, 7, 8, 9, 0}��numeros # {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}�numeros.discard(1)�numeros.discard(45)�numeros # {2, 3, 4, 5, 6, 7, 8, 9, 0}

28 of 31

[28]

{}.pop

numeros = {1, 2, 3, 1, 2, 4, 5, 5, 6, 7, 8, 9, 0}��numeros # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}�numeros.pop() # 0�numeros.pop() # 1�numeros # {2, 3, 4, 5, 6, 7, 8, 9}

29 of 31

[29]

{}.remove

numeros = {1, 2, 3, 1, 2, 4, 5, 5, 6, 7, 8, 9, 0}��numeros # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}�numeros.remove(0) # 0�numeros # {1, 2, 3, 4, 5, 6, 7, 8, 9}

30 of 31

[30]

len

numeros = {1, 2, 3, 1, 2, 4, 5, 5, 6, 7, 8, 9, 0}��len(numeros) # 10

31 of 31

[31]

in

numeros = {1, 2, 3, 1, 2, 4, 5, 5, 6, 7, 8, 9, 0}��1 in numeros # True10 in numeros # False