Python 嵌套循环列表



我目前陷入了一个嵌套循环问题。如果有人能就如何解决我面临的这个棘手问题提供他们的见解或提示,我将不胜感激。

我正在尝试将一些值附加到 for 循环中的列表中。我成功地做到了。但是我怎样才能让最后一个列表作为我的变量在另一个循环中使用呢?

比方说。我正在通过将它们附加到 for 循环的列表中来提取一些东西。

a=list()
for b in hugo:
a.append(ids)
print(a)

给我

[1]
[1,2]
[1,2,3]
[1,2,3,4]

但是我只需要列表的最后一行作为我的变量,就可以在另一个 for 循环中使用。任何人都可以给我一些如何做到这一点的见解吗?非常感谢您的帮助。提前谢谢。

编辑:

其实我并不是想找人帮我做功课。我只是在使用python测试一些软件编程。这里是:

我正在尝试编写一个脚本,以使用正确的名称和文件 ID 从 ANSA 预处理器中提取具有 .dat 结束名称的文件

例如:

ID       Name
1        hugo1.dat
8        hugo2.dat
11       hugo3.dat
18       hugo4.dat

这是我写的:

import os
import ansa
from ansa import base
from ansa import constants
from ansa import guitk
def export_include_content():
directory = gutik.UserInput('Please enter the directory to Output dat files:')
ishow=list()
includes=list()
setna=list()
iname=list()
# Set includes variables to collect the elements from a function known as "INCLUDE" from the software
includes=base.CollectEntitites(deck, None, "INCLUDE")
# For loop to get information from the "INCLUDE" function with the end filename ".dat"
for include in includes:
ret=base.GetEntityCardValues(deck, include, 'NAME', 'ID')
ids=str(ret['ID'])
setname=ret['NAME']
if setname.endswith('dat'):
ishow.append(ids)
iname.append(setname)
# Print(ishow) gives me
[1]
[1,8]
[1,8,11]
[1,8,11,18]
# print(iname) gives me
[hugo1]
[hugo1,hugo2]
[hugo1,hugo2,hugo3]
[hugo1,hugo2,hugo3,hugo4]
# Now that I got both of my required list of IDs and Names. It's time for me to save the files with the respective IDs and Names.
for a in ishow:
test=base.GetEntity(deck,'INCLUDE',int(a))
print(a)
file_path_name=directory+"/"+iname
print(file_path_name)
#print(a) gives me
1
8
11
18
#print(file_path_name) gives me
filepath/[hugo1,hugo2,hugo3,hugo4]
filepath/[hugo1,hugo2,hugo3,hugo4]
filepath/[hugo1,hugo2,hugo3,hugo4]
filepath/[hugo1,hugo2,hugo3,hugo4]
# This is the part I got stuck. I wanted the output to be printed in this order:
1
filepath/hugo1
8
filepath/hugo2
11
filepath/hugo3
18
filepath/hugo4

但到目前为止,它对我来说效果不佳,这就是为什么我要问你们是否可以为我提供一些帮助来解决这个问题:)帮助赞赏!!谢谢大家

您的问题在于代码缩进:

a=list()
for b in hugo:
a.append(ids)
print(a)

使用字典,而不是为 id 和包含名称提供 2 个单独的列表 下面的代码创建一个字典,其中包含 id 作为键,相应的包含的名称作为值。稍后此字典用于打印文件名

如果您想将每个包含保存为单独的文件,首先使用"Or"(API(隔离包含,然后我们为ANSA中的每个甲板都有一个API来保存文件(确保启用可选参数"save visible"(。

dict={}
for include in includes:
ret=base.GetEntityCardValues(deck, include, 'NAME', 'ID')
ids=str(ret['ID'])
setname=ret['NAME']
if setname.endswith('.dat'):
dict[ids]=setname
for k, v in dict.items():
test=base.GetEntity(deck,'INCLUDE',int(k))
file_path_name=directory+"/"+v
print(file_path_name)

希望这有帮助

假设 ids 实际上只是 hugo 中的元素:

a=[id for id in hugo]
print(a)

a=hugo.copy()
print(a)

print(hugo)

a=hugo
print(a)

string = "["
for elem in hugo:
string.append(elem + ",")
print(string[:-1] + "]")

编辑:添加了更多惊人的答案。最后一个是我个人的最爱。

编辑 2:

回答您编辑的问题:

这部分

for a in ishow:
test=base.GetEntity(deck,'INCLUDE',int(a))
print(a)
file_path_name=directory+"/"+iname
print(file_path_name)

需要更改为

for i in range(len(ishow)):
test=base.GetEntity(deck,'INCLUDE',int(ishow[i]))
file_path_name=directory+"/"+iname[i]

如果您愿意,可以保留打印语句。

当您尝试引用多个列表中的同一索引时,最好使用for i in range(len(a))以便可以访问两个列表中的同一索引。

当前代码每次迭代时都会打印循环,因此请将 print 语句向左移动到与 for 循环相同的缩进级别,以便仅在 for 循环完成运行其迭代后打印。

a=list()
for b in hugo:
a.append(ids)
print(a)

最新更新