There is an array of numbers:

cen_x = [770.5, 867.5, 1164.0, 312.0, 1256.0] 

I write this cycle:

 kat_x = [] for i in range(len(cen_x)): for j in range(i + 1, len(cen_x)): k_x = abs(cen_x[i] - cen_x[i + 1]) kat_x.append(k_x) print(kat_x) 

In theory, in kat_x should be 10 different numbers, but this result is obtained:

 [97.0, 97.0, 97.0, 97.0, 296.5, 296.5, 296.5, 852.0, 852.0, 944.0] 

I do not know what's the matter.

  • interestingly called the title of the question :) it seems to me, or is it the same thing? - Flippy
  • Agreed It would be more correct to call "Incorrect cycle operation due to my mistake", but I have no idea what my mistake is. - Alexey Ermolaev
  • to make the question more useful, words need to indicate what you expect your code to do (this can help a better solution to suggest and can help other people with a similar task find a question). No need for broken code as a task specification to use. For example, I first thought that you want [abs(x - y) for x, y in zip(cen_x, cen_x[1:])] to do, but your answer from @Vlad from Moscow does something else. You can update the question (especially the title) and give a clearly expected result - jfs

2 answers 2

Although I do not know Python, but it seems to be

 kat_x = [] for i in range(len(cen_x)): for j in range(i + 1, len(cen_x)): k_x = abs(cen_x[i] - cen_x[j]) ^^^^^^^^^^^^^^^^^^^ kat_x.append(k_x) print(kat_x) 
  • Thank. Problem solved. - Alexey Ermolaev

You have already found the error, and I will add only that the way you are trying to solve the problem is not very Python.

In python, we try not to iterate over elements by index. The for loop allows you to loop through items directly. So it turns out much clearer and reduces the likelihood of errors.

If, in some particular case, along with the element, its index is also needed (for example, as in your case, in order to use it to limit the nested loop), then you can use the enumerate function to get pairs (index, value).

 cen_x = [770.5, 867.5, 1164.0, 312.0, 1256.0] kat_x = [] for i, x1 in enumerate(cen_x): for x2 in cen_x[i+1:]: k_x = abs(x1 - x2) kat_x.append(k_x) print(kat_x) 

So read easier than in your case. In python, the readability of the code is very important, because it is necessary to read the code many times more than to write it.

But, in fact, your problem can be solved even easier.

In cycles you try to find all combinations of two elements. But for this, there is a ready-made function in python (there are a lot of ready-made functions in python in general).

 from itertools import combinations cen_x = [770.5, 867.5, 1164.0, 312.0, 1256.0] pairs = combinations(cen_x, 2) kat_x = [abs(p[0] - p[1]) for p in pairs] print(kat_x) 

I highly recommend to get acquainted with the itertools module and with other existing python libraries - both built-in and third-party.

There are not just a lot of them for python, but VERY much - for all occasions. And often you can not reinvent your own bike, but write in the search engine "python module% key_sword_your task%"