给定一个字符串
s = "dog cat bat cat dog dog"
找到两次出现的第一个单词。答案:猫
到目前为止,我有以下代码:
def first_word(string):
for word in string.split():
print(word)
first_word("dog cat bat cat dog dog")
答案 0 :(得分:2)
首先,您的功能名称不具有描述性。我会将其更改为first_repeated_word
。
分裂部分很好,但你需要记住你已经看过的单词。将单词添加到set
(因为它支持快速查找)并查看您是否已经看到它会起作用:
def first_repeated_word(string):
processed = set()
for word in string.split():
if word in processed:
# this will immediatly end the function and return the repeated word
return word
# Add the word to the set.
processed.add(word)
# You need to consider the case when no repeated word was found, best
# to throw an exception
raise ValueError('no repeated word found')
功能的实际测试:
>>> first_repeated_word("dog cat bat cat dog dog")
'cat'
我实际上有一个第三方模块可以做到这一点(更有效率):iteration_utilities.duplicates
>>> from iteration_utilities import duplicates
>>> your_string = "dog cat bat cat dog dog"
>>> next(duplicates(your_string.split()))
'cat'
答案 1 :(得分:0)
你只需要你的循环来检查你之前是否看过那个单词。像这样:
def first_repeat(string):
previous = []
for word in string.split():
if word in previous:
print(word)
break
else:
previous.append(word)
说明:对于每个单词,检查您之前是否已经看到它(它是否在previous
中),如果是,则打印该单词并停止循环,如果它< em>不是,将当前单词添加到以前单词列表中。
答案 2 :(得分:0)
>>> found_twice = False
>>> seen = set()
>>> i = 0
>>> s = s.split()
>>> s
['dog', 'cat', 'bat', 'cat', 'dog', 'dog']
>>> while not found_twice:
... if s[i] not in seen:
... seen.add(s[i])
... else:
... print(s[i])
... found_twice = True
... i += 1
...
cat
或者:
>>> seen = set()
>>> for x in s:
... if x not in seen:
... seen.add(x)
... else:
... print(x)
... break
...
...
cat
有很多方法可以迭代一系列元素并跟踪你已经看过的内容。&#34;
答案 3 :(得分:0)
这有效:
def find_first_duplicate(s):
s_list = s.split(' ') # split string everytime there is a space
l = []
for item in s_list:
if item in l:
# We found the first duplicate!
return item
else:
# Otherwise add to list and try again
l.append(item)
return 'No Duplicates'
答案 4 :(得分:0)
这可以解决
def first_word(string): # string is the words you pass
a= string.split() # a is the list of individual words
b=[] # b is an empty list
for item in a:
if item not in b:
b.append(item) # here we add unique words to b
else: # if a word gets repeated for the first time it will print the word
print item
break # since we printed the first repeated word we break out of loop
first_word('cat dog bird cat dog')
解决方案:
cat