我正在编写一个小型NLP算法,需要执行以下操作:
对于列表["this", "this", "and", "that"]
中的每个字符串x,如果字符串x
和下一个字符串相同,我想打印该字符串。
最佳答案
s = ["this", "this", "and", "that"]
for i in xrange(1,len(s)):
if s[i] == s[i-1]:
print s[i]
编辑:
另外,如果您使用的是python 3.X,请使用
range
而不是xrange
关于python - 如何将一个字符串与列表中的下一个字符串进行比较?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6697113/