如何从另一个模块调用用户定义的变量…:P

我想为x分配一个变量,在这种情况下,列表。
a = ['a','b','c']
b = ['d','e','f'] ##列表a和b都在一个单独的
x = a
打印(importedModule.x)
当我以这种方式运行它时,该程序只是在主要模块中仍未定义X,因此这是Psuedocode ...有什么想法吗?
我还想知道是否可以使用相同的解决方案来调用从导入的模块而不是简单变量的随机/用户定义的函数。

# 回答1


" A"和" X"都指向同一对象,因此没有区别,即使用" A"。

选择 | 换行 | 行号
  1. A = ['a', 'b', 'c']
  2.  
  3. x = A
  4. print id(A)
  5. print id(x)
  6. x[1] = "1"
  7. print A 

对我来说很好,所以发布您的代码。
自己尝试一下,看看。

# 回答2

@dwblas

-----------------------------------------------------------------------------
导入ListModule
x =输入('类型A,B或C')## A,B或C是在导入模块中存储的列表
打印(ListModule.x)
当以这种方式运行时,导入列表及其列表名称时,我会发现一个错误,说X未定义。
>>>
A型A,B或C.A
Trackback(最近的最新电话):
文件" c:/users/untral帐户/桌面/bbpowre3vrjh",第5行,in
打印(ListModule.x)
attributeError:"模块"对象没有属性'x'
>>>
对不起,我确实写了第一个示例。

# 回答3


作为用户输入的" A"是一个字符串,与变量完全不同的内存块中,a"在ListModule中。

选择 | 换行 | 行号
  1. ##---------- listModule  ----------
  2. A = ["a", "c", "b"]
  3. B = "ABC"
  4. C = 123
  5.  
  6.  
  7. ##---------- calling program  ----------
  8. import listModule
  9.  
  10. x = input('Type A, B, or C--> ')
  11. x = x.upper()
  12.  
  13. if "A" == x:     ## note the comparison...string==x
  14.    print(listModule.A)  ## not a string (in quotes), but a variable
  15. elif "B" == x:
  16.    print(listModule.B)
  17. elif "C" == x:
  18.    print(listModule.C)
  19.  
  20. # if you have a lot of comparisons you can use a list or tuple or dictionary
  21. print("----- second way")
  22. for check_it in (("A", listModule.A), ("B", listModule.B), ("C", listModule.C)):
  23.     if x = check_it[0]:
  24.         print(check_it[1]) 
# 回答4

@dwblas

-----------------------------------------------------------------------
我知道如何从导入的模块调用设定的函数或变量。...我不知道该怎么做是
紧凑地允许用户从导入的模块访问超过10,000(是........一万...)列表(实际上是几个模块:P)。我需要写10k Elif块。
我不能简单地写出每个Elif块。
好吧...我可以,但是我真的没有效果。大声笑:d
此外,这只是一个不切实际的文件。
我希望这可能具有最低的足迹。
是否没有办法将两者进入相同的内存块?

# 回答5


否。无法自动将键盘输入映射到内存中的变量。您必须自己编程,因为这正在编程计算机。您可以使用字典来简化事物。一个例子如下。

选择 | 换行 | 行号
  1. ##---------- listModule  ----------
  2. external_dict = {"A":["a", "b", "c"],
  3.                  "B":["B", "B", "b"],
  4.                  "C":[1, 2, 3] }
  5.  
  6.  
  7.  ##---------- calling program  ----------
  8.  import listModule
  9.  
  10.  x = input('Type A, B, or C--> ')
  11.  x = x.upper()
  12.  
  13. if x in listModule.external_dict:
  14.     print(listModule.external_dict[x])
  15.  
  16. # or
  17. choices_dict = {"A":listModule.A,
  18. "                B":listModule.B }  ## etc but that would require thousands of hand built entries 

几乎不可能在黑暗中以良好的选择射击。列表是否以任何方便的方式命名,可以自动放入字典中?您可以对列表模块进行编程以生成列表的生成列表吗?您能本身迭代程序的陈述并将列表提取到字典中吗?

# 回答6

@dwblas

------------------------------------------------------------------------------------ - -
哇。。哈哈
这就解释了为什么在"解决"中带有10K问题的紧凑型测试引擎并不常见。 :P感谢您 r帮助。

# 回答7

@dwblas --------------------------------------------------------------------- 将列表复制到字典应该工作。 再次... ty。
# 回答8

Python不是PHP:没有变量。 通常,数据应是使用腌菜保存的字典。 你不应该这样做 进口 数百/数千个列表/元组。 这是一个解决方案。 我在外部文件中写下了您的数据(非常出色的格式)。 data.txt: a = ['a','b','c'] b = ['d','e','f'] ...

选择 | 换行 | 行号
  1. import re
  2. f = open('data.txt', 'r')
  3. data = {}
  4. for line in f:
  5.     key, value = re.findall('([^=]+)=(.*)', line)[0]
  6.     data[key.strip()] = eval(value.strip())
  7. f.close()
  8. ## from here, you can save the dictionary with pickle,
  9. ## ready to be loaded and quickly accessed
  10. x = raw_input('Type A, B, or C--> ')
  11. print data[x.upper()][1]  # returns 'e' if x == 'b'
  12. del data
  13.  

避免使用eval()。

# 回答9

嘿...我已经写了这个程序...我前一段时间有答案。...不过,谢谢。

标签: python

添加新评论