I am currently stuck at one nested loop problem. I would appreciate it greatly if anyone can offer their insight or tips on how to solve this sticky problem that i am facing.
I am trying to append some values to a list in a for loop. I succeeded in doing that. But how can I get the last list as my variable to use in another loop?
Lets say. I am extracting something by appending them in a list in a for loop.
a=list()
for b in hugo:
a.append(ids)
print(a)
gives me
[1]
[1,2]
[1,2,3]
[1,2,3,4]
But I only need the last line of the list as my variable to be used in another for loop. Can anybody gives me some insights how to do this? Your help is much appreciated. Thanks in advance.
Edit:
Actually I am not trying to get someone to do my homework for me. I am just testing some software programming using python. Here goes:
I am trying to write a script to extract files with the end name of .dat from ANSA pre-processor with the correct name and file ID
For example:
ID Name
1 hugo1.dat
8 hugo2.dat
11 hugo3.dat
18 hugo4.dat
Here is what I have written:
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
But it doesnt work well so far for me, that's why I am asking whether you all can provide me some assistance on solving this problem :) Helps appreciated!! Thanks all
Assuming ids is actually just the elements in hugo:
a=[id for id in hugo]
print(a)
Or
a=hugo.copy()
print(a)
Or
print(hugo)
Or
a=hugo
print(a)
Or
string = "["
for elem in hugo:
string.append(elem + ",")
print(string[:-1] + "]")
Edit: Added more amazing answers. The last is my personal favourite.
Edit 2:
Answer for your edited question:
This part
for a in ishow:
test=base.GetEntity(deck,'INCLUDE',int(a))
print(a)
file_path_name=directory+"/"+iname
print(file_path_name)
Needs to be changed to
for i in range(len(ishow)):
test=base.GetEntity(deck,'INCLUDE',int(ishow[i]))
file_path_name=directory+"/"+iname[i]
The print statements can be left if you wish.
When you are trying to refer to the same index in multiple lists, it is better to use for i in range(len(a))
so that you can access the same index in both.