I would like to use a double array. But I still fail to do it. This what I did.
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']for dn in Name :for dr in Ver :if dr is None:Default = "asbsda"Path = os.path.join(Folder, Default)print(Path)else:Path = os.path.join(Folder, dr, dn)print(Path)
it return this:
D:\folder\gadfg5\asbsda
D:\folder\gadfg5\asbsda
D:\folder\gadfg5\asbsda
D:\folder\546sfdgh\hhdt5463
But my expectation of the output is:
D:\folder\gadfg5\asbsda
D:\folder\546sfdgh\hhdt5463
Anyone can give me advice or idea please. Thank you.
the output of the code you posted is:
D:\Folder\asbsda
D:\Folder\hhdt5463\gadfg5
D:\Folder\asbsda
D:\Folder\hhdt5463\546sfdgh
it is expected because you iterate through both arrays that has 2 elements each and that gives 4 combinations:
gadfg5 & None
gadfg5 & hhdt5463
546sfdgh & None
546sfdgh & hhdt5463
I hope you understand.
To get the desired result try using zip:
Folder = "D:\Folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
for dn, dr in zip(Name, Ver): # get each element of Name and Ver in sequenceif dr is None:Default = "asbsda"Path = os.path.join(Folder, dn, Default)print(Path)else:Path = os.path.join(Folder, dn, dr)print(Path)
Zip combines each element of each of the array to a tuple and return an array of tuples to iterate through, zip([a,b,c], [1,2,3]) -> [(a,1),(b,2),(c,3)]
or enumerate:
Folder = "D:\Folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
for idx, dn in enumerate(Name): # get index and element of Nameif Ver[idx] is None:Default = "asbsda"Path = os.path.join(Folder, dn, Default)print(Path)else:Path = os.path.join(Folder, dn, Ver[idx])print(Path)
enumerate adds a counter to your iterable and gives you each element of the array with its index.
output:
D:\Folder\gadfg5\asbsda
D:\Folder\546sfdgh\hhdt5463