Home > Back-end >  Find the index of the repeated elements
Find the index of the repeated elements

Time:12-15

Assume I have a list containing 905 elements as:

roughness = [140.0, 120.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 120.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 120.0, 140.0, 120.0, 140.0, 120.0, 120.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 120.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 120.0, 120.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0, 140.0]

I want to find the all indices of the element of '120' in the list shown above. I tried to do it using the code:

all_index = []
for i in roughness:
    if i == 120:
        index = roughness.index(i)
        all_index.append(index)
print(all_index)

but it only returned the index of first '120',like [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Could you please give me some ideas about how to find the index of each element of '120' in the roughness list? Thank you very much!

CodePudding user response:

Use enumerate function:

out = [i for i,x in enumerate(roughness) if x==120]

You can also use numpy.where if dependency is not an issue:

out = np.where(np.array(roughness)==120)

Output:

    [    1,   3,   8,  34,  35,  36,  39,  41,  42,  43,  44,  45,  46,
        47,  49,  50,  51,  64,  71,  72,  73,  74,  75,  77,  85, 144,
       145, 146, 147, 148, 149, 150, 151, 154, 156, 158, 159, 161, 178,
       179, 180, 185, 194, 206, 215, 228, 229, 235, 339, 340, 341, 342,
       343, 344, 352, 353, 354, 355, 356, 357, 358, 371, 372, 373, 384,
       385, 388, 389, 392, 393, 398, 399, 400, 403, 404, 405, 406, 531,
       532, 533, 546, 547, 672, 673, 674, 675, 676, 677, 678, 679, 736,
       737, 787, 788, 789, 798, 799, 800, 801, 802, 803, 804, 811, 812,
       813, 814, 815, 816, 817, 818, 819, 820, 833, 834, 835, 836, 837,
       864, 865]

CodePudding user response:

That's how you do it:

indices = []
for i in range(len(roughness)):
   if roughness[i] == 120:
      indices.append(i)

print(indices)

Gives output like this:

[1, 3, 8, 34, 35, 36, 39, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 64, 71, 72, 73, 74, 75, 77, 85, 144, 145, 146, 147, 148, 149, 150, 151, 154, 156, 158, 
159, 161, 178, 179, 180, 185, 194, 206, 215, 228, 229, 235, 339, 340, 341, 342, 343, 344, 352, 353, 354, 355, 356, 357, 358, 371, 372, 373, 384, 385, 388, 389, 392, 393, 398, 399, 400, 403, 404, 405, 406, 531, 532, 533, 546, 547, 672, 673, 674, 675, 676, 677, 678, 679, 736, 737, 787, 788, 789, 798, 799, 800, 801, 802, 803, 804, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 833, 834, 835, 836, 837, 864, 865]

CodePudding user response:

There's probably an easy way to do this using for i in roughness, but I'd just do it like this:

all_index = []

for i in range(len(roughness)):
  if roughness[i] == 120
    all_index.append(i)

print(all_index)

  • Related