0

I have this array which has 4 element each. And I want to replace the same element before with empty string.

arr1 = [
    ["Ford", "Fiesta", "Manual", "$ 120,000,000"],
    ["Ford", "Fiesta", "Manual", "$ 134,000,000"],
    ["Ford", "Fiesta", "Automatic", "$ 140,000,000"],
    ["Ford", "Fiesta", "Automatic", "$ 150,000,000"],
    ["Ford", "Focus", "Manual", "$ 330,000,000"],
    ["Ford", "Focus", "Manual", "$ 335,000,000"],
    ["Ford", "Focus", "Manual", "$ 350,000,000"],
    ["Ford", "Focus", "Automatic", "$ 360,000,000"],
    ["VW", "Golf", "Manual", "$ 350,000,000"],
    ["VW", "Golf", "Automatic", "$ 370,000,000"]
];

So the result would be like :

[
    ["Ford", "Fiesta", "Manual", "$ 120,000,000"],
    ["", "", "", "$ 134,000,000"],
    ["", "", "Automatic", "$ 140,000,000"],
    ["", "", "", "$ 150,000,000"],
    ["", "Focus", "Manual", "$ 330,000,000"],
    ["", "", "", "$ 335,000,000"],
    ["", "", "", "$ 350,000,000"],
    ["", "", "Automatic", "$ 360,000,000"],
    ["VW", "Golf", "Manual", "$ 350,000,000"],
    ["", "", "Automatic", "$ 370,000,000"]
];

I have tried to create new array which include all the unique element, and loop it and then append it into 3rd array

arr2 = ["Ford", "Fiesta", "Focus", "Manual", "Automatic", "VW", "Golf"],
arr3 = []
print('-----------------------------------')
for x in arr1 :
  print("X[0] ", x[0])
  print("X[1] ", x[1])
  print("X[2] ", x[2])
  print("X[3] ", x[3])
  if(x[0] == arr1[0][0]):
    arr3.append("")
    if(x[1] == arr1[0][1]):
      arr3.append("")
      if(x[2] == arr1[0][2]):
        arr3.append("")
      if(x[2] != arr1[0][2]):
        arr3.append(x[2])
  arr3.append(x[3])

But my result only correct for the 2nd row :

['', '', '', '$ 120,000,000', 
'', '', '', '$ 134,000,000', 
'', '', 'Automatic', '$ 140,000,000', 
'', '', 'Automatic', '$ 150,000,000', 
'', '$ 330,000,000', '', '$ 335,000,000', 
'', '$ 350,000,000', '', '$ 360,000,000', 
'$ 350,000,000', '$ 370,000,000']

Can someone tell me where my mistake please? Thanks in Advance

1
  • 1
    This is a "vertical" problem, so I suggest you process one column at a time. That way, you can keep the last unique value as you go down the rows, and when it's the same, erase the cell, and when it's different, the value becomes the new unique value. But again, probably easiest to process a single column at a time. Commented Aug 13, 2021 at 13:22

2 Answers 2

1

By creating a new array you can add the first row to it because you know the first row will not be changed. Then you loop the first array to check every row and adding them to the new array.

Something like this should work:

arr2 = []
arr2.append(arr1[0])
for i in range(0,len(arr1)-1):
    tmp = []
    for v in range(len(arr1[i])):
        if arr1[i+1][v] == arr1[i][v]:
            tmp.append("")
        else:
            tmp.append(arr1[i+1][v])
    arr2.append(tmp)

print(arr2)
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, would you mind explaining why you're using range + len? I still dont get it. Thanks.
I'm using range + len so I get the index (i) of the value. That way can do i+1 that will be the row after the current row. The same for v, I check every cell on two rows in that loop.
Its like my method, but you have different ways. I was struggling on comparing the value. Anyway, thanks for the help mate.
0

You can do this with some transpositioning and itetools.groupby:

from itertools import chain, groupby

res = zip(*[chain(*([next(g)] + [""] * len(list(g)) 
                    for _, g in groupby(col))) 
            for col in zip(*arr1)])
for line in res:
    print(line)

('Ford', 'Fiesta', 'Manual', '$ 120,000,000')
('', '', '', '$ 134,000,000')
('', '', 'Automatic', '$ 140,000,000')
('', '', '', '$ 150,000,000')
('', 'Focus', 'Manual', '$ 330,000,000')
('', '', '', '$ 335,000,000')
('', '', '', '$ 350,000,000')
('', '', 'Automatic', '$ 360,000,000')
('VW', 'Golf', 'Manual', '$ 350,000,000')
('', '', 'Automatic', '$ 370,000,000')

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.