Skip to content Skip to sidebar Skip to footer

How To Process Double Array With Break Using Python?

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

Solution 1:

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 inzip(Name, Ver):
    if dr isNone:
        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)

Post a Comment for "How To Process Double Array With Break Using Python?"