Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python program to find the character position of Kth word from a list of strings
When it is required to find the character position of Kth word from a list of strings, a list comprehension along with enumerate() is used to get the position of each character when all strings are concatenated.
Example
Below is a demonstration of the same ?
my_list = ["python", "is", "fun", "to", "learn"]
print("The list is :")
print(my_list)
K = 15
print("The value of K is :")
print(K)
my_result = [element[0] for sub in enumerate(my_list) for element in enumerate(sub[1])]
my_result = my_result[K]
print("The result is :")
print(my_result)
Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The value of K is : 15 The result is : 2
How It Works
Let's break down the list comprehension step by step to understand how it finds character positions:
my_list = ["python", "is", "fun", "to", "learn"]
# Step 1: enumerate(my_list) gives word positions and words
print("Words with their positions:")
for word_pos, word in enumerate(my_list):
print(f"Position {word_pos}: '{word}'")
print("\nCharacter positions when all strings are concatenated:")
# Step 2: For each word, enumerate characters to get their positions
char_positions = []
for word_pos, word in enumerate(my_list):
for char_pos, char in enumerate(word):
char_positions.append((word_pos, char_pos, char))
print(f"Word {word_pos}, Char {char_pos}: '{char}'")
# The list comprehension extracts word positions for each character
result_positions = [word_pos for word_pos, word in enumerate(my_list) for char_pos, char in enumerate(word)]
print(f"\nWord positions for each character: {result_positions}")
print(f"15th character belongs to word at position: {result_positions[15]}")
Words with their positions: Position 0: 'python' Position 1: 'is' Position 2: 'fun' Position 3: 'to' Position 4: 'learn' Character positions when all strings are concatenated: Word 0, Char 0: 'p' Word 0, Char 1: 'y' Word 0, Char 2: 't' Word 0, Char 3: 'h' Word 0, Char 4: 'o' Word 0, Char 5: 'n' Word 1, Char 0: 'i' Word 1, Char 1: 's' Word 2, Char 0: 'f' Word 2, Char 1: 'u' Word 2, Char 2: 'n' Word 3, Char 0: 't' Word 3, Char 1: 'o' Word 4, Char 0: 'l' Word 4, Char 1: 'e' Word 4, Char 2: 'a' Word 4, Char 3: 'r' Word 4, Char 4: 'n' Word positions for each character: [0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4] 15th character belongs to word at position: 4
Alternative Approach
Here's a more readable way to achieve the same result:
def find_word_position(string_list, k):
"""Find which word the Kth character belongs to when strings are concatenated."""
char_count = 0
for word_index, word in enumerate(string_list):
char_count += len(word)
if k < char_count:
return word_index
return -1 # K is beyond the total length
my_list = ["python", "is", "fun", "to", "learn"]
K = 15
result = find_word_position(my_list, K)
print(f"The {K}th character belongs to word at position: {result}")
print(f"The word is: '{my_list[result]}'")
The 15th character belongs to word at position: 4 The word is: 'learn'
Conclusion
The list comprehension approach uses nested enumerate() to map each character position to its corresponding word index. This technique is useful when you need to find which word a specific character belongs to in a concatenated string sequence.
Advertisements
