0

I would like to process a multidimensional array.

Here is my array:

DefaultName = ["AB", "BC", "CD"]
DefaultCode = ["1D", "2D", "3D"]

Name = ["AB", "BC", "CD"]
Code = ["11","12", "13"]

from those arrays above, each DefaultName value belongs to DefaultCode value. For example,

DefaulCode "1D" belongs to DefaultName "AB" and so on.

For Array Name and Code as well. Each value of Code belongs to Name.

Code "11" belongs to Name "AB" and so on.

My problem is when the length of Code is different with length of Name. This is the case:

Name = ["AB", "BC", "CD"]
Code = [None ,"12", "13"]

The value of AB is none. So I need to looking for the Code for the Name that empty from DefaultCode by mapping the Name.

In that case, my expectation, AB will has Code from DefaultCode which is 1D because 1D DefaultName is the same as Name of the code.

I tried this, but I am stuck in the part to mapping which Name that empty code and how to looking for DefaultCode.

if (len(Name)) == (len(Code)):
    print("There is no empty code")
else:
    print("There is empty code")
    # looking for the Name that does not has code

    # after know which name that without code, then looking for the code from Default Code by mapping the Name

Anyone can help me please. I really appreciated for the help. Thank you so much. Gbu

2 Answers 2

2

You can do a dict lookup if the code is empty.

Ex:

DefaultName = ["AB", "BC", "CD"]
DefaultCode = ["1D", "2D", "3D"]

DefaultCode = dict(zip(DefaultName, DefaultCode)) # --> {'AB': '1D', 'BC': '2D', 'CD': '3D'}

Name = ["AB", "BC", "CD"]
Code = [" ","12", "13"]

res = {n: c if c.strip() else DefaultCode.get(n, "N/A") for n, c in zip(Name, Code)}
print(res)

Output:

{'AB': '1D', 'BC': '12', 'CD': '13'}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much! Your output is all the list of array Name and Code. But my expectation, I only need to output the Name and Code that empty. Could you please help me then. Thank you
I only need to output the empty code which is this 'AB': '1D'
Then use res = {n: DefaultCode.get(n, "N/A") for n, c in zip(Name, Code) if not c.strip()}
sorry. I edit my question. The case that I have like this Name = ["AB", "BC", "CD"] Code = [None ,"12", "13"] and it does not work :( any suggestion please
use if c is None
|
1

The answer above is pretty much correct, but there is another simpler way to do it. You can Zip the two lists into one list of tuples Ex:

Name = ["AB", "BC", "CD"]
Code = ["11","12", "13"]

NewList = zip(Name, Code)

#iterate through list to demonstrate outputs
for i in NewList:
   print(i)

The Output of this code would be:

('AB', '11')
('BC', '12')
('CD', '13')

Where each element in the list is a tuple. The only difference from a list is that you can't make changes to the values in it hence why they are immutable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.