gpt4 book ai didi

Python Count Characters(Python计数字符数)

转载 作者:bug小助手 更新时间:2023-10-25 22:37:53 25 4
gpt4 key购买 nike



Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. The output should include the input character and use the plural form, n's if the number of times the characters appears is not exactly 1.
Ex: If the input is:

编写一个程序,其输入是一个包含一个字符和一个短语的字符串,其输出指示该字符在该短语中出现的次数。输出应包括输入字符,并使用复数形式,如果字符出现的次数不完全为1,则为n。例如:如果输入为:


n Monday
the output is:

星期一的输出是:


1 n
Ex: If the input is:

1 n Ex:如果输入为:


z Today is Monday
the output is:

Z今天是星期一,输出是:


0 z's
Ex: If the input is:

0 z‘s Ex:如果输入为:


n It's a sunny day
the output is:

如果是晴天,输出为:


2 n's
Case matters. n is different than N.

2n的案件很重要。N不同于N。


Ex: If the input is:

例如:如果输入为:


n Nobody
the output is:

N无人输出为:


0 n's

0%n%s


This is what I have so far:

这就是我到目前为止所拥有的:


user_string=input(str())
character=user_string[0]
phrase=user_string[1]
count=0

for i in phrase:
if i == character:
count = count+1

if count!= 1:
print(str(count) + " " + character + "'s")
else:
print(str(count) + " " + character)

This works great for the phrases that have 0 characters matching. But its not counting the ones that should match.

这对于有0个字符匹配的短语非常有效。但这并不包括那些应该匹配的。


更多回答

Just an FYI, the 'str()' at the input is not necessary, input returns the entered value as a string by default

仅供参考,输入处的‘str()’不是必需的,默认情况下,输入以字符串形式返回输入的值

a closely related topic is disscussed here: stackoverflow.com/questions/1374457/…

这里讨论了一个密切相关的主题:Stackoverflow.com/Questions/1374457/…

优秀答案推荐

string = input()
chr = string[0]
phrase = string[1:]
count = phrase.count(chr)
print(count)

not as detailed as others but this got me a 10/10 on zybooks.

不像其他人那样详细,但这让我在zyBooks上得到了10分。



Suggest just using str.count.

建议只使用str.count。


user_string = input()
character, phrase = user_string[0], user_string[1:]
count = phrase.count(character)

print(f"{count} {character}" + ("'s" if count != 1 else ''))


We will take the user's input, with the assumption that the first letter is the one that you are counting, and find that character with user_string.split()[0]. We will then take all the other words from the user's input (with user_string.split()[1:]), join them with ''.join and then explode them into a list of letters with [*]. We will return a list of "hits" for the character we are looking for. The length of that list will be the number of "hits".

我们将接受用户的输入,假设第一个字母就是您要计算的那个字母,并使用USER_STRING.Split()[0]找到该字符。然后,我们将从用户的输入中获取所有其他单词(使用USER_STRIN.Split()[1:]),用‘’.Join将它们连接起来,然后用[*]将它们分解成一个字母列表。我们将返回一个列表的“命中”为我们正在寻找的角色。该列表的长度将是“点击”的数量。


user_string=input()

numOfLetters = [letter for letter in [*''.join(user_string.split()[1:])]
if user_string[0]==letter]
print(f'Number of {user_string[0]} is: {len(numOfLetters)}')

t This is a test    # Input
Number of t is: 2 # Output

h Another test for comparison # Input
Number of h is: 1 # Output


user_phrase=input()
letter=user_phrase[0]
if letter in user_phrase:
if user_phrase.count(letter)>2:
print (f'{user_phrase.count(letter)-1} {letter}\'s')
elif user_phrase.count(letter)==1:
print(f'0 {letter}\'s')
else:
print(f'{user_phrase.count(letter)-1} {letter}')


This is my answer to my zybooks lab it is not the best answer on the thread but is the second right after Chris's answer. Since his adopts the technique where you don't have to subtract one from your count. The techniques he employed are things to adopt imo.

这是我对zyBooks实验室的回答,它不是最好的答案,但仅次于克里斯的答案。因为他采用的技术是你不需要从你的计数中减去一。他采用的技术是采用国际海事组织的。


phrase = input()
for letter in phrase[0]:
count = phrase.count(letter) - 1 #This counts the amt of letter in phrase, then subtracts 1 excluding index 0
if count != 1: #1 isn't plural so to say "1 f's" doesn't make sense
print(count, str(letter)+"'s") #sample output [4 f's]
else: #and there we have it
print(count, str(letter)) #sample output [1 f]



user_string = input()
character, phrase = user_string[0], user_string[1:]
count = phrase.count(character)
print(f"{count} {character}" + "'s" if count != 1 else f"{count} {character}")


user_input = input()    
x = user_input[0]
y = user_input.count(x) - 1

print(y)


user_string=input(str())
character=user_string[0]
phrase=user_string[1:]
count=0

for i in phrase:
if i == character:
count = count+1

if count != 1:
print(str(count) + " " + character + "'s")
else:
print(str(count) + " " + character)


letter_and_phrase = str(input())
number_times_char_appears = 0
begin_letter = letter_and_phrase[0]
split_phrase = letter_and_phrase[1:].strip(' ')

for letter in split_phrase:
if letter in begin_letter:
number_times_char_appears += 1

if number_times_char_appears > 1:
print(f"{number_times_char_appears} {begin_letter}'s")
elif number_times_char_appears == 0:
print(f"{number_times_char_appears} {begin_letter}'s")
else:
print(f"{number_times_char_appears} {begin_letter}")

更多回答

Thank you for posting your solution. I see you are an new user on StackOverflow. It would be helpful if your answer had more explanation on why it is a good solution or more info about the syntax that would help educate others.

感谢您发布您的解决方案。我看到您是StackOverflow的新用户。如果你的答案有更多关于为什么它是一个好的解决方案的解释,或者更多关于有助于教育他人的语法的信息,这将是有帮助的。

This seems like a very roundabout way to say count = sum(1 for ch in s[1:] if ch == s[0]).

这似乎是一种非常迂回的方式来表示count=sum(S[1:]if ch==S[0]中的ch为1))。

Could you add a comment that explains what your code does?

您能添加一个注释来解释您的代码的作用吗?

This looks to be an attempt to correct Chris' answer.

这似乎是为了纠正克里斯的答案。

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

请记住,Stack Overflow不仅仅是为了解决眼前的问题,也是为了帮助未来的读者找到类似问题的解决方案,这需要理解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。考虑到这一点,你能编辑你的答案,包括你正在做的事情的解释以及为什么你认为这是最好的方法吗?

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com