一种班轮语法python

时间:2019-03-31 15:13:28

标签: python syntax-error

我想计算文件中每个单词的数量,我正在尝试写它 作为一行代码,但是我收到无效的语法错误,而且我不明白为什么, 或如何更改它。

我的代码:

def print_words(filename):
  my_file = open(filename, 'r')
  word_dict = {}
  for line in my_file:
    line.lower()
    words_in_line = line.split(" ")
    word_dict[word] += 1 if word_dict.get(word) else word_dict[word] = 0 
      for word in words_in_line

错误消息:

word_dict[word] += 1 if word_dict.get(word) else word_dict[word] = 0 for word in words_in_line
                                                                 ^
SyntaxError: invalid syntax

我也尝试编写一些不同的代码(将遵循代码),但是仍然遇到相同的错误。但是当我删除“ = 0”时,语法就可以了(当我从原始的一个衬里删除它时,语法仍然是无效的。)

my_file = open(filename, 'r')
word_dict = {}
for line in my_file:
  line.lower()
  words_in_line = line.split(" ")
  for word in words_in_line:
    word_dict[word] += 1 if word_dict.get(word) else word_dict[word] = 0

3 个答案:

答案 0 :(得分:1)

您可以使用正则表达式来获取单词,并使用Counter类(来自集合)对单词进行计数:

<p id='demo' style = 'display: none'>Hello Javascript</p>     
<button type='button' onclick="document.getElementById('demo').style.display='block'" >click me</ button>

如果文件很大,则可能需要逐行处理它:

from collections import Counter
import re
with open("testfile.txt") as file: words = Counter(re.findall("\w+",file.read()))

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?> <ContentPage Style="{StaticResource baseStyle}" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Flightio" x:Class="Flightio.MainPage" xmlns:ffimageloadingSVG="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms" xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"> <ContentPage.Content> <ScrollView> <Grid RowSpacing="8" x:Name="grid_Main"> <Grid.RowDefinitions> <RowDefinition Height="45" /> <RowDefinition Height="75" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="Logo.jpg" x:Name="image_logo" HorizontalOptions="Center" Margin="0,10,0,0"/> <RelativeLayout HorizontalOptions="FillAndExpand" x:Name="relLay_blueMenu" BackgroundColor="#1e559e" Grid.Row="1" Margin="10,0,10,0"> <ContentView BackgroundColor="White" x:Name="view_domesticMenu" Margin="0,5,0,0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.8}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"> <RelativeLayout x:Name="relLay_flightMenu" HorizontalOptions="FillAndExpand"> <Label Text="aliali" TextColor="Red" XAlign="Center" Margin="10,0,10,0"/> </RelativeLayout> </ContentView> <ContentView BackgroundColor="#1e559e" x:Name="view_internationalFlightMenu" Margin="0,5,0,0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.6}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"></ContentView> <ContentView BackgroundColor="#1e559e" x:Name="view_hotelMenu" Margin="0,5,0,0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.4}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"></ContentView> <ContentView BackgroundColor="#1e559e" x:Name="view_trianMenu" Margin="0,5,0,0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"></ContentView> <ContentView BackgroundColor="#1e559e" x:Name="view_busMenu" Margin="0,5,0,0" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.2}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"> <Label Text="aliali2" TextColor="Blue" XAlign="Center"/> </ContentView> </RelativeLayout> <!-- <ffimageloadingSVG:SvgCachedImage Source="baseline-flight.svg" x:Name="image_logo2" HorizontalOptions="Center" Grid.Row="2"/> --> </Grid> </ScrollView> </ContentPage.Content> </ContentPage>

答案 2 :(得分:0)

使用默认字典而不是常规字典。

from collections import defaultdict

def print_words(filename):
    with open(filename, 'r') as my_file:
        word_dict = defaultdict(int)
        for line in my_file:
            for word in line.lower().split(" "):
                word_dict[word] += 1

    ...

或者更进一步,然后使用Counter

from collections import Counter
from itertools import chain

def print_words(filename):
    flatten = chain.from_iterable
    with open(filename, 'r') as my_file:
        word_dict = Counter(flatten(line.lower().split(" ") for line in my_file))

    ...