Open In App

Python – Sequence Assignment to Words

Given a String of words, assign an index to each word.

Input : test_str = ‘geeksforgeeks is best’ 
Output : {0: ‘geeksforgeeks’, 1: ‘is’, 2: ‘best’} 
Explanation : Index assigned to each word. 



Input : test_str = ‘geeksforgeeks best’ 
Output : {0: ‘geeksforgeeks’, 1: ‘best’} 
Explanation : Index assigned to each word.

Method #1: Using enumerate() + dict() + split()



In this, we first perform task of split() and then add index component to map each word with index using enumerate().




# Python3 code to demonstrate working of
# Sequence Assignment to Words
# Using split() + enumerate() + dict()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using dict() to convert result in idx:word manner
res = dict(enumerate(test_str.split()))
     
# printing result
print("The Assigned Sequence : " + str(res))

Output
The original string is : geekforgeeks is best for geeks
The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

Method #2: Using zip() + count() + dict()

In this, the count() component renders the index logic and pairing of each word to index is done using zip().




# Python3 code to demonstrate working of
# Sequence Assignment to Words
# Using zip() + count() + dict()
from itertools import count
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using dict() to convert result in idx:word manner
# count() from itertools used for this task
res = dict(zip(count(), test_str.split()))
     
# printing result
print("The Assigned Sequence : " + str(res))

Output
The original string is : geekforgeeks is best for geeks
The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

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

Approach #3: Dictionary comprehension

Split the input string into words using the split() function. Create a dictionary comprehension to assign a sequence number to each word using the word indices as keys and the words as values.

Step-by-step approach:




test_str = 'geeksforgeeks is best'
words = test_str.split()
result = {i: words[i] for i in range(len(words))}
print(result)

Output
{0: 'geeksforgeeks', 1: 'is', 2: 'best'}

Time Complexity: O(n), where n is the number of words in the input string. The split() function has a time complexity of O(n), and the dictionary comprehension iterates over the indices of the words list once.
Space Complexity: O(n), where n is the number of words in the input string. The space complexity is dominated by the resulting dictionary, which has n key-value pairs.

Approach #4: Using list comprehension:

Step-by-step approach:




# Python3 code to demonstrate working of
# Sequence Assignment to Words
# Using list comprehension
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using list comprehension to convert result in idx:word manner
res = {idx: word for idx, word in enumerate(test_str.split())}
 
# printing result
print("The Assigned Sequence : " + str(res))
#This code is contributed by Rayudu.

Output
The original string is : geeksforgeeks is best for geeks
The Assigned Sequence : {0: 'geeksforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

Time Complexity:
The time complexity of this algorithm is O(n), where n is the number of words in the input string. This is because the split() method takes O(n) time to split the string into words, and the zip() function takes O(n) time to create tuples for each word. The dict() function takes O(n) time to create the dictionary.

Auxiliary Space:
The space complexity of this algorithm is O(n), where n is the number of words in the input string. This is because the split() method creates a new list of words with length n, and the resulting dictionary also has n key-value pairs. The iterator created using the count() function takes up a constant amount of space.

Method #5: Use a for loop and a dictionary to store the index and corresponding word.

Step-by-step approach:




# initializing string
test_str = 'geeksforgeeks is best for geeks'
print("The original string is : " + str(test_str))
 
# using for loop and dictionary to assign sequence
res_dict = {}
word_list = test_str.split()
for idx, word in enumerate(word_list):
    res_dict[idx] = word
 
# printing result
print("The Assigned Sequence : " + str(res_dict))

Output
The original string is : geeksforgeeks is best for geeks
The Assigned Sequence : {0: 'geeksforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

Time complexity: O(n) where n is the number of words in the input string.
Auxiliary space: O(n) to store the dictionary of index and word pairs.


Article Tags :