【发布时间】:2019-12-23 02:50:49
【问题描述】:
我目前遇到一个嵌套循环问题。如果有人能就如何解决我面临的这个棘手问题提供他们的见解或提示,我将不胜感激。
我正在尝试将一些值附加到 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 测试一些软件编程。如下:
我正在尝试编写一个脚本,以从 ANSA 预处理器中提取具有正确名称和文件 ID 的结尾名称为 .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
但到目前为止它对我来说效果不佳,这就是为什么我问你们是否可以为我提供一些帮助来解决这个问题:) 帮助感谢!谢谢大家
【问题讨论】:
-
什么是
hugo?ids是什么?嵌套循环在哪里? -
你的意思是[-1]?
标签: python list for-loop nested-loops