I would like to process an array. Here is my previous question. How to process break an array in Python?
But I have another case and I refer to that answer, but I still can get my expectation result. Here is the case
Case 1Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
Default = {'gadfg5': '6754435'}
Case 2
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'None']
Default = {'gadfg5': '6754435', '546sfdgh': '98769786'}
Case 3
Folder = "D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [g675436g, 'hhdt5463']
Default = {}
This is what I've tried:
for dn, dr, key in zip(Name, Ver, Default):
if dr is None:
Path = os.path.join(Folder, dn, Default[key])
print("None",Path)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)
The output of CASE 3
is empty
. But my expectation the output supposed to be:
D:\folder\gadfg5\g675436g
D:\folder\546sfdgh\hhdt5463
The output of CASE 2
is correct as my expectation which is:
D:\folder\gfg\6754435
D:\folder\546sfdgh\98769786
The output of CASE 1
only returns one path, like this:
D:\folder\gadfg5\6754435
But my expectation the output supposed to be like this:
D:\folder\gadfg5\6754435
D:\folder\546sfdgh\hhdt5463
Anyone can give me an idea, please. Thanks
what happens in case one is Name
and Ver
has two elements while dictionary Default
has just one so when you zip them the output will have just one tuple: ('gadfg5', None, 'gadfg5')
Since the Default
is a dictionary and its keys are the elements of Names
we don't have to zip the three of them, instead try:
for dn, dr in zip(Name, Ver):
if dr is None:
Path = os.path.join(Folder, dn, Default[dn])
print("None", Path)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)
The same logic applies to case 3
But there is an issue here let me demonstrate with another scenario, case 4:
Input:
Folder = "D:\Folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
Default = {}
Here dict Default
is empty and Ver
has a None element. This will throw a Key error at Default[dn]
. So lets put a check for that too as follows:
for dn, dr in zip(Name, Ver):
if dr is None:
if dn in Default: # check if Default contains the key dn
Path = os.path.join(Folder, dn, Default[dn])
print("None", Path)
else:
print('no default path for ', dn)
else:
Path = os.path.join(Folder, dn, dr)
print("Not None", Path)