Syafiqur_
Syafiqur_

Reputation: 587

Replacing string in multidimensional Array

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

Upvotes: 0

Views: 65

Answers (2)

Sefan
Sefan

Reputation: 709

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)

Upvotes: 1

user2390182
user2390182

Reputation: 73450

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')

Upvotes: 0

Related Questions