Skip to content
Advertisement

find indices of duplicate floats in a list

I have an input of a very large list of floating point numbers, a sample is given

[1.2, 2.4, 3.1, 4.0, 5.6, 6.5, 1.2, 3.1, 8.1, 23.6, 29.3]

I want to find all the duplicates and their index i.e. location in the list. The duplicates will only occur as a pair; never more than twice.

The output should be like

1.2  1 7
3.1  3 8

so there are just two entries 1.2 and 3.1 which occur as duplicates, and their positions are 1, 7 and 3, 8 respectively.

any suggestions with python?

Advertisement

Answer

Taking xi_’s answer a bit further. By adding the list comprehension it will provide a list of all indices that contain the value.

x = [1.2, 2.4, 3.1, 4.0, 5.6, 6.5, 1.2, 3.1, 8.1, 23.6, 29.3]
for el in set(x):
    if x.count(el) > 1:
        print el, " ".join([str(index) for index, value in enumerate(x) if value == el])

You will get an output of: (0-based index)

1.2 0 6
3.1 2 7

Edit

Explanation of [str(index) for index, value in enumerate(x) if value == el]

This is enumerating x which creates an enumerate object of the list which will return tuple pairs of (<index>, <value>)

Then it loops through this enumerate object using the for index, value in enumerate(x)

The if value == el checks each value and if it is equal to el then we evaluate, otherwise we do nothing.

The str(index) is the part that gets evaluated based on the condition we defined above. It returns a string version of index which is an integral type.

This will provide a list (all the code between the [ and ]) which will then be passed to the string method join(list) which joins all of the items in the list with the value in the " " (in this case a space, it could be any string.) providing a string of space separated values from the list that was created.

I also assume that you may even want this data later on other than just printing it. Here is a version to do that. This creates an empty dictionary y = {} then we create a new entry with a key of the value (el), providing it a list of the indices.

x = [1.2, 2.4, 3.1, 4.0, 5.6, 6.5, 1.2, 3.1, 8.1, 23.6, 29.3]
y = {}
for el in set(x):
    if x.count(el) > 1:
        y[el] = [str(index) for index, value in enumerate(x) if value == el]

If you do a print y this is what you should get:

{3.1: ['2', '7'], 1.2: ['0', '6']}

Edit2

To print y so that it matches the output you specified. Do something like this:

print "n".join(["{} {}".format(key, " ".join(vals)) for key, vals in y.iteritems()])

output:

3.1 2 7
1.2 0 6

What this is doing is iterating through the dictionary y with: (for key, vals in y.iteritems()) making a string of "<key> <values...>" with: ("{} {}".format(key, " ".join(vals))) This returns a list of strings so we join them using "n" to make them on each line.

Now it is important to note that since dictionary is a hash, that your output order of the keys will not be sorted. If you want to do that, then you could change the code above to this:

print "n".join(["{} {}".format(key, " ".join(y[key])) for key in sorted(y.keys())])

output:

1.2 0 6
3.1 2 7
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement