找出一个短句中字符最多的单词
sentence = 'Today is a perfect day!' # 输入英文短句如 Today is a perfect day!words = sentence.split() # 按照空格分裂
list =[]
maxlength = 0
for word in words:
if word.isalpha(): #如果word 全为字母的话
if len(word) >= maxlength:
list =
maxlength = len
list.append(word)
else:
len(word[:-1]) == maxlength:# 排查文末有标点的特殊情况,去掉最后一个字符
list.append(word)
print(list)
需求: 从一个英文短句中找出字符最多的那个单词 (可能不只一个)
上面的我的代码,但是显示else 下面的一句报错,请假下大神是什么原因啊,或者说我这个思路是否正确呢,感谢指点!
这是一个逻辑表达式
len(word[:-1]) == maxlength:
应该用 if 打头
if len(word[:-1]) == maxlength: jackz007 发表于 2021-7-18 16:18
这是一个逻辑表达式
应该用 if 打头
啊 这么低级的错误,感谢大大神sentence = 'today is a perfect day!' # 输入英文短句如 Today is a perfect day!
words = sentence.split() # 按照空格分裂
list =[]
另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?
maxlength = 0
for word in words:
if word.isalpha(): #如果word 全为字母的话
if len(word) >= maxlength:
maxlength = len(word)
list.append(word)
else:
if len(word[:-1]) == maxlength:# 排查文末有标点的特殊情况,去掉最后一个字符
list.append(word)
print(list)
jackz007 发表于 2021-7-18 16:18
这是一个逻辑表达式
应该用 if 打头
我改了下,然后结果把taday 和 perfect 都打印出来了,怎样改正我这个if else 的逻辑啊
sentence = 'today is a perfect day!' # 输入英文短句如 Today is a perfect day!
words = sentence.split() # 按照空格分裂
list =[]
另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?
maxlength = 0
for word in words:
if word.isalpha(): #如果word 全为字母的话
if len(word) >= maxlength:
maxlength = len(word)
list.append(word)
else:
if len(word[:-1]) == maxlength:# 排查文末有标点的特殊情况,去掉最后一个字符
list.append(word)
print(list)
sentence = 'Today is a perfect day!' # 输入英文短句如 Today is a perfect day!
words = sentence.split() # 按照空格分裂
maxlength = 0
xw = ''
for word in words:
if not word . isalpha():
word = word[:-1]
if len(word) > maxlength:
maxlength = len(word)
xw = word
print(xw) weiyideid823 发表于 2021-7-18 16:32
我改了下,然后结果把taday 和 perfect 都打印出来了,怎样改正我这个if else 的逻辑啊
当word比maxlength大的时候这个word就被放到list里,所以你打印list会打印两个,可以把最后的打印改为只打印list的最后一项 sentence = 'today is a perfect day!' # 输入英文短句如 Today is a perfect day!
words = sentence.split() # 按照空格分裂
list =[]
另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?
maxlength = 0
for word in words:
if word.isalpha(): #如果word 全为字母的话
if len(word) >= maxlength:
maxlength = len(word)
list.append(word)
elif len(word[:-1]) == maxlength:# 排查文末有标点的特殊情况,去掉最后一个字符
list.append(word)
print(list)
可以elif jackz007 发表于 2021-7-18 16:34
这个如果遇到最长的单词有多个的时候好像不行? 青出于蓝 发表于 2021-7-18 16:42
这个运行出来好像也是同时打印的today 和 perfect啊 jackz007 发表于 2021-7-18 16:34
Today is a perfect dayyyyy!
如果句子是这个,好像不能同时把perfect 和dayyyyy 打印出来啊
另外 XW = ' '是啥意思求教。。。 weiyideid823 发表于 2021-7-18 16:59
Today is a perfect dayyyyy!
如果句子是这个,好像不能同时把perfect 和dayyyyy 打印出来啊
sentence = 'Today is a perfect day!' # 输入英文短句如 Today is a perfect day!
words = sentence.split() # 按照空格分裂
maxlength = 0
for word in words: # 确定最长单词的长度值
if not word . isalpha():
word = word[:-1]
if len(word) > maxlength:
maxlength = len(word)
for word in words: # 把符合最大长度的所有单词打印出来
if not word . isalpha():
word = word[:-1]
if len(word) == maxlength:
print(word) sentence = 'Today is a perfect dayyyyy!' # 输入英文短句如 Today is a perfect day!
words = sentence.split()
dic = {}
maxlength = 0
for word in words:
if not word.isalpha():
word = word[:-1]
length = len(word)
if length > maxlength:
maxlength = length
dic = length
for k,v in dic.items():
if v ==maxlength:
print(k)
本帖最后由 R0n9 于 2021-7-18 17:50 编辑
可否这样?
from collections import Counter
sentence = 'Today is a perfect day!'
words = sentence.split()
b = dict(Counter(words))
c={key:value for key,value in b.items() if value > 1}
if c:
print(max(zip(c.values(), c.keys())))
else:
print('都是一个')
页:
[1]