Skip to content Skip to sidebar Skip to footer

How To Process Break An Array In Python?

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 :

Solution 1:

the output of the code you posted is:

D:\Folder\asbsdaD:\Folder\hhdt5463\gadfg5D:\Folder\asbsdaD:\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 inzip(Name, Ver): # get each element of Name and Ver in sequenceif dr isNone:
        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 inenumerate(Name): # get index and element of Nameif Ver[idx] isNone:
        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\asbsdaD:\Folder\546sfdgh\hhdt5463

Solution 2:

Based on your expected output, it seems you need to iterate over the elements of the two lists in a pairwise fashion and you can use zip() for that:

for dn, dr in zip(Name, 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)

Output:

D:\folder\asbsdaD:\folder\hhdt5463\546sfdgh

Solution 3:

I would recommend to reduce the complexity, by replacing all None values in Ver first:

Folder = "D:/folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']

# Replace all Nones in Ver with default value
Ver = [v if v else'defaultPath'for v in Ver]

allPaths = list(zip(Name, Ver))

for pth in allPaths:
    print(os.path.join(Folder, *pth))

Out:

D:\folder\gadfg5\defaultPathD:\folder\546sfdgh\hhdt5463

Solution 4:

You need a single loop.

We go through the array and see if the element in question is none, if so we replace the directory and make the path.

this way you only have to do a single os.path.join

Folder = "D:/folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
for i inrange(len(Name)):
    if Ver[i] isNone:
        Ver_element= "asbsda"else:
        Ver_element = Ver[i]

    Path = os.path.join(Folder, Name[i], Ver_element)
    print(Path)

Output:

D:/folder/gadfg5/asbsda
D:/folder/546sfdgh/hhdt5463

Solution 5:

You need to zip the two lists so that you cycle through them both simultaneously, as in the following code. You also had some errors in your code which I took the liberty to correct:

import os;
Folder = r"D:\folder"
Name = ['gadfg5', '546sfdgh']
Ver = [None, 'hhdt5463']
for dn,dr inzip(Name,Ver) :
        if dr isNone:
            Default = "asbsda"
            Path = os.path.join(Folder, dn, Default)
            print(Path)
        else:
            Path = os.path.join(Folder, dn, dr)
            print(Path)

The output is:

D:\folder/gadfg5/asbsda
D:\folder/546sfdgh/hhdt5463

Post a Comment for "How To Process Break An Array In Python?"