[Tutor] Removing duplicates
Roger Lea Scherer
rls4jc at gmail.com
Wed Aug 1 16:38:03 EDT 2018
I'm trying to get a list of tuples to be a float, a numerator, and a
denominator for all the fractions: halves, thirds, fourths etc up to
ninths. 1/2 returns the same float as 2/4, 3/6, 4/8. I would like to keep
only the 1/2. When I try (line 18) to "pop" from the list I get a "TypeError:
integer argument expected, got float". When I try (line 18) to "remove"
from the list, nothing happens: nothing is removed and I do not receive an
error message.
What do you think is a good way to solve this?
Thank you as always.
import math
fractions = [(0, 0, 0)]
for i in range(1, 10):
for j in range(1, 10):
if i < j:
x = i/j
if x not in fractions:
fractions.append((x, i, j))
sortedFrac = sorted(fractions)
print(sortedFrac)
for i in range(len(sortedFrac)):
try:
if sortedFrac[i][0] == sortedFrac[i-1][0]: # so if the float equals
the previous float
sortedFrac.pop(sortedFrac[i][0]) # remove the second
float
else:
sortedFrac.append(sortedFrac[i][0])
except ValueError:
continue
--
Roger Lea Scherer
623.255.7719
*Strengths:*
Input, Strategic,
Responsibility,
Learner, Ideation
More information about the Tutor
mailing list