【问题标题】:Check if any strings in a list that are next to each other have the same first letter检查列表中相邻的任何字符串是否具有相同的首字母
【发布时间】:2021-12-30 06:31:36
【问题描述】:

您能否帮我检查一下列表中相邻的字符串是否具有相同的首字母。我对 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。

提前谢谢你, 保罗

【问题讨论】:

  • 这可能会有所帮助:zip(myStings_words_lower[:-1],myStings_words_lower[1:]).

标签: python list function compare nltk


【解决方案1】:

您可以使用 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))}")

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

【讨论】:

    【解决方案2】:

    另一种方法:

    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  
    

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多