简体   繁体   English

如何从另一个python文件导入列表

[英]How to import a list from another python file

I just started programming in Python so I would love a detailed explanation. 我刚刚开始用Python编程,所以我希望提供详细的解释。 Let's say I have a list of words at file number 1: 假设我有一个文件列表为1的单词列表:

list=["leaf","cream","pickles","vinegar","gouda","almond","fire","orbit","spider","symbol"]

In a cache called "Random Words". 在一个名为“ Random Words”的缓存中。

Now I'm coding a different file (file 2) called "The Selected Word". 现在,我正在编码一个称为“所选单词”的不同文件(文件2)。 I want to import a different word from file 1 each time I run file 2. What command should I write? 每次运行文件2时,我都想从文件1中导入一个不同的单词。我应该写什么命令? I tried this: 我尝试了这个:

word = random.choice.open("random words","w")
print(word)

And it didn't work ... 而且没有用...

to make things simple make sure randomwords.py and theselectedword.py are in the same folder and directory. 为简单起见,请确保randomwords.py和theselectedword.py位于同一文件夹和目录中。 in randomwords.py 在randomwords.py中

list_of_words = ['age','body',.... etc]
#do not use 'list' to name a variable or object,so as not to override the list function called list()

then in theselectedword.py 然后在selectedword.py中

from randomwords import list_of_words

import random
word = random.choice(list_of_words)
print(word)

You could use Pythons module system for this task: 您可以将Pythons模块系统用于此任务:

Save list in python script, "Random_Words.py": 将列表保存在python脚本“ Random_Words.py”中:

my_list = ["leaf"
           , "cream"
           , "pickles"
           , "vinegar"
           , "gouda"
           , "almond"
           , "fire"
           , "orbit"
           , "spider"
           , "symbol"
            ]

And now import this script as a module in the other program, "The_Selected_Word.py": 现在,将此脚本作为模块导入到另一个程序“ The_Selected_Word.py”中:

from Random_Words import my_list

word = random.choice(my_list)
print(word)

ok, from your question i am assuming that you have two files, 1st is file1.py and 2nd is file2.py . 好的,从您的问题中我假设您有两个文件,第一个是file1.py ,第二个是file2.py You have a list named list in file1.py and you want to import list to file2.py and from this list, you want to get random words. 您在file1.py有一个名为list ,并且想要将list导入到file2.py并且想要从该列表中获取随机单词。

so for this, you can do in file2.py 因此,您可以在file2.py

from file1 import list
import random

words = random.choice(list)
print(words)
  1. You can't import Python files using any file-open library, like 您无法使用任何文件打开库(例如
open('random_words', 'w')
# IT DOESN'T WORK

Because you will not be able to use code from these files 因为您将无法使用这些文件中的代码

But you can use "import" instead: 但是您可以使用“导入”代替:

import random_words

Or: 要么:

from random_words import words
  1. Don't use "list" name for any of your variable, because it's special Python word 不要对任何变量使用“列表”名称,因为它是特殊的Python单词
list = ['some word', 'word']
# IT DOESN'T WORK

Use another name instead: 请改用其他名称:

words = ['some_word', 'word']
  1. Let's combine this: 让我们结合一下:
# random_words.py

words = [
"leaf",
"cream",
"pickles",
"vinegar",
"gouda",
"almond",
"fire",
"orbit",
"spider",
"symbol"
]
# the_selected_word.py

from random_words import words
print(words)

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

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