Skip to content

Exercices from Javier Rodriguez, for dict-tupes-list - #24

Open
xrodrimo wants to merge 3 commits into
Ironhack-data-bcn-oct-2023:mainfrom
xrodrimo:main
Open

xrodrimo wants to merge 3 commits into
Ironhack-data-bcn-oct-2023:mainfrom
xrodrimo:main

Conversation

@xrodrimo

Copy link
Copy Markdown

No description provided.

@bripollc

bripollc commented Oct 11, 2023

Copy link
Copy Markdown

Hola Javi,

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

Challenge 1

  1. Now try to append the following elements to tup.
tup_append = tup + ("r","o", "n", "h", "a", "c", "k")
print(tup_append)

De esta manera te funcionará:)

  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. What is the index number of "h" in tup3?
    tup3.index("h")

El método correcto sería usando .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:
letters = ["a", "b", "c", "d", "e"]
for i in letters:
    if i in tup3:
        print(i, "is True")
    else:
        print(i, "is False")
  1. How many times does each letter in letters appear in tup3?
letters = ["a", "b", "c", "d", "e", "r"]

counter = 0

for i in letters:
    counter = 0
    if i in tup3:
        counter += counter+1
        print (i, "appears", counter, "times")
    else:
        print (i, "doesn't appear")

Challenge 2

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

Debes usar "random.randint()" ya que te pide que que los valores de la lista sean aleatorios pero no sean únicos.

  1. Convert sample_list_2 to a set called set2. Print the length of the set. Is its length still 80?

Al hacer el "random.randint()" en el anterior ejercicio, has generado una lista con valores random repetidos. Enconces, la len del set debería salirte inferior debido a que los valores repetidos se eliminan en el set.

  1. Using the Python Set union method, aggregate set3, set4, and set5. Then aggregate set1 and set2.
set3_4_5 = set3.union(set4, set5)
set1_2 = set1.union(set2)
print(set3_4_5 == set1_2)

Puedes unir los sets de golpe separándolos con comas. Tu código puede generar resultados diferentes si hay duplicados entre los conjuntos involucrados.

  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.

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