Python Forum
Loop through a list of string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through a list of string
#1
Hello,
I would like to write the following code,

caeXmlObjects.kernel.mainXML.saveXMLRecords(fileName='settings.xml', 
    className='caeXmlObjects.paths.pathXML.PathXML', silentMode=False, outputType='File', 
    recordNames=('Path-0', 'Path-1', 'Path-2', ), params=('Path-0', 'Path-1', 'Path-2',  ), paramsPerSave=1)
but by introducing a list of paths variable, so I can have # params=('Path-0', 'Path-1', 'Path-2',...,Path-n, )
It will start like this for exemple:
Number_Fea = 3
Var_Paths = ["'Path-%d'" %i for i in range(Number_Fea)]
Reply
#2
Perhaps something like:

number_features = 3
var_paths = [f"Path-{i}" for i in range(number_features)]
Reply
#3
Thank you for your answer, but I'm more trying to create something like this
params=('Path-0', 'Path-1', 'Path-2',...,Path-n, )
so when I have 3 or 4 or 5 ... paths. my params=('Path-0',...) will change accordingly
Reply
#4
Put it in a function? I'm not sure what sort of interface you're looking for.

def path_params(n):
    number_features = n
    return [f"Path-{i}" for i in range(number_features)]


params = path_params(3)
Reply
#5
To change the number of paths in var_paths, change the value of "number_features".
import random
number_paths = random.randint(1, 10)
var_paths = [f"Path-{i}" for i in range(number_paths )]
print(var_paths)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through a list of string oldtrafford 3 1,713 Feb-15-2022, 04:42 PM
Last Post: snippsat
  loop for dynamic cut string - cleaner way? korenron 4 1,956 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 2,188 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  Convert string to JSON using a for loop PG_Breizh 3 2,988 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  Appending to list of list in For loop nico_mnbl 2 2,373 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,399 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  String slicing and loop iteration divyansh 9 4,760 Jun-07-2020, 10:29 PM
Last Post: divyansh
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,260 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 2,351 Feb-05-2019, 12:52 PM
Last Post: perfringo
  loop through list or double loop 3Pinter 4 3,456 Dec-05-2018, 06:17 AM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020