检查列表中相邻的字符串是否具有相同的第一个字母



你能帮我检查列表中相邻的字符串是否有相同的第一个字母吗。我对python还很陌生,我的方法是首先标记并使列表小写。然后我创建了一个嵌套列表:

import nltk
myStrings = "Bob build a house" 
myStrings_words = nltk.word_tokenize(myStrings)
myStings_words_lower = [word.lower() for word in myStrings_words]
nested_list = [list(x) for x in myStings_words_lower]

现在我不知道如何将每个单词(1个字母(相互比较,并确保它们在列表中相邻。也许是for循环,并通过myString_words_lower[x][1]访问1个字母?

输出应该是开头有相同字母的单词,因此在本例中为bob和build。

提前感谢您,Paul

您可以使用itertools.groupby来帮助您。假设你有一个小写单词列表:

import nltk
myStrings = "Bob build a house" 
myStrings_words = nltk.word_tokenize(myStrings)
myStings_words_lower = [word.lower() for word in myStrings_words]

要将它们分组为共享第一个字母的任何邻居,您可以执行以下操作:

import itertools
# define a grouping helper
first_letter = lambda x: x[0]
# get the groups
grouped_words = itertools.groupby(myStings_words_lower, key=first_letter)
print(f"The number of words is {len(myStings_words_lower)} and the number of groups is {len(list(grouped_words))}")

如果组的数量等于单词的数量,则没有连续的单词共享起始字母。如果数字不相等,那么您知道有相邻的条目共享一个起始字母。

另一种方法:

In [6]: myString = "Bob build an aeroplane, boat and a haunted house"                                                                                                                                                                                                                                                                                                                                                                 
In [7]: my_words = [word.lower() for word in myString.split()]                                                                                                                                                                                                                                                                                                                                                                        
In [8]: my_words                                                                                                                                                                                                   
Out[8]: ['bob', 'build', 'an', 'aeroplane,', 'boat', 'and', 'a', 'haunted', 'house']
# Iterate over the words and while iterating, check if present word and 
# the next word has the same first letter. (We use len(my_words) - 1 as
# we are using i+1 in the loop and so should stop at the penultimate word)                                                                                                                                                                                                                                                                                                                                               
In [9]: for i in range(len(my_words) - 1):                                                                                                                                                                            
...:     if my_words[i][0] == my_words[i+1][0]:                                                                                                                                                                    
...:         print(my_words[i], my_words[i+1])                                                                                                                                                                     
...:                                                                                                                                                                                                            
bob build                                                                                                                                                                                                          
an aeroplane,                                                                                                                                                                                                      
and a                                                                                                                                                                                                              
haunted house  

干杯!

相关内容

  • 没有找到相关文章

最新更新