【问题标题】:python nested loop listpython嵌套循环列表
【发布时间】: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

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

【问题讨论】:

  • 什么是hugoids 是什么?嵌套循环在哪里?
  • 你的意思是[-1]?

标签: python list for-loop nested-loops


【解决方案1】:

您的问题在于代码缩进:

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

【讨论】:

    【解决方案2】:

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

    如果您想将每个包含保存为单独的文件,首先使用“或”(API)隔离包含,然后我们为 ANSA 中的每个卡组提供一个 API 来保存文件(确保启用可选参数 'save visible ').例如对于 NASTRAN,它是 OutputNastran,您可以在脚本编辑器窗口的 API 搜索选项卡中搜索它

    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)
    

    希望对你有帮助

    【讨论】:

    • 请考虑在您的答案中添加一些解释和细节。虽然它可能会回答问题,但仅添加一些代码并不能帮助 OP 或未来的社区成员理解问题或解决方案。
    【解决方案3】:

    假设 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(hugo)是一回事。
    • @Selcuk 没错,但他的问题一开始并没有多大意义,而且一旦有人为他做功课,他可能就会消失。他的代码甚至没有被正确编辑。
    • 编写一个什么都不做的列表理解虽然意义不大。此外,即使我不确定 OP 的要求是什么,但我认为他们并没有要求以另一种方式来完成同样的事情。
    • @Selcuk 他想要一个 for 循环来获得乐趣,所以我添加它只是为了后代。至少他现在有一个基本上是 hugo.copy() 的列表,如果他想的话,他可以随意处理:P
    • 您好,我编辑了我对上述问题的解释 :) 也许我最初的解释太含糊了 :)
    【解决方案4】:

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

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

    【讨论】:

    • 不认为显示正确的代码会使其成为更好的答案吗?谢谢。
    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 2019-04-22
    • 2018-01-06
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多