Skip to content

aida garcia d - #12

Open
aidagarcd wants to merge 1 commit into
Ironhack-data-bcn-oct-2023:mainfrom
aidagarcd:main
Open

aidagarcd wants to merge 1 commit into
Ironhack-data-bcn-oct-2023:mainfrom
aidagarcd:main

Conversation

@aidagarcd

Copy link
Copy Markdown

No description provided.

@bripollc

bripollc commented Oct 11, 2023

Copy link
Copy Markdown

Hola Aída,

Felicidades por tu segundo lab!!!!!!! Nadie dijo que fuera fácil. Te dejo a continuación las correcciones.

Challenge 1: está perfecto, buen trabajo!

Challenge 2

  1. In the cell below, create a list named sample_list_1 with 80 random values.
sample_list_1 = random.sample(range(100), k=80)
print(sample_list_1)

En tu código has usado random.randint(a, b) por lo que te genera valores aleatorios pero no únicos. En el enunciado especifica que deben ser únicos por lo se debería usar random.sample(a, b)

  1. Identify the elements present in set1 but not in set2. Assign the elements to a new set named set3.
set3 = []
for i in set1:
    if i not in set2:
        set3.append(i)
set3 = set(set3)
print(set3)

En primer lugar estas definiendo set3 como un diccionario {}. Por otro lado, dentro del bucle estás sobrescribiendo set3 en cada iteración con el valor de i. Se debería usar set3.append(i) para agregar elementos a la lista:) Por último, en tu código el print debe ir fuera del bucle!

Igualmente, te dejo una opción más rapida usando difference:

set3 = set1.difference(set2)
print(set3)
  1. Identify the elements present in set2 but not in set1. Assign the elements to a new set named set4.

Idem que en el ejercicio anterior.

  1. Now Identify the elements shared between set1 and set2. Assign the elements to a new set named set5.
set5 = set1.intersection(set2)
print(set5)

Para encontrar los elementos compartidos entre ambos sets puedes usar intersection.

  1. What is the relationship among the following values:
    len(set1) == ( len(set3) + len(set5) )

Aquí te pedia relaciones entre los sets. Te dejo un ejemplo. Si lo ejecutas te tendría que salir True:)

  1. Create an empty set called set6.
    set6 = set()

En tu código estas creando un diccionario {}.

  1. Check if set1 contains set2 using the Python Set issubset method. Then check if set1 contains set3.*
print(set2.issubset(set1))
print(set3.issubset(set1))

Ojo! Lo has hecho al revés.

  1. Using the Python Set union method, aggregate set3, set4, and set5. Then aggregate set1 and set2. Check if the aggregated values are equal.
set3_4_5 = set3.union(set4, set5)
set1_2 = set1.union(set2)

print(set3_4_5 == set1_2)

El enunciado indica que primero debes agrupar el set 3, 4 y 5, y luego agrupar el set 1 y el 2. Luego, puedes comprobar que ambos son iguales.

  1. Using the pop method, remove the first element from set1.
set1_list = list(set1)
set1_list.pop(0)
set1_list_pop = set(set1_list)  # And we convert it back to a set
print(set1_list_pop)

The pop method removes an element randomly, thus we could convert it to list to pop the first element.

  1. Remove every element in the following list from set1 if they are present in the set. Print the remaining elements.
remaining_elem = []
list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]

for i in set1:
    if i not in list_to_remove:
        remaining_elem.append(i)
print(remaining_elem)

Challenge 3

  1. Sort the keys of word_freq ascendingly.
keys_list = list(word_freq.keys()) #extract the keys from the word_freq dictionary
keys_list.sort()  # sort them alphabetically
print(keys_list)
# Create an empty dictionary word_freq2 
word_freq2 = {}

# Find the corresponding value in word_freq and insert the key-value pair to word_freq2
for key in keys_list:
    word_freq2[key] = word_freq[key]
    
print(word_freq2)
  1. Sort the values of word_freq ascendingly.
import operator
sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))
print(sorted_tups)
word_freq3 = {}

for i in sorted_tups:
    for key,value in word_freq.items():
        if key == i[0]: 
            word_freq3[key] = value

print(word_freq3)

sigue así!!!! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants