I am new to Python. I have been stuck on a rather simple problem.
I have a list
r = [2,3,4,5,6,7,8,9,10]
I want to divide each element of the list by every other element
I want the output to be 2/2, 2/3, 2/4, 2/5, 2/6, 2/7, 2/8, 2/9, 2/10, 3/2, 3/3 and so on
I have tried
for i in r:
print(i/i)
But the output I get is
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
What should I do? Thanks in advance!
>Solution :
You could use list comprehension to archive this:
r = [2,3,4,5,6,7,8,9,10]
[print(x / y) for x in r for y in r]
Which is somehow (it is not exactly) the same as:
for x in r:
for y in r:
print(x / y)