【问题标题】:How to find how many words there are in python?如何查找python中有多少个单词?
【发布时间】:2015-10-01 06:49:38
【问题描述】:

好的,所以我需要一个输出,它告诉我同一个词出现了多少次。有我可以使用的代码或功能吗?我正在为此使用 Grok Learning。这正是我需要的:我正在尝试找出这段代码。为此,我需要最简化的代码。这就是我的任务:


当你等待过马路时,你正在看着汽车从你身边经过 想看看红色还是蓝色是更受欢迎的汽车颜色。

编写一个程序,读取每辆车的颜色字符串 开车过去,然后打印出红色汽车的数量和数量 蓝色汽车。

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 


我当前的代码是:

    colours = []
    cars = input("Cars: ")
    colours.append(cars)
    if "red" in cars:
        for colour in colours:
          print("red:",(len(colours)))
    if "blue" in cars: 
        print("blue:",(len(colours)))

它输出错误数量的单词“red”或“blue” 请帮忙:)

【问题讨论】:

  • Is there a code or function I could use?
  • 那是什么,因为经过研究我没有找到任何东西.. @inspectorG4dget
  • 我的尝试在那里@AhsanulHaque
  • 您仍然没有向我们展示您的尝试有什么问题 - 预期的行为是什么,以及观察到的行为与它有何不同
  • 很抱歉我不小心放错了任务,我现在已经更新了。请看一下,看看你是否可以帮助我:) @inspectorG4dget

标签: python python-3.x


【解决方案1】:

算法

  1. 定义天数列表
  2. 从用户那里获取下雨天数
  3. 使用集合操作获取集合 1 与集合 2 之间的差异
  4. 显示无雨天数。

演示

>>> 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
>>> 

注意: 在 Python 2.x

中使用 raw_input

Python 3.x

中使用 input

[编辑 2]

通过使用列表及其计数方法

演示

>>> 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
>>> 

count :这将返回整数,即搜索元素在字符串或列表中出现的次数。

演示

>>> 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 3 的编码。虽然我做了一些更改。但是还是不行?
  • 为什么是.__len__()?为什么不是 len 内置函数?
  • @AnandSKumar:是的,但这在性能上更快吗? len().__len__()
  • 非常感谢。这对我也有很大帮助:)
【解决方案2】:
import collections

colorCounts = collections.defaultdict(int, collections.Counter(input("cars: ").lower().split()))
needed = 'red blue'.split()
for n in needed:
    print("{}:".format(n), colorCounts[n])

【讨论】:

  • 非常感谢您!我被困在这几天了。这真的帮助了我。但是你能告诉我导入是做什么的吗?
  • 没有import,您将无法使用collections.Counter
  • 如果可以的话,你能解释一下整个代码吗?
  • 您在最初的问题中提到您正在使用 Grok Learning。我无法评价该网站的质量,但如果您正在尝试解决编码任务,但仍然对 Python 中的“导入”等基础知识感到困惑,那么可能值得一看 Codecademy 的 Python 课程 (codecademy.com/en/tracks/python)。有一个基础知识可以让您尝试做的事情大大变得更容易。我希望这不会显得傲慢,但目前这似乎是本末倒置。
【解决方案3】:

这是最简单的解决方案,也是 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)

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2021-04-27
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多