如何计算给定单词列表的复数单词数以找到分数
How to count the number of plural words given a list of words in order to find the fraction
我正在努力完成这个家庭作业问题:
问题 #1: "Write a program that asks the user for a list of nouns (separated by spaces) and approximates the fraction that are plural by counting the fraction that end in "s”。你的程序应该输出单词总数和以 "s" 结尾的分数。你应该假设单词由空格分隔(并忽略单词之间的制表符和标点符号的可能性)。
- 首先,统计用户输入的字符串中的单词数(提示:统计空格数)。打印出字数。在进入下一部分之前确保它有效。
- 接下来忽略最后一个词(属于特殊情况,可以单独处理),统计以's'结尾的词的个数(提示:数一数"s ")。在继续下一步之前测试此部分是否有效。
- 最后,检查最后一个单词是否以 "s" 结尾--因为它是最后一个单词,"s" 将始终出现在字符串中的相同索引处。*
问题 #2: 如果我们计算 S 的数量,那将计算单词中的所有 S,而不仅仅是最后一个。我怎样才能弄清楚每个给定单词的最后一个字母是否以 S 结尾。到目前为止我有这个:
noun = input("Enter nouns: ")
print("You entered: ", noun)
words = noun.split()
print(words)
amount = len(words)
print(amount)
我不认为我可以简单地做一个 words.count('s')
。非常感谢任何帮助,谢谢。
将与用户输入相同,使用 .split()
和 str.endswith()
data = 'cat dogs people lovers'
y = data.split()
print(len(y))
x = [i for i in y[:-1] if i.endswith('s')]
print(len(x))
if y[-1].endswith('s'):
print(y[-1])
不使用 .endswith()
y = data.split()
print(len(y))
x = [i for i in y[:-1] if i[-1] == 's']
print(len(x))
if y[-1][-1] == 's':
print(y[-1])
您可以通过简单的列表理解来做到这一点:
test_input = 'apples carrots pickles tractor tree goat friends people'
plurals = [i for i in test_input.split() if i.endswith('s')]
total = len(plurals)
fraction = total/len(test_input.split())
如果您无法使用endswith()
,那么您可以使用索引:
plurals = [i for i in test_input.split() if i[-1]=='s']
请注意,split()
默认情况下会将输入字符串拆分为空格 (' '
)。
按照@Billthelizard 的提示,看起来最简单的解决方案是这种情况:
plurals = noun.count('s ')
谢谢大家! @billthelizard @toti08 @vash_the_stampede 和@rahlf23 你们的回复真的很有帮助。使用您的建议,我终于找到了正确的代码。我将在下面列出可能发生在这个问题上的任何其他人。再次感谢!
noun = input("Enter nouns: ")
print("You entered: ", noun)
words = noun.split()
print(words)
amount = len(words)
print(amount)
plural = noun.count('s ')
for i in noun:
if i[-1] == "s":
last = 1
else:
last = 0
plurals = plural + last
print(plurals)
fraction = plurals / amount
print(fraction)
我正在努力完成这个家庭作业问题:
问题 #1: "Write a program that asks the user for a list of nouns (separated by spaces) and approximates the fraction that are plural by counting the fraction that end in "s”。你的程序应该输出单词总数和以 "s" 结尾的分数。你应该假设单词由空格分隔(并忽略单词之间的制表符和标点符号的可能性)。
- 首先,统计用户输入的字符串中的单词数(提示:统计空格数)。打印出字数。在进入下一部分之前确保它有效。
- 接下来忽略最后一个词(属于特殊情况,可以单独处理),统计以's'结尾的词的个数(提示:数一数"s ")。在继续下一步之前测试此部分是否有效。
- 最后,检查最后一个单词是否以 "s" 结尾--因为它是最后一个单词,"s" 将始终出现在字符串中的相同索引处。*
问题 #2: 如果我们计算 S 的数量,那将计算单词中的所有 S,而不仅仅是最后一个。我怎样才能弄清楚每个给定单词的最后一个字母是否以 S 结尾。到目前为止我有这个:
noun = input("Enter nouns: ")
print("You entered: ", noun)
words = noun.split()
print(words)
amount = len(words)
print(amount)
我不认为我可以简单地做一个 words.count('s')
。非常感谢任何帮助,谢谢。
将与用户输入相同,使用 .split()
和 str.endswith()
data = 'cat dogs people lovers'
y = data.split()
print(len(y))
x = [i for i in y[:-1] if i.endswith('s')]
print(len(x))
if y[-1].endswith('s'):
print(y[-1])
不使用 .endswith()
y = data.split()
print(len(y))
x = [i for i in y[:-1] if i[-1] == 's']
print(len(x))
if y[-1][-1] == 's':
print(y[-1])
您可以通过简单的列表理解来做到这一点:
test_input = 'apples carrots pickles tractor tree goat friends people'
plurals = [i for i in test_input.split() if i.endswith('s')]
total = len(plurals)
fraction = total/len(test_input.split())
如果您无法使用endswith()
,那么您可以使用索引:
plurals = [i for i in test_input.split() if i[-1]=='s']
请注意,split()
默认情况下会将输入字符串拆分为空格 (' '
)。
按照@Billthelizard 的提示,看起来最简单的解决方案是这种情况:
plurals = noun.count('s ')
谢谢大家! @billthelizard @toti08 @vash_the_stampede 和@rahlf23 你们的回复真的很有帮助。使用您的建议,我终于找到了正确的代码。我将在下面列出可能发生在这个问题上的任何其他人。再次感谢!
noun = input("Enter nouns: ")
print("You entered: ", noun)
words = noun.split()
print(words)
amount = len(words)
print(amount)
plural = noun.count('s ')
for i in noun:
if i[-1] == "s":
last = 1
else:
last = 0
plurals = plural + last
print(plurals)
fraction = plurals / amount
print(fraction)