试图完成我的一个单元,我被其中一个练习困住了,那就是从我必须收到的文本中数出每个单词的字母,直到所有字母的总和为>= 1000,但即使文本超过1000个,它也永远不会达到1000个。
首先,我认为这可能是因为我没有收到完整的信息,但在查看并处理了我创建的不同功能之后(它们都没有解决问题),我的计数器功能似乎不起作用。(此计数器函数返回一个数字数组,稍后我将其转换为字符串)
有什么东西我不明白,就是把我的代码搞砸了?
def letterCounter(text):
counter = 0
indice = 0
lettTotal = 0
totalCount = []
while True:
if text[indice] == " ":
totalCount.append(counter)
counter = 0
indice += 1
else:
counter += 1
lettTotal += 1
indice += 1
if lettTotal >= 1000:
break
return totalCount
编辑:-Sample输入:
权威设计及讲座教授、职工主管、未来因素、警察、春活、找具体、易、续、区、军、事、行、床、政府等都可以积极反映产品计划,检查产品计划,检查油漆杆,一般重量,对政策控制系列,煤气蓝行程,确定结构评分,以100名教师中的任何一个人为起点,市民,学校,答案股票,以开始号,真枪,真实比率,发低形式下的通知,躲避任务卡,深建议成人游戏秩序,可重警察文化,第三,这样,他就用提前挑选报告摄像头,他就是你的。当然,选择妻子一般百现在的土地,从没有朋友从有边接受,从没有边沿接受,作为名单,国民降费,报纸活动,新没有手臂,个人陈述,基础建设,个体字,主要留吃,看丢权差,最后看宝宝公司,所以来源对待电脑公司,所以在没有消息的情况下,她只是希望没有消息,低档家长画面清晰之后,才能在真实的办公室里看到长线,所以不能看到长细胞,所以现在她5月末的兴趣名单上的数字,最小的风险,需要网络提到地板摄像头,他自己如何通过实践分析进入区域内的女孩行为。在中心原因下,全员情况下的详细知识、成功的制度任务和攻击,证明十名参与者身体上的女性宗教,她应该朋友蓝,尽管投深项目,最后问几个国家候选人,当官东月运营,十条街道损失类,准备攻击经验机构对课程建设的影响,最终发现到掌握别人的故事,贴近市民的主要六棵树,有时选择应用数据、学生试验、讨论生产地丈夫的质量、眼睛,在美丽的失去、事实色彩之间,这样的高级管理事实色彩。
输出:
9、5、6、5、5、9、6、9、6、6、6、4、4、8、4、4、3、8、5、4、8、3、6、7、3、3、10、3、8、7、7、5、8、4、3、7、6、8、2、6、7、6、4、3、4、4、4、4、4、4、5、9、9、5、7、6、8、7、5、7、7、3、6、7、3、6、4、5、7、6、6、5、2、5、6、3、4、7、4、6、5、4、3、4、4、5、4、3、4、4、6、4、7、4、4、6、4、4、5、5、4、4、5、7、6、4、5、5、4、3、4、4、5、4、6、6、4、6、4、7、7、7、4、4、5、6、4、4、5、6、4、7、4、6、2、4、5、4、4、9、8、7、8、3、3、2、3、10、9、4、5、3、6、4、3、3、5、9、10、4、3、5、6、4、4、7、2、6、5、8、5、3、3、6、4、4、3、2、4、7、7、4、4、9、5、3、3、5、6、7、7、7、7、7、5、4、3、3、5、3、3、5、6、7、7、7、7、5、3、4、4、3、3、5、7、7、7、7、3、4、4、3、3、3、5、6、7、7、7、7、7、4、3、4、5、3、3、5、6、7、7、7、7、5、5、3、4、4、3、3、9、4、4、3、4、4、3、5、9、4、4、3、
求和= 999,不应达到1000
发布于 2022-05-10 15:00:36
如果您使用的文本参数的单词由空格分隔,您可以如下所示
def countLetters(sentence):#split your sentence to get the individual words
words=sentence.split(" ")
store=[]#declare an array to hold the length of each word
summation=0 #variable to hold the total number of letters in the text
for word in words:
store.append(len(word))
summation+=len(word) #pile up the total number of letters
if(summation>=1000):
break #break out of the word iteration loop
for i in range(len(words)):#print the words alongside the length of each
print(words[i],store[i])
发布于 2022-05-10 16:26:01
这里的其他解决方案可能会解决你的家庭作业,但让我们来看看为什么它一开始就失败了。
注意,在循环的第一部分中,您要键入空格字符以附加当前计数。然而,在计算最后一个单词的时候,你总是会达到你的字母总数的极限。一旦您从循环中break
,您就不能添加最后一个单词(被计数的那个)。
如果在将单词添加到计数后,重新排列函数以检查字母总数,则不会丢失最后一个单词。
while True:
if text[index] == " ":
total_counts.append(counter)
counter = 0
index+= 1
if total_letters >= 1000:
break
else:
counter += 1
total_letters += 1
index += 1
然后,单词计数将在中断循环之前追加。
发布于 2022-05-10 15:46:21
您可以使用list comprehension
来实现您的目标:
[len(w) for w in s.split()]
得到它的sum()
sum([len(w) for w in s.split()])
示例
s = 'authority stuff Design reach speak professor worker Executive future factor police spring live seek specific Easy What lot continue chair area military may matter Mention act bed Government all positive reflect Product plan Check Painting drop bar general weight anything to policy Control series over gas blue trip sure form care commercial hand term offer determine structure score Produce Person whatever someone among hundred teacher act garden hundred red appear With stock Citizen school answer stock as start number arm real himself Rate notice Under hair low Form avoid task get baby card anyone deep suggest adult Game order road able heavy Police culture reduce teacher third such he She Just your with ahead pick report camera sure choose wife general Hundred present land will Guess within skin Line never friend from Contain edge accept as list Civil drop cost newspaper anything section activity New no arm individual Statement base Build per figure main stay Eat See throw Authority difference last see night across baby firm Company So source treat computer plant how across news just her me hope without include news seem difficult after low yet style parent picture clear subject during true office owner but turn can war never see long cell so list current top her end May interest course list figure start least risk need network mention Floor camera inside he Himself how under Girl Behavior Through Practice analysis enter region very next Behavior under central cause hand Whole situation detail knowledge Successful institution task lay attack Prove ten participant Physical woman religious Her should friend Blue though throw claim deep item Ask who Several finally national candidate serve officer East month operation peace Ten street loss kind prepare attack against experience Institution toward course Dinner build impact final Rise find arrive hold Understand Others story close citizen main six region tree sometimes choice apply data student trial get food discuss production country husband quality finally eye in beautiful lose lay Sister whom thus between still Top management Fact color such'
sum([len(w) for w in s.split()])
输出
1794
https://stackoverflow.com/questions/72188447
复制相似问题