Nicolas Alpargatero
EstudianteMaga Hernandez
EstudianteEduardo Monzón
EstudianteJhon Sebastián Monsalve Gomez
EstudianteHarold Zurita Simon
EstudianteEdgar Alarcón
EstudianteWaldir Zapata Garcia
EstudianteJohan Espejo
EstudianteDiego Martínez Maqueda
EstudianteEmmanuel Paiva
EstudianteFrancisco Pin
EstudianteManuel Gomez
EstudianteDender Adrian Pernia Valero
EstudianteMarcos Bonifasi
EstudianteEnrique Barrera
EstudianteVictor Javier Vidal Durón
EstudianteBraulio Rangel
EstudianteAlejandro Anaya
EstudianteDidier Fernando Aragón Moreno
EstudianteJosé Miguel Reyes
EstudianteWaldir Zapata Garcia
EstudianteLuis Kennedy Saavedra Fuentes
EstudianteHay que escoger entre las dos declaraciones del Zen de python Flat is better than nested. o Explicit is better than implicit. solo por reto escogí plano es mejor que anidando 🐍 porque entre mas larga sea la línea más complicada de captar rápido.
wow!!
Lo que me gusta de esta solución es que usa el hecho de que no se puede tener la misma llave más de una vez en el diccionario; por ello no es necesario agregar ninguna condición para las palabras que tienen la misma cantidad de letras.
Aqui mi solucion: . . . . . . . . .
def count_words_by_length(words): # Tu código aquí 👈 list_word = [len(word) for word in words] return {word: list_word.count(word) for word in list_word}
Solución… Esto es muy divertido 😄 .
def count_words_by_length(words): return { len(word): len([ item for item in words if len(item) == len(word) ]) for word in words }
solución:
def count_words_by_length(words): return {len(propword): len([countfruit for countfruit in words if len(propword) == len(countfruit) ]) for propword in words } if __name__ == '__main__': list01 = [ "apple", "banana", "orange", "grapefruit", "pear", "kiwi" ] response = count_words_by_length(list01) print(response) list02 = [ "apple", "banana", "orange" ] response = count_words_by_length(list02) print(response)
hola esta fue mi solución:
. . . . . . . . . . . def count_words_by_length(words): result= {len(palabra):len(list(filter(lambda vol:vol if len(palabra) == len(vol) else 0,words))) for palabra in words} return result
Les comparto mi solución empleando list comprehension y dictionary comprehension
def count_words_by_length(words): # Tu código aquí 👈 words_by_length = { len(word): [ len(word) for word in words ].count(len(word)) for word in words } return words_by_length pass
Mi solución:
def count_words_by_length(words): # Tu código aquí 👈 counter = [len(word) for word in words] return {num: counter.count(num) for num in counter }
def count_words_by_length(words): # Tu código aquí 👈 l1 = ([len(word) for word in words]) return {fruta : l1.count(fruta) for fruta in l1} pass ```def count\_words\_by\_length(words): # Tu código aquí 👈 l1 = (\[len(word) for word in words]) return {fruta : l1.count(fruta) for fruta in l1} pass
otra forma, esta si me funciono en el play ground
def count_words_by_length(words): return { len(word) : [len(word) for word in words].count(len(word)) for word in words}
print(count_words_by_length(words))
Hola, I just want to share how I coded it
tratando de adaptarme a los tiempos modernos:
def count_words_by_length(words): return {longitud: sum(1 for palabra in words if len(palabra) == longitud) for longitud in set(map(len, words))}
def count_words_by_length(words): return { len(word) : sum(1 for w in words if len(w) == len(word)) for word in words }
def count_words_by_length(words): out = {} for word in words: if out.get(len(word)): out[len(word)] += 1 else: out[len(word)] = 1 print(out) return out
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
def count_words_by_length(words): return { len(w): sum( 1 for i in words if len(i) == len(w)) for w in words }
def count_words_by_length(words): # Tu código aquí 👈 result = {} for word in words: letters = 0 for letter in word: letters += 1 if letters in result: result[letters] += 1 else: result[letters] = 1 return result words = count_words_by_length(["apple", "ana", "rio"]) print(words)
Mi código:
def count_words_by_length(words): count_words = [len(word) for word in words] return {word: count_words.count(word) for word in count_words}
Sin diccionario comprehension:
def count_words_by_length(words): """Function to count words by length""" # Declaro a Dict words_by_length = {} if not words: return words_by_length # Itero the words list for create the dict with dict comprehension for word in words: # Calculate len of the word len_word = len(word) if len_word not in words_by_length: words_by_length[len_word] = 1 else: words_by_length[len_word] += 1 return words_by_length # Dictionary comprehension # return [words_by_length[len(word)] = 1 if len(word) not in words_by_length else words_by_length[len(word)]+=1 for word in words] if __name__ == '__main__': words = ["apple", "banana", "orange", "grapefruit", "pear", "kiwi"] words2 = [] response = count_words_by_length(words2) print(response)