Skip to content

Sara Benitez-Inglott Lab 2 - #26

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

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

Conversation

@sarabenitezinglott

Copy link
Copy Markdown

No description provided.

@bripollc

bripollc commented Oct 11, 2023

Copy link
Copy Markdown

Hola Sara!!!

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

Challenge 1

  1. How about re-assign a new value to an existing tuple?
tup = list(tup)
tup = "I","r","o","n","h","a","c","k",
tup = tuple(tup)
tup

Tuples are immutable but we can:
1. Convert the tuple to a list
2. Reasign values (edit content)
3. Convert list back into a tuple
This works because they are same length.
It's the same as just overwriting the tuple.

  1. Count the number of elements in tup1 and tup2. Then add the two counts together and check if the sum is the same as the number of elements in tup3.
sum1 = len(tup1)
sum2 = len(tup2)
sum12 = sum1 + sum2
print(sum12)

sum3 = len(tup3)
print(sum3)

En tu código, estas imprimiendo la longitud del tup1 (4) y el tup 2 (4) por separado. Te pide que los sumes, por lo que debes añadir un contador que sume la longitud de ambos tups para que te de 8. Entonces si será igual a la longitud de la tup3, que también es 8.

  1. What is the index number of "h" in tup3?
    tup3.index("h")

En tu código estás accediendo al elemento en la posición [4] y asignándolo a la variable i_tup3_h.
El método correcto sería .index() para encontrar la posición (número) del elemento "h" en tup3.

  1. Now, use a FOR loop to check whether each letter in the following list is present in tup3:

Tienes mal identado el "else". Debería estar a la misma altura que el "if", ya que sino el "False" se imprimirá después de que el bucle haya terminado.

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 especificado un range(80), por lo que contendrá valores comprendidos en un rango de 0 a 79. En el ejercicio te pide que el rango sea de 0 a 100.

  1. Create another list named sample_list_2 with 80 random values.
sample_list_2 = []
for i in range(80):
    sample_list_2.append(random.randint(0,100))
sample_list_2

Idem que el ejercicio anterior. En tu código, has especificado un rango de 80 valores.

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

In your code, with set6 = { }, you are actually creating an empty dictionary (not a set).

  1. Add set3 and set5 to set6 using the Python Set update method.
set6.update(set3,set5)
print(set6)

Tu código está bien, pero te pide que lo hagas con "update" method.

  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 echo al revés.

  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