繁体   English   中英

腌制所有变量

[英]Pickle all variables

我正在寻找一种方法来按排序顺序腌制所有变量及其值。我正在尝试以下方法:

预期结果: va的 Append 值到totalList ,然后vb's值..等等。 然后将totalList转储到 pickle 文件中。

import pickle
import time
import os
import re

v0 = ["r0", "hello","hello there","hi there","hi", "hey","good to see you"]
v1 = ["r1", "how are you", "how do you do","how are you doing", "how you doing"]
v2 = ["r2", "What's up" , "what is up" , "what's up bro"]
v10 = ['r10', 'who made you', 'who built you', 'who is your creator']


imports = "pickle","time","os","re"
totalList = []
for key in dir():
    if key.startswith("__") == 0 and key not in imports:
        print(globals()[key])
        totalList.append(globals()[key])
# print(totalList) # getting weird values

with open('queryList.pkl', 'wb') as f:
    pickle.dump(totalList, f)

我得到这样的结果:

>> ('pickle', 'time', 'os', 're')
>> []
>> ['r0', 'hello', 'hello there', 'hi there', 'hi', 'hey', 'good to see you']
>> ['r1', 'how are you', 'how do you do', 'how are you doing', 'how you doing']
>> ['r10', 'who made you', 'who built you', 'who is your creator']
>> ['r2', "What's up", 'what is up', "what's up bro"]

我想摆脱结果('pickle', 'time', 'os', 're')[]并在附加到totalList之前对结果进行排序或在迭代之前对其进行排序。

这是一种做你想做的事的方法。 您应该忽略一些您不感兴趣的变量 - 特别是importstotalList

ignore_list = [ "ignore_list", "totalList"]
totalList = []

for key in dir():
    if not key.startswith("__") and key not in ignore_list and type(globals()[key]) != type(pickle):
        print(key)
        totalList.append(globals()[key])

totalList = sorted(totalList, key=lambda x: int(x[0][1:]) )

结果是:

[['r0', 'hello', 'hello there', 'hi there', 'hi', 'hey', 'good to see you'],
 ['r1', 'how are you', 'how do you do', 'how are you doing', 'how you doing'],
 ['r2', "What's up", 'what is up', "what's up bro"],
 ['r10', 'who made you', 'who built you', 'who is your creator']]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM