首页 > 试题广场 >

3[问答] 有一段英文文本,其中有单词连续重复了2次,编写程

[问答题]
有一段英文文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。例如文本内容为“This is is a desk.”,程序输出为“This is a desk.”
def handle(s):
    t = s.split()
    ret = []
    for k,v in enumerate(t):
        if k<len(t)-1 and t[k+1] != v:
                ret.append(v)
    ret.append(t[-1])
    return " ".join(ret)

发表于 2020-06-04 00:07:41 回复(1)
  a = input().split()
  b = list()
  b.append(a[0])
  for name in a:
      for i in range(1, len(a)):
          if name != b[-1]:
              b.append(name)
  print(" ".join(b))

发表于 2019-03-22 13:32:58 回复(0)
a = input().split()
b = list() for i in a:  if i not in b:
        b.append(i) print(" ".join(b))

发表于 2019-03-04 15:54:54 回复(3)
字符串spilt切割,用集合去重,再成列表在用join拼接
发表于 2021-07-22 07:34:18 回复(0)
m = input('请输入')
a = m.split()
print(a)
b = []
for i in a:
    if i not in b:
        b.append(i)
s = ' '.join(b)
print(s)

发表于 2021-06-16 19:26:08 回复(0)
a = input().split()
b = []
for i in a:
    if i not in b:
        b.append(i)
s = ' '.join(b)
print(s)

发表于 2020-07-16 19:11:22 回复(0)
发表于 2020-05-14 15:01:11 回复(0)
str_list=input().spilt(' ')
for i in range(len(str_list)): #这里的长度评论里都是len-2,why?
    if str_list[i]==str_list[i+1]:
        del str_list[i+1]
print(' '.join(str_list))
#评论里还有另一种方法,看不懂?

发表于 2020-02-17 14:25:40 回复(1)
error_str = "This is is a desk"
correct_str = []
for i in error_str.split():
    if i not in correct_str:
        correct_str.append(i)
print(" ".join(correct_str))

发表于 2019-11-04 15:40:05 回复(0)
para = 'This is is a a desk desk.'.split(' ')
print(para)
d = []
for word in para:
    if word not in d:
        d.append(word)


print(' '.join(d))

D:\Anaconda3\python.exe D:/pytest/moduler/DPC/test.py
['This', 'is', 'is', 'a', 'a', 'desk', 'desk.']
This is a desk desk.
这种方法的 有标点符号的 都无法正确分类
发表于 2019-09-11 10:36:13 回复(0)
 str = "This is is a desk"
>>> str = str.split(" ")
>>> for i in range(0,len(str)-2):
...     if str[i] == str[i+1]:
...         del str[i]
发表于 2019-08-26 20:24:58 回复(0)
string = 'This is is a desk' a = string.split(' ')
list = [] for i in a: if i not in list:
        list.append(i) else: pass print(' '.join(list))
发表于 2019-08-15 12:14:09 回复(0)
words = "This is is a desk"
new_word = " ".join(list({word:words.split(" ").index(word) for word in words.split(" ")}.keys()))
print(new_word)

发表于 2019-08-08 14:30:14 回复(0)
content='This is is a desk'
s=content.split(' ')
l=[] 
for i in s:
   if i in l:
      continue   
  l.append(i) 
print(' '.join(l))


发表于 2019-07-08 10:17:49 回复(0)
假设 a为该字符串
#第一种 集合去重
result = str(set(a))
#第二种 字典健值去重
d={}
a={}.fromkeys(a,vlaue=None)# 此处的value可以省略
#a={}.fromkeys(a)
# 第三种方法 lambda 或者 列表推导式
l = []
[ l.append(i) for i in a if  i not in a else a.remove(i)]
#第四种
用pop 方法      略
#第五种
生成器方法     略
#第六种
字典推导式
#如果保障顺序不变的话可以在每种方法中之前添加一个排序。就是排序后操作



发表于 2019-07-04 02:00:30 回复(0)
str='This is is a desk' 
a=str.split(' ')
l=[] for i in a: if i in l:
  continue    l.append(i) print(' '.join(l))

发表于 2019-05-21 07:29:08 回复(0)
l = s.input().split()
new = list(set(l)).join(" ")
print(new)

发表于 2019-05-17 13:59:04 回复(0)
import re
a=input('请输入一段英文:')
p=re.compile(r'(\b\w+)\s+(\1)')
t=p.search(a)
s=a[:t.span(1)[0]]+a[t.span(1)[1]+1:]

发表于 2019-05-12 15:04:45 回复(0)
text = 'This is is a desk'
list = text.split(' ')
text1=[]
for i in list:
    if i not in text1:
        text1.append(i)
print(' '.join(text1))
发表于 2019-04-07 17:54:56 回复(0)
a=arr.split() res = [] for i in range(len(arr)): if i > 0: if arr[i]!=res[-1]: res.append(arr[i]) else:continue else: res.append(arr[i]) return "".join(res)
发表于 2019-03-20 15:46:52 回复(0)