python 嵌套循环列表
python nested loop list
我目前陷入了一个嵌套循环问题。如果有人能就如何解决我面临的这个棘手问题提供他们的见解或提示,我将不胜感激。
我正在尝试将一些值附加到 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)
您当前的代码在每次迭代时都会打印循环,因此将打印语句向左移动到与 for 循环相同的缩进级别,这样它只会在 for 循环完成后打印 运行它的迭代。
a=list()
for b in hugo:
a.append(ids)
print(a)
假设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))
以便您可以在两个列表中访问同一个索引。
使用字典而不是为包含的 ID 和名称使用 2 个单独的列表
下面的代码创建了一个字典,其中包含 id 作为键,相应的包含名称作为值。后来这个字典用于打印文件名
如果您想将每个包含保存为单独的文件,首先使用 "Or"(API) 隔离包含,然后我们在 ANSA 中为每个甲板做一个 API保存文件(确保启用可选参数 'save visible')。例如,对于 NASTRAN,它是 OutputNastran,您可以在脚本编辑器的 API 搜索选项卡中搜索它 window
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)
希望对您有所帮助
我目前陷入了一个嵌套循环问题。如果有人能就如何解决我面临的这个棘手问题提供他们的见解或提示,我将不胜感激。
我正在尝试将一些值附加到 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)
您当前的代码在每次迭代时都会打印循环,因此将打印语句向左移动到与 for 循环相同的缩进级别,这样它只会在 for 循环完成后打印 运行它的迭代。
a=list()
for b in hugo:
a.append(ids)
print(a)
假设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))
以便您可以在两个列表中访问同一个索引。
使用字典而不是为包含的 ID 和名称使用 2 个单独的列表 下面的代码创建了一个字典,其中包含 id 作为键,相应的包含名称作为值。后来这个字典用于打印文件名
如果您想将每个包含保存为单独的文件,首先使用 "Or"(API) 隔离包含,然后我们在 ANSA 中为每个甲板做一个 API保存文件(确保启用可选参数 'save visible')。例如,对于 NASTRAN,它是 OutputNastran,您可以在脚本编辑器的 API 搜索选项卡中搜索它 window
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)
希望对您有所帮助