Python - 如何在追加字典后保存输出?

时间:2016-03-30 16:17:14

标签: python loops dictionary

这是我的脚本,用于计算'sentences.txt'中行之间的余弦距离。 'print temp'返回22个不同的行。如何使用22个唯一键和值将结果保存到dict?

_.pluck(contactsWithAtLeastOnePhoneNumber, 'phoneNumbers')

这是'sentences.txt'

import re
import numpy as np

with open('./Tests/1/sentences.txt') as file_obj: 
    file_obj_list = list(file_obj) 

file_to_str = ''.join(file_obj_list)
lower_file_to_str = file_to_str.lower()
strip_lower_file_to_str = lower_file_to_str.strip()
splitted_file_to_str = re.split('[^a-z]', strip_lower_file_to_str)
filtered_str = filter(None, splitted_file_to_str)
dict_str = {i:filtered_str.count(i) for i in filtered_str}
array_dict = np.array(dict_str.keys(), dtype=str)

for line in file_obj_list:
    file_string = ''.join(line) 

lower_file_string = file_string.lower()
strip_lower_file_string = lower_file_string.strip()
splitted_file_string = re.split('[^a-z]', strip_lower_file_string) 
filtered_file_string = filter(None, splitted_file_string) 

temp = []
for word in array_dict:
    if word in filtered_file_string:
        word = filtered_file_string.count(word)
        temp.append(word)
    else:
        word = 0
        temp.append(word)
print temp

这是'print temp'输出(22行):

In comparison to dogs, cats have not undergone major changes during the      domestication process.
As cat simply catenates streams of bytes, it can be also used to concatenate binary files, where it will just concatenate sequence of bytes.
A common interactive use of cat for a single file is to output the content of a file to standard output.
Cats can hear sounds too faint or too high in frequency for human ears, such as those made by mice and other small animals.
In one, people deliberately tamed cats in a process of artificial selection, as they were useful predators of vermin.
The domesticated cat and its closest wild ancestor are both diploid organisms that possess 38 chromosomes and roughly 20,000 genes.
Domestic cats are similar in size to the other members of the genus Felis, typically weighing between 4 and 5 kg (8.8 and 11.0 lb).
However, if the output is piped or redirected, cat is unnecessary.
cat with one named file is safer where human error is a concern - one wrong use of the default redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete the file you were just needing to read.
In terms of legibility, a sequence of commands starting with cat and connected by pipes has a clear left-to-right flow of information.
Cat command is one of the basic commands that you learned when you started in the Unix / Linux world.
Using cat command, the lines received from stdin can be redirected to a new file using redirection symbols.
When you type simply cat command without any arguments, it just receives the stdin content and displays it in the stdout.
Leopard was released on October 26, 2007 as the successor of Tiger (version 10.4), and is available in two editions.
According to Apple, Leopard contains over 300 changes and enhancements over its predecessor, Mac OS X Tiger.
As of Mid 2010, some Apple computers have firmware factory installed which will no longer allow installation of Mac OS X Leopard.
Since Apple moved to using Intel processors in their computers, the OSx86 community has developed and now also allows Mac OS X Tiger and later releases to be installed on non-Apple x86-based computers.
OS X Mountain Lion was released on July 25, 2012 for purchase and download through Apple's Mac App Store, as part of a switch to releasing OS X versions online and every year.
Apple has released a small patch for the three most recent versions of Safari running on OS X Yosemite, Mavericks, and Mountain Lion.
The Mountain Lion release marks the second time Apple has offered an incremental upgrade, rather than releasing a new cat entirely.
Mac OS X Mountain Lion installs in place, so you won't need to create a separate disk or run the installation off an external drive.
The fifth major update to Mac OS X, Leopard, contains such a mountain of features - more than 300 by Apple's count. 
Leopard, contains such a mountain of features - more than 300 by Apple's count.

我需要像这样在字典中插入行:

[1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0]
[1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0]
.
.
[0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]

1 个答案:

答案 0 :(得分:1)

如果您只是尝试使用发生次数创建dict,则可以使用理解。

d = {word:filtered_file_string.count(word) for word in array_dict}

这应该导致dict其中key = word,val = filtered_file_string对象中该单词的出现次数。