How to process break an array in Python?

2024/9/22 14:28:18

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.

Answer

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
https://en.xdnf.cn/q/119120.html

Related Q&A

Why am I getting replacement index 1 out of range for positional args tuple error

I keep getting this error: Replacement index 1 out of range for positional args tuple on this line of code: print("{1}, {2}, {3}, {4}".format(question[3]), question[4], question[5], question[…

Python: Find keywords in a text file from another text file

Take this invoice.txt for exampleInvoice NumberINV-3337Order Number12345Invoice DateJanuary 25, 2016Due DateJanuary 31, 2016And this is what dict.txt looks like:Invoice DateInvoice NumberDue DateOrder …

How to split a list into chucks of different sizes specified by another list? [duplicate]

This question already has answers here:How to Split or break a Python list into Unequal chunks, with specified chunk sizes(3 answers)Closed 4 years ago.I have an array I am trying to split into chunks …

Python Sum of digits in a string function

My function needs to take in a sentence and return the sum of the numbers inside. Any advice?def sumOfDigits(sentence):sumof=0for x in sentence:if sentence.isdigit(x)== True:sumof+=int(x)return sumof

How to select columns using dynamic select query using window function

I have sample input dataframe as below, but the value (clm starting with m) columns can be n number. customer_id|month_id|m1 |m2 |m3 .......m_n 1001 | 01 |10 |20 1002 | 01 |20…

Downloading Books from website with python

Im downloading books from the website, and almost my code runs smoothly, but when I try to open the pdf Book on my PC. An error generated by Adobe Acrobat Reader that this is not supported file type.He…

Discord.py How can I make a bot delete messages after a specific amount of time

I have a discord bot that sends images to users when they use the !img command, I dont want people to request an image and then have it sit there until someone deletes it. Is there any way I can make i…

How to encode and decode a column in python pandas?

load = pd.DataFrame({A:list(abcdef),B:[4,5,4,5,5,4],C:[7,8,9,4,2,0],D:[1,3,5,4,2,0],E:[5,3,6,9,2,4],F:list(aaabbb)})How to encode and decode column F.Expected Output:Should have two more columns with e…

Pygame module not found [duplicate]

This question already has answers here:Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module?(23 answers)Closed 3 months ago.I have installed p…

Working with Lists and tuples

My data looks like:X=[1,2,3,4]But I need it to look like: Y=[(1,2,3,4)]How does one do this in python?