我正在对一个巨大的文件运行分析,这需要几个小时来完成,结果是一个字典,我需要为下一步,我想保存在一个文件中的输出,以保留它。但是当我在文件中编写输出时,它会将字典输出转换为str并保存它,但是python不能在将来将保存的str解释为字典例如我的输出字典是
output={a:[1,2]}
当我保存它时,它被保存为:
'{a:[1,2]}' #can not be interpreted as dictionary by python anymore for further use in future!
有没有什么方法可以让我将输出作为字典保存在文件中,或者python是否可以
编辑2:发布更多的代码,这样可以更容易地识别我的错误。代码似乎忽略了我的“继续”语句,因此它处理应该被忽略的单词,这会使输出中出现空键/值对。
for line in tweets_lines:
total = line.split() ###this gives me each line broken out by word. Need to get value of each word, then sum each line.
line_score = {}
for word in total:
我是编程新手,当我使用python时,我发现'in‘操作在不同数据结构上的性能有很大的不同。例如:
a=list_a######list_a and list_b both are lists,data scale:300,000
b=set(list_b)
t1=time()
s=0
for entry in a:
if entry in b:
s+=1
t2=time()
print t2-t1
我以这样的结果结束,这是非常有效的
0.0699999332428
但是,当我搜索list_b而不更改为设置数据结构时
a=list_a
b=list_b
t1=t
我在Python中有一个字典,把人的姓作为键,每个键都有多个链接到它的值。是否有一种方法可以使用for循环在字典中迭代,以搜索特定的值,然后返回链接到的键?
for i in people:
if people[i] == criteria: #people is the dictionary and criteria is just a string
print dictKey #dictKey is just whatever the key is that the criteria matched element is linked to
可能有多个匹
我有一份清单:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
在a列表中有多个检查数字的if用法。
while True:
if 3 in a:
some_work1 #(different)
if 4 in a:
some_work2 #(different)
if 8 in a:
some_work3 #(different)
if 11 in a:
some_work4 #(different)
if 1
我在看来学习Numba。在17:00,演示者屏幕上有以下代码:
@njit
def simulate_spring_mass_funky_damper(x0, T=10, dt=0.0001, vt=1.0):
times = np.arange(0, T, dt)
positions = np.zeros_like(times)
v = 0
a = 0
x = x0
positions[0] = x0/x0
for ii in range(len(times)):
if ii == 0:
我是Python的新手,想把文本文件读入字典。问题是它会读取所有内容,并且只保留最后一条记录。我想读入所有数据,并将所有内容存储在Python dict中。 它只是一个简单的文本文件读取和python数据字典存储。不确定为什么它不工作。如果有人能帮上忙,我会很感激。 book_data = {}
with open('test_data.txt', 'r', encoding='utf8') as raw_data:
for item in raw_data:
if ':' in ite