我可以在python中同时进行变量赋值和列表插入吗?
上下文:我想指定我想要脚本创建的一堆目录的名称。我想将这些路径名称声明为变量,并将它们添加到列表中,我将循环使用这些目录。 Python 2.7.5。我可以在python中同时进行变量赋值和列表插入吗?
我想以下几点:
dir_list = [
(dir_1 = "<path_to_dir_1>")
(dir_2 = "<path_to_dir_2>")
]
我也得到:
(dir_1 = "<path_to_dir_1>"),
^
SyntaxError: invalid syntax
如果我做的:
dir_1 = "<path_to_dir_1>"
dir_2 = "<path_to_dir_2>"
dir_list = [dir_1, dir_2]
print dir_list
它工作正常,没有错误。
我已经搜查,没有找到这个问题的答案,但我没有找到一个答案,如何在Python格式化多行字典:What is the proper way to format a multi-line dict in Python?
而且PEP8风格指南对如何提供指导格式多行列表以及:https://www.python.org/dev/peps/pep-0008/#indentation
但是,我还没有找到答案是否可以折叠变量赋值和列表包含到一个步骤。
我认为简短的答案是否定的。在Python 2.7中,assignment statement是一个语句,因此不能包含在list element的上下文中。可能有办法将列表解压缩到其他变量中,但不完全如您所述。我期待着真正的Python专家:)。
不可以。赋值不是一个表达式(它不计算任何东西),所以不能同时赋值给变量和插入到列表中。
你可以用字典对付它:
dir_list = [
{"dir_1" : "<path_to_dir_1>",
"dir_2" : "<path_to_dir_2>"}
]
,如果它一定要使用列表中,你不能声明一个变量像你这样,你只能做到这一点:
dir_list = ["<path_to_dir_1>", "<path_to_dir_2>"]
这仅仅是不可能的,因为你正在做什么都会被视为assignment
:
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
以便在赋值操作数=
右侧的每个表达式都将在左侧评估,而您的正式值为syntax error
。
你的代码是混乱的,但我不认为你需要一个字典,你只需要的路径列表,这是这样的:
dir_list = ['c:/users/me/hi', 'c:/users/you/hi', ...]
然后你可以迭代它:
for path in dir_list:
os.mkdir(path) #or os.mkdirs(path), check the usage
如果你想命名每个路径,就可以使用,但是一个字典:
dir_dict = {'me': 'c:/users/me/hi', 'you': 'c:/users/you/hi'...}
然后你就可以遍历按键名称:
for name in dir_dict.keys():
path = dir_dict[name]
os.mkdir(path)
然后,您可以以某种方式合并这些名称。
编辑:如果你想,例如,从这样的源代码编译:
source = [('name', 'c:/path/%s'), ('you', 'c:/otherpath/%s'), ('him', 'c:/yetanotherpath/%s')]
dir_paths = dict()
for elem in source:
dir_paths[elem[0]] = elem[1] % (elem[0])
dir_paths
> {'him': 'c:/yetanotherpath/him',
'name': 'c:/path/name',
'you': 'c:/otherpath/you'}
我不清楚你如何建立你的路径和名称列表 - 是有一些工艺,还是你只是在输入一堆?
你说得对,我不需要一个词典 - 这就是为什么我没有制作词典。我希望目录具有变量名称 - 如果稍后有人出现并想要更改仍然逻辑上指向相同资源的目录的磁盘位置,该怎么办?变量名称保持逻辑连接,同时允许分配新的物理位置。好处是分离物理层和逻辑层。 – bobo
如果您想命名路径以供将来参考,则字典就是您需要的。你的第一个代码与语法错误看起来就像创建一个字典。或者,如果订单对列表的维护方式而言很重要,则可以使用集合中的ordereddict。 – Jeff
如果我使用字典,可以在插入字典的同时进行分配吗? 所以: dir_dict = { '我': 'C:/用户/我', '我的东西': '%s' 的%(我+ '/的MyStuff')} '我的东西' 应该求:'c:/ user/me/mystuff' 这是你的意思吗? – bobo
如果没有您的情况的完整背景,我的首选是保持简单。
dir_1 = '<path_to_dir_1>'
dir_2 = '<path_to_dir_2>'
dir_list = [dir_1, dir_2]
print dir_list
但是,因为你已经明确拒绝了这一点,我的下一个选择是一个字典
dir_dict = dict(
dir_1='<path_to_dir_1>',
dir_2='<path_to_dir_2>',
)
print dir_dict.values()
你好像不喜欢类型的字典要么,所以我将提供这个怪物,滥用locals()
,但我强烈建议反对它。
dir_1 = '<path_to_dir_1>'
dir_2 = '<path_to_dir_2>'
dir_list = [path for name, path in locals().items() if name.startswith('dir_')]
print dir_list
Python是所有关于简单(和字典),所以我会说拥抱他们。
最后一种选择:您可以创建一个字典子类,允许对其键进行属性访问。这意味着路径名称周围的引号减少了。
class PathCollection(dict):
def __init__(self, *args, **kwds):
super(PathCollection, self).__init__(*args, *kwds)
self.__dict__ = self
paths = PathCollection()
paths.dir_1 = '<path_to_dir_1>'
paths.dir_2 = os.path.join(paths.dir_1, '<relative_to_path_dir_2>')
print paths.values()
该列表消除了变量的所有需求。只需使用该列表。 – user2357112