Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个词典写入一个输出文件;txt";_Python_Python 3.x - Fatal编程技术网

Python 如何将两个词典写入一个输出文件;txt";

Python 如何将两个词典写入一个输出文件;txt";,python,python-3.x,Python,Python 3.x,这实际上是一个由4部分组成的问题: 1) 返回一个字典,其中每个关键字都是单词长度,其值是具有该长度的单词数。 e、 g.如果输入文件的文本是“Hello Python people Welcome to the world of Python”,那么字典应该是: {2:2,3:1,5:2,6:3,7:1} 2) 返回一个字典,其中每个关键字都是一个单词,其值是该单词的出现次数。 e、 g.{'hello':1,'of':1,'people':1,'python':2,'the':1,'to':

这实际上是一个由4部分组成的问题:

1) 返回一个字典,其中每个关键字都是单词长度,其值是具有该长度的单词数。 e、 g.如果输入文件的文本是“Hello Python people Welcome to the world of Python”,那么字典应该是: {2:2,3:1,5:2,6:3,7:1}

2) 返回一个字典,其中每个关键字都是一个单词,其值是该单词的出现次数。 e、 g.{'hello':1,'of':1,'people':1,'python':2,'the':1,'to': 1,“欢迎”:1,“世界”:1}

我已经用下面的代码完成了前两部分

def make_length_wordcount(x):
    filename=x+'.txt'
    infile=open(filename)
    wordlist=infile.read().split()
    counter1={}
    for word in wordlist:
        if len(word) in counter1:
            counter1[len(word)]+=1
        else:
            counter1[len(word)]=1
    infile.close()
    print(counter1)

def make_word_count(string):
    words=string.lower().split()
    dictionary={}
    for word in words:
        dictionaryp[word]=0
    for word in words:
        dictionary[word]+=1
    print(dictionary) 
我很难理解如何完成第3)部分和第4部分):

3) 使用上面的两个函数——
make\u length\u wordcount()
make\u word\u count()
——构建(i)length wordcount字典和(ii)word count字典

打开一个新的输出文件
“file\u analysisted\u FIRST\u LAST.txt”
,并在此文件中写入两个词典(格式如下)。输出文件名为
“test\u analysisted\u HYUN\u KANG.txt”
,它应该包含以下行:

Words of length 2 : 2
Words of length 3 : 1
Words of length 5 : 2
Words of length 6 : 3
Words of length 7 : 1
to : 1
of : 1
people : 1
the : 1
python : 2
welcome : 1
hello : 1
world : 1
4) 在“hw2_FIRST_LAST.py”文件中,使用以下输入运行analyze_text()函数三次:
a。“nasdaq.txt”
b。“raven.txt”
c。“frankenstein.txt”

您的hw2.py代码应生成以下三个文件:
“nasdaq\u analysisd\u FIRST\u LAST.txt”
“raven\u analysisd\u FIRST\u LAST.txt”
“科学怪人”首先分析了\u FIRST\u LAST.txt“

我的老师没有真正教我们写文件,所以这让我很困惑。

首先要讲几件事: 1) 你可以避免

如果计数器1中的len(字):
计数器1[len(word)]+=1
其他:
计数器1[len(word)]=1
通过使用集合模块中的defaultdict或计数器:

counter1=defaultdict(int)
对于单词列表中的单词:
计数器1[字]+=1
这同样适用于
make\u word\u count

def make_word_count(字符串):
words=string.lower().split()
字典=计数器(单词)
印刷(字典。最常见(10))
对于你的第三点(我没有测试,但你知道了):

def make_text_wordlen(计数器1):
文本=“”
对于wordlen,计数器1.items()中的值:
text+=f'长度为{wordlen}:{value}的单词\n'
将open('your_file.txt','w')作为f:
f、 书写(文本)
def make_text_wordcount(字典):
文本=“”
对于word,在dictionary.items()中计数:
text+=f'{word}:{count}\n'
将open('your_file.txt','a')作为f:#'a'参数附加到现有文件
f、 书写(文本)
我会让你明白第四点。

首先要做几件事: 1) 你可以避免

如果计数器1中的len(字):
计数器1[len(word)]+=1
其他:
计数器1[len(word)]=1
通过使用集合模块中的defaultdict或计数器:

counter1=defaultdict(int)
对于单词列表中的单词:
计数器1[字]+=1
这同样适用于
make\u word\u count

def make_word_count(字符串):
words=string.lower().split()
字典=计数器(单词)
印刷(字典。最常见(10))
对于你的第三点(我没有测试,但你知道了):

def make_text_wordlen(计数器1):
文本=“”
对于wordlen,计数器1.items()中的值:
text+=f'长度为{wordlen}:{value}的单词\n'
将open('your_file.txt','w')作为f:
f、 书写(文本)
def make_text_wordcount(字典):
文本=“”
对于word,在dictionary.items()中计数:
text+=f'{word}:{count}\n'
将open('your_file.txt','a')作为f:#'a'参数附加到现有文件
f、 书写(文本)
我来告诉你第四点