Open In App

Python – Sequence Assignment to Words

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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:

  • Split the input string into words using the split() function and store the resulting list in a variable called words.
  • Create a new dictionary using a dictionary comprehension.
  • Iterate over the indices of the words list using the range() function
  • For each index i, add a new key-value pair to the dictionary where the key is i and the value is the word at index i.
  • Print the resulting dictionary.

Python3




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:

  • Initialize the input string test_str.
  • Split the string into a list of words using the split() method.
  • Initialize an iterator using the count() function from the itertools module.
  • Use the zip() function to combine the iterator with the list of words and create a tuple for each word with its corresponding index.
  • Use the dict() function to convert the tuples into a dictionary with the index as the key and the word as the value.
  • Print the resulting dictionary.

Python3




# 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:

  • Initialize an empty dictionary res_dict to store the index and corresponding word pairs.
  • Split the input string test_str into a list of words using the split() function and assign it to word_list.
  • Loop through the word_list using a for loop and enumerate() function to get the index and corresponding word at each iteration.
  • Add each index and word pair to the res_dict dictionary using the index as the key and the word as the value.
  • Print the res_dict dictionary to show the assigned sequence.

Python3




# 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.



Last Updated : 16 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads