Home > database >  Give every Element in (nested) List a unique number
Give every Element in (nested) List a unique number

Time:01-06

I want to give every number in my list a unique code as they are repeating itself.

data = [
    [0, 1, 2, 1, 1, 2, 3, 1, 2, 0, 2, 1, 4, 3, 2, 4, 4, 4, 0, 0, 2, 2, 2, 2, 5, 5, 3, 4, 0, 0, 4, 2, 5, 2, 3],
    [2, 0, 0, 2, 3, 2, 3, 0, 1, 4, 3, 3, 0, 0, 0, 0, 3, 2, 0, 1, 4, 5, 5, 3, 3, 1, 3, 4, 3, 0, 0, 5, 1, 4, 5],
    [0, 1, 2, 0, 3, 2, 0, 4, 3, 1, 3, 1, 1, 2, 4, 0, 2, 3, 1, 4, 3, 4, 1, 5, 3, 0, 5, 3, 3, 4, 2, 3, 1, 5, 5]
]

so what I was thinking of is that the values should be like this

0 0
1 1
2 2
3 1
4 1
5 2

etc. and when it goes into the next list it should continue with the numberflow that I dont have any doubles... BUT the List should not be flattend as I need the values like this

I tried already to put a enumerate loop but I could not make it work with the different elements in the list

for i in enumerate(data): for j in i

CodePudding user response:

This code might work for you, and it doesn't change the structure of your list.

count = 0
for line in data:
    for number in line:
        print(count, number)
        count  = 1
  • Related