如何查找python中有多少单词?
好的,我需要一个输出,它告诉我同一个单词出现了多少次。是否有我可以使用的代码或函数?我正在使用Grok Learning来实现这一点。这正是我所需要的:我正试图找出这段代码。为此,我需要最简单的代码。这就是我的任务:如何查找python中有多少单词?,python,python-3.x,Python,Python 3.x,好的,我需要一个输出,它告诉我同一个单词出现了多少次。是否有我可以使用的代码或函数?我正在使用Grok Learning来实现这一点。这正是我所需要的:我正试图找出这段代码。为此,我需要最简单的代码。这就是我的任务: 当你等待过马路时,你看着汽车从你身边驶过 想知道红色还是蓝色是汽车比较流行的颜色 编写一个程序,读取每辆车的颜色字符串 开车经过,然后打印出红色汽车的数量和数字 蓝色的汽车 Cars: silver red white white blue white black green y
当你等待过马路时,你看着汽车从你身边驶过 想知道红色还是蓝色是汽车比较流行的颜色 编写一个程序,读取每辆车的颜色字符串 开车经过,然后打印出红色汽车的数量和数字 蓝色的汽车
Cars: silver red white white blue white black green yellow silver white
red: 1 blue: 1
Cars: blue green white black silver silver silver blue silver black silver white white silver white white yellow red red silver red
red: 3 blue: 2
Cars: yellow green white silver white blue white silver yellow pink
red: 0 blue: 1
它输出的单词“红色”或“蓝色”的数量不正确
请帮助:)算法
>>> day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
>>> rain_days = raw_input("Which days had rain (use space for saparated)? ")
Which days had rain (use space for saparated)? wednesday friday
>>> day_count_without_rain = set(day).difference(days).__len__()
>>> print day_count_without_rain
5
>>>
>>> car_colors = raw_input("Enter string of the colour of each car that drives past")
Enter string of the colour of each car that drives past red blue Red white black green
# Convert to lower case.
>>> car_colors = car_colors.lower()
#- spit colors in to list.
>>> car_colors = car_colors.split()
>>> print "Red Color cars count:", car_colors.count("red")
Red Color cars count: 2
>>> print "Blue Color cars count:", car_colors.count("blue")
Blue Color cars count: 1
>>>
>>> l = [1,2,3,1, 0]
>>> l.count(1)
2
>>> l.count(11)
0
>>> a = "aagghhttee"
>>> a.count("a")
2
>>> a.count("aa")
1
>>> a.count("aaa")
0
>>>
注意:
在Python 2.x中使用raw\u输入
在Python 3.x中使用input
[编辑2]
使用列表及其计数方法
演示:
>>> day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
>>> rain_days = raw_input("Which days had rain (use space for saparated)? ")
Which days had rain (use space for saparated)? wednesday friday
>>> day_count_without_rain = set(day).difference(days).__len__()
>>> print day_count_without_rain
5
>>>
>>> car_colors = raw_input("Enter string of the colour of each car that drives past")
Enter string of the colour of each car that drives past red blue Red white black green
# Convert to lower case.
>>> car_colors = car_colors.lower()
#- spit colors in to list.
>>> car_colors = car_colors.split()
>>> print "Red Color cars count:", car_colors.count("red")
Red Color cars count: 2
>>> print "Blue Color cars count:", car_colors.count("blue")
Blue Color cars count: 1
>>>
>>> l = [1,2,3,1, 0]
>>> l.count(1)
2
>>> l.count(11)
0
>>> a = "aagghhttee"
>>> a.count("a")
2
>>> a.count("aa")
1
>>> a.count("aaa")
0
>>>
计数:这将返回整数,即搜索元素在字符串或列表中出现的次数
演示:
>>> day = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
>>> rain_days = raw_input("Which days had rain (use space for saparated)? ")
Which days had rain (use space for saparated)? wednesday friday
>>> day_count_without_rain = set(day).difference(days).__len__()
>>> print day_count_without_rain
5
>>>
>>> car_colors = raw_input("Enter string of the colour of each car that drives past")
Enter string of the colour of each car that drives past red blue Red white black green
# Convert to lower case.
>>> car_colors = car_colors.lower()
#- spit colors in to list.
>>> car_colors = car_colors.split()
>>> print "Red Color cars count:", car_colors.count("red")
Red Color cars count: 2
>>> print "Blue Color cars count:", car_colors.count("blue")
Blue Color cars count: 1
>>>
>>> l = [1,2,3,1, 0]
>>> l.count(1)
2
>>> l.count(11)
0
>>> a = "aagghhttee"
>>> a.count("a")
2
>>> a.count("aa")
1
>>> a.count("aaa")
0
>>>
以下是最简单的解决方案,也是Grok Learning提供的示例解决方案:
cars = input('Cars: ')
cars = cars.split()
red = blue = 0
for car in cars:
if car == 'red':
red += 1
elif car == 'blue':
blue += 1
print('red:', red)
print('blue:', blue)
我希望这有帮助 有我可以使用的代码或函数吗?
那是什么,因为经过研究,我什么都没找到@inspectorG4dgetmy Attention在那里@AhsanulHaqueYou仍然没有告诉我们您的尝试有什么问题-预期的行为是什么,观察到的行为与itI有什么不同我很抱歉我不小心放置了错误的任务,我现在已经更新了它。请看一看,看看你是否能帮我:)@inspectorg4dgett谢谢你的帮助,但是如果我不打扰你,请你使用python 3的编码。虽然我做了一些改变。但它仍然不起作用?为什么?为什么不使用内置功能?@AnandSKumar:是的,但这在性能上更快len()
至。\uuuu len\uuuu()
非常感谢。这对我也有很大帮助:)非常感谢!我被困在这上面好几天了。这真的帮助了我。但是你能告诉我导入的作用吗?如果没有导入
,你将无法使用集合。计数器
,etcif没关系,你能解释整个代码吗?你在最初的问题中提到你在使用Grok Learning。我不能谈论该网站的质量,但如果您试图解决编码任务,但仍然对Python中的“导入”等基础知识感到困惑,那么可以参考Codecademy的Python课程()之类的内容。有一个基础的基础,这将使你想做的事情容易得多。我希望这不会让人觉得自命不凡,但目前看来这似乎有点本末倒置。