Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython – Find Words with both alphabets and numbers

Python – Find Words with both alphabets and numbers

Sometimes, while working with Python strings, we can have problem in which we need to extract certain words with contain both numbers and alphabets. This kind of problem can occur in many domains like school programming and web-development. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using any() + isdigit() + isalpha() The combination of above functionalities can be used to perform this task. In this, we iterate for all the words and check for required combination using isdigit() and isalpha(). 

Python3




# Python3 code to demonstrate working of
# Words with both alphabets and numbers
# Using isdigit() + isalpha() + any()
 
# initializing string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# printing original string
print("The original string is : " + test_str)
 
# Words with both alphabets and numbers
# Using isdigit() + isalpha() + any()
res = []
temp = test_str.split()
for idx in temp:
    if any(chr.isalpha() for chr in idx) and any(chr.isdigit() for chr in idx):
        res.append(idx)
         
# printing result
print("Words with alphabets and numbers : " + str(res))


Output

The original string is : Lazyroarfor23Lazyroar is best45 for gee34ks and cs
Words with alphabets and numbers : ['Lazyroarfor23Lazyroar', 'best45', 'gee34ks']

Method #2 : Using regex This is yet another way by which we can perform this task. In this, we feed the string to findall(), and extract the required result. Returns strings till the numbers only. 

Python3




# Python3 code to demonstrate working of
# Words with both alphabets and numbers
# Using regex
import re
 
# initializing string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# printing original string
print("The original string is : " + test_str)
 
# Words with both alphabets and numbers
# Using regex
res = re.findall(r'(?:\d+[a-zA-Z]+|[a-zA-Z]+\d+)', test_str)
         
# printing result
print("Words with alphabets and numbers : " + str(res))


Output

The original string is : Lazyroarfor23Lazyroar is best45 for gee34ks and cs
Words with alphabets and numbers : ['Lazyroarfor23', 'best45', 'gee34']

Time Complexity: O(n)
Space Complexity: O(n)

Method #3: Using operator.countOf() method.

Python3




# Python3 code to demonstrate working of
# Words with both alphabets and numbers
import operator as op
# define all digits
all_digits = ['0', '1', '2', '3', '4',
              '5', '6', '7', '8', '9']
 
# define all letters
all_letters = ['a', 'b', 'c', 'd', 'e', 'f',
               'g', 'h', 'i', 'j', 'k', 'l',
               'm', 'n', 'o', 'p', 'q', 'r',
               's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
 
# initializing string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# printing original string
print("The original string is : " + test_str)
 
# Words with both alphabets and numbers
# Using isdigit() + isalpha() + any()
res = []
temp = test_str.split()
for idx in temp:
    if any(op.countOf(all_letters, chr) > 0 for chr in idx) and any(op.countOf(all_digits, chr) > 0 for chr in idx):
        res.append(idx)
 
# printing result
print("Words with alphabets and numbers : " + str(res))


Output

The original string is : Lazyroarfor23Lazyroar is best45 for gee34ks and cs
Words with alphabets and numbers : ['Lazyroarfor23Lazyroar', 'best45', 'gee34ks']

Time Complexity: O(n)
Space Complexity: O(n)

Method #4: Using NumPy, you can perform this task by splitting the string into words, checking if both letters and numbers exist in each word, and then appending the words that contain both letters and numbers to a result list. Here’s an example:

Python3




import numpy as np
 
# initialize the string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# split the string into words
words = np.array(test_str.split())
 
# create a boolean array indicating if a word contains letters and numbers
has_letters_and_numbers = np.array([any(c.isdigit() for c in word) and any(c.isalpha() for c in word) for word in words])
 
# extract words that contain both letters and numbers
result = words[has_letters_and_numbers]
 
print("Words with alphabets and numbers:", result)
#This code is contributed by Edula Vinay Kumar Reddy


Output:

Words with alphabets and numbers: ['Lazyroarfor23Lazyroar' 'best45' 'gee34ks']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using a for loop and isalnum() method

  • Initialize an empty list ‘res’ to store the words with both alphabets and numbers.
  • Split the string ‘test_str’ into a list of words and assign it to ‘temp’.
  • Iterate through each word in ‘temp’ using a for loop.
  • Check if the word has at least one alphabet and one digit using the isalnum() method.
  • If the word satisfies the condition, append it to the ‘res’ list.
  • Finally, print the ‘res’ list.

Python3




# Python3 code to demonstrate working of
# Words with both alphabets and numbers
 
# initializing string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# printing original string
print("The original string is : " + test_str)
 
# Words with both alphabets and numbers
# Using a for loop and isalnum() method
res = []
temp = test_str.split()
for word in temp:
    if any(c.isalpha() for c in word) and any(c.isdigit() for c in word):
        res.append(word)
 
# printing result
print("Words with alphabets and numbers : " + str(res))


Output

The original string is : Lazyroarfor23Lazyroar is best45 for gee34ks and cs
Words with alphabets and numbers : ['Lazyroarfor23Lazyroar', 'best45', 'gee34ks']

Time Complexity: O(n*k), where n is the number of words in the input string and k is the maximum length of a word in the input string.
Auxiliary Space: O(m), where m is the number of words with both alphabets and numbers in the input string.

Method 6: Using filter() function and lambda function

  • Use the filter() function to iterate over each word in the string.
  • Create a lambda function that checks if the word contains both alphabets and numbers using the isalnum() method.
  • Return the filtered list of words that have both alphabets and numbers.
  • Calculate the time complexity and auxiliary space complexity of this method.

Python3




# Python3 code to demonstrate working of
# Words with both alphabets and numbers
# Using filter() function and lambda function
 
# initializing string
test_str = 'Lazyroarfor23Lazyroar is best45 for gee34ks and cs'
 
# printing original string
print("The original string is : " + test_str)
 
# Words with both alphabets and numbers
# Using filter() function and lambda function
res = list(filter(lambda word: any(char.isdigit() for char in word) and any(char.isalpha() for char in word), test_str.split()))
 
# printing result
print("Words with alphabets and numbers : " + str(res))


Output

The original string is : Lazyroarfor23Lazyroar is best45 for gee34ks and cs
Words with alphabets and numbers : ['Lazyroarfor23Lazyroar', 'best45', 'gee34ks']

Time complexity: O(n), where n is the number of words in the string, because we iterate over each word once.
Auxiliary space: O(k), where k is the number of words in the string that have both alphabets and numbers, because we create a list to store the filtered words.

RELATED ARTICLES

Most Popular

Recent Comments