一行程序语法python
我想数一数一个文件中每个单词的数量,我正在尝试写它 作为一行代码,但我得到一个无效的语法错误,我不明白为什么, 或者如何改变它 我的代码:一行程序语法python,python,syntax-error,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
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”时,语法还可以(当我从原始的一行代码中删除它时,语法仍然是invaid)
def打印字(文件名):
my_file=(打开(文件名'r').readlines())
单词_dict={}
对于my_文件中的行:
行。下()
单词_in_line=line.replace(“\n”,”).split(“”)
对于单词中的单词,请参见第行:
如果单词中有单词:
单词dict[单词]=单词dict[单词]+1
其他:
单词dict[单词]=1
您可以使用正则表达式获取单词,并使用计数器类(来自集合)对其进行计数:
from collections import Counter
import re
with open("testfile.txt") as file: words = Counter(re.findall("\w+",file.read()))
如果文件很大,您可能需要逐行处理:
with open("testfile.txt") as file: words = Counter( w for line in file for w in re.findall("\w+",line.upper()))
使用默认dict而不是常规dict
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
...
或者进一步使用计数器
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))
...
你让我们猜猜错误是什么,在哪里。编辑问题以包含完整的错误消息。赋值不能是表达式的一部分。此外,在定义
word
之前,您正在使用word
作为索引。如前所述,这可能是使用计数器的好时机。1。如果我有一个几十GB的大文件,有没有比一次读取整个文件更有效的方法?这是最有效的方法吗?2.我怎么能不区分大小写地计算呢?
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))
...