Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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中将列表保存为文件?_Python_Save - Fatal编程技术网

如何从Python中将列表保存为文件?

如何从Python中将列表保存为文件?,python,save,Python,Save,我创建了一个简单的程序,让您输入一组数字,然后根据此人提供的数据创建一个随机生成的配对列表。 完成后如何保存数据(作为Windows文件)? 这是我的代码: import random as ran import easygui as eg nList=vList=eg.multenterbox(msg="Enter the names of the people:" , title="Random Pair Generator"

我创建了一个简单的程序,让您输入一组数字,然后根据此人提供的数据创建一个随机生成的配对列表。 完成后如何保存数据(作为Windows文件)? 这是我的代码:

import random as ran
import easygui as eg
nList=vList=eg.multenterbox(msg="Enter the names of the people:"
                , title="Random Pair Generator"
                , fields=('Name:', 'Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:','Name:',)
                , values=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']
                )

index=0
x=''
y=''
pList=[]
pair=''

while not(index==len(nList)):
   x=nList[index]
   y=ran.choice(vList)
   pair=x+'; '+y
   pList.insert(index, pair)
   vList.remove(y)
   index= index+1

    eg.textbox(msg="These are the pairs generated."
                , title= "Random Pair Generator"
                , text= str(pList)
                , codebox=0
                )
我只想将pList保存为文件,保存在计算机上的任何位置(最好是我可以指定的位置)。 此外,此循环还会产生问题。它不会引起语法或任何错误,但输出不是我想要的

它使用nList中的每个值,然后从vList中选择一个随机值,然后将这些值作为一个对象放入pList。然而,问题是,当我让它从vList中删除“y”的输出时,它也会从nList中删除它

示例:如果nList包含5个对象:[1,2,3,4,5],并且vList具有相同的对象[1,2,3,4,5]。它将从vList中为nList中的每个值选择一个随机数。
但是,一旦选择了vList中的变量,它将从列表中删除。问题是,假设pList从[1;2]开始,其中1;2是一个对象,下一个对象将从3开始。它将跳过2,因为2已被用作“y”值。

我不清楚您是否希望将
pList
编写为纯文本,或者作为一个列表,稍后可以轻松重新打开

第一种情况很简单:

f = open("path/your_filename.txt", 'w') # opening file object for writing (creates one if not found)
f.write(str(pList))                     # writing list as a string into your file
f.close()                               # closing file object
不能将非字符串Python对象直接写入文件。如果您也想保留对象类型(以便以后加载),最简单的方法之一是使用
pickle

import pickle

f = open("/path/your_filename.pkl", 'w')
pickle.dump(f, pList)
f.close()
并将其加载为:

import pickle

f = open("/path/your_filename.pkl", 'r') # opening file object for reading
pList = pickle.load(f)
f.close()

希望这能有所帮助。

如果您只想以与
例如文本框中显示的相同格式保存配对列表,请在程序末尾添加类似的内容:

filename = eg.filesavebox(msg=None
                        , title='Save Pair List'
                        , default="pairs.txt"
                        , filetypes=['*.txt']
                        )

with open(filename, 'wt') as output:
    output.write(str(pList)+'\n')
您可以将列表的每一对写入输出文件的单独一行,如下所示:

with open(filename, 'wt') as output:
    for pair in pList:
        output.write(pair+'\n')

使用
with
语句意味着文件控制的代码块完成后,文件将自动关闭。

谢谢!这起到了拯救它的作用。。。但我有没有办法解决我的第二个问题?nList中有20个值,它只生成10对,因为一旦使用了vList中的值(与nList最初相同),它会以某种方式同时从nList和vList中删除,而我只希望它从vList中删除。谢谢。关于你问题的第二部分,我没有(现在仍然没有)完全理解你想要达到的目标。一般来说,使用
random.shuffle()可以避免从列表中删除元素的问题
功能。同样关于第二个问题:您在更改
vlist
的同时更改
nlist
时遇到的问题,可以通过在单独的行上执行此操作,将其中一个作为另一个的独立副本来避免。现在的情况是,它们只是同一个列表中的不同名称,这就是为什么更改会影响它们。是的,这就解决了它。我不知道你必须这么做。谢谢顺便说一句,如果你喜欢的话,习惯上也会对你接受的答案投赞成票。