Dada una string de palabras, extraiga el recuento de palabras con letras específicas.
Entrada : test_str = ‘geeksforgeeks es lo mejor para los geeks’, letra = “g” Salida : 2 Explicación : “g” aparece en 2 palabras. Entrada : test_str = ‘geeksforgeeks es lo mejor para los geeks’, letra = “s” Salida : s Explicación : “s” ocurre en 4 palabras.
Método n.º 1: usar la comprensión de listas + len() + split()
Esta es una de las formas en que se puede realizar esta tarea. En esto, realizamos la tarea de extraer palabras de una string usando split() y loop se usa para iterar palabras para verificar la existencia de letras y len() se usa para obtener el número de palabras con letra.
Python3
# Python3 code to demonstrate working of # Count of Words with specific letter # Using list comprehension + len() + split() # initializing string test_str = 'geeksforgeeks is best for geeks' # printing original string print("The original string is : " + str(test_str)) # initializing letter letter = "e" # extracting desired count using len() # list comprehension is used as shorthand res = len([ele for ele in test_str.split() if letter in ele]) # printing result print("Words count : " + str(res))
The original string is : geeksforgeeks is best for geeks Words count : 3
Método #2: Usando filter() + lambda + len() + split()
Esta es otra forma más en la que se puede realizar esta tarea. En este, realizamos la tarea de filtrar usando filter() + lambda.
Python3
# Python3 code to demonstrate working of # Count of Words with specific letter # Using filter() + lambda + len() + split() # initializing string test_str = 'geeksforgeeks is best for geeks' # printing original string print("The original string is : " + str(test_str)) # initializing letter letter = "e" # extracting desired count using len() # filter() used to check for letter existence res = len(list(filter(lambda ele : letter in ele, test_str.split()))) # printing result print("Words count : " + str(res))
The original string is : geeksforgeeks is best for geeks Words count : 3
Método #3: Usar len() + for loop
Usamos len() y for loop para iterar sobre la string para resolver el problema.
Python3
# Python3 code to get count of Words with specific letter # initializing string test_str = 'geeksforgeeks is best for geeks' # printing original string print("The original string is : " + str(test_str)) # initializing letter letter = "e" """ As we have to only count one letter per word we have to check if we have checked that word or not and to do so we use variable word_count that become odd if we founded the letter in the word and even when not""" word_count = 0 # count of words with specific letter word = 0 # For loop to iterate over string for i in range(len(test_str)): if word_count % 2 == 0 and test_str[i] == letter: word += 1 word_count = 1 # set word_count an odd number so we know to not check the current word. elif test_str[i] == " ": """ New word starts with " " """ word_count = 0 # set word_count an even number so we know to check the current word. print("Words count :", word) # This code is contributed by Shivesh Kumar Dwivedi
The original string is : geeksforgeeks is best for geeks Words count : 3
Método #4: Usando split() y find()
Python3
# Python3 code to demonstrate working of # Count of Words with specific letter # Using split() and find() # initializing string test_str = 'geeksforgeeks is best for geeks' # printing original string print("The original string is : " + str(test_str)) # initializing letter letter = "e" # extracting words with specific letter res=0 x=test_str.split() for i in x: if(i.find(letter)!=-1): res+=1 # printing result print("Words count : " + str(res))
The original string is : geeksforgeeks is best for geeks Words count : 3
Publicación traducida automáticamente
Artículo escrito por manjeet_04 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA