0

I try to find positions in list - data[i][2] if the string equal with the others string values, but if there are more than two of the same values, I do not want duplicates. And then I want to sort list. I need to keep the position of the first wanted for sort.

Thanks for help.

INPUT:

data = [["something1", 1, "number one", "inf 1",1, 33,22, "other"],
        ["something2",2, "number twenty", "inf 2", 1,66, 11, "other"],
        ["something3",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something4",4, "number five", "inf 4", 1, 1212, 9988, "other"],
        ["something5",3, "number four", "inf 3", 1,99, 55, "other"],
        ["something6",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something7",3, "number twenty", "inf 3", 1,99, 55, "other"]]

listMatch = []
for i in range(len(data)):

    for j in range(i+1, len(data)-1):

        if data[i][2] in data[j+1][2]:
            if len(listMatch) == 0:
                listMatch.append([i, j+1])
            if len(listMatch) > 0:
                if ([i, j+1]) not in listMatch:
                    listMatch.append([i, j+1])


            #print(data[i][2],(i, "Position of search value"), (j+1, "Position of found value"))
print(listMatch)

OUTPUT:

[[0, 2], [0, 5], [1, 6], [2, 5]]

GOAL 1:

[[0, 2], [0, 5], [1, 6]]

GOAL 2:

data = [["something1", 1, "number one", "inf 1",1, 33,22, "other"],
        ["something3",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something6",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something2",2, "number twenty", "inf 2", 1,66, 11, "other"],
        ["something7",3, "number twenty", "inf 3", 1,99, 55, "other"],        
        ["something4",4, "number five", "inf 4", 1, 1212, 9988, "other"],
        ["something5",3, "number four", "inf 3", 1,99, 55, "other"]]


Pr.Syn
  • 99
  • 1
  • 7

1 Answers1

1

hence the answer (shortest i can come up with):

data = [["something1", 1, "number one", "inf 1",1, 33,22, "other"],
        ["something2",2, "number two", "inf 2", 1,66, 11, "other"],
        ["something3",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something4",4, "number three", "inf 4", 1, 1212, 9988, "other"],
        ["something5",3, "number four", "inf 3", 1,99, 55, "other"],
        ["something6",3, "number one", "inf 3", 1,99, 55, "other"],
        ["something7",3, "number two", "inf 3", 1,99, 55, "other"]]

data = sorted(data, key=lambda element: element[2])

for i in data:
    print(i)

This does not fulfil the first goal but you would have to explain what you want better first.

Jakub Dóka
  • 2,477
  • 1
  • 7
  • 12
  • Thanks for hint. But I do not know names (numbers) of data[i][2] there can be any string... – Pr.Syn Jun 29 '21 at 13:52
  • @Pr.Syn I changed this to sort strings in general, you really confused me with that example. – Jakub Dóka Jun 29 '21 at 14:02
  • thanks, I found (sorted(data, key=itemgetter(2))) - that is the same result. But I need to keep the position of the first wanted for sort - like in GOAL 2. – Pr.Syn Jun 29 '21 at 14:07