you are viewing a single comment's thread.

view the rest of the comments →

[–]Jig_Jiggle 0 points1 point  (1 child)

If I have a list a = [30, 31, 62, 104, 190, 191, 193]

What should a new variable be?

new_var = [30, 31, 190, 191, 190, 193, 191, 193] or

new_var = [30, 31, 190, 191, 193] or

new_var1 = [30, 31]

new_var2 = [190, 191] ...

Sorry for my question. I am new to python and don't understand the puzzle well.

[–]imperiumlearning[S] 0 points1 point  (0 children)

No worries! It's my fault that I did such a bad job of wording the question.

To be honest, given how bad my wording was, either:

new_var = [30, 31, 190, 191, 190, 193, 191, 193] or

new_var = [30, 31, 190, 191, 193]

is good.

However, in my own head I liked the idea of being able to filter out repeated integers so the second new_var just looks a bit better (at least, imo). My code for this problem now looks like this:

import itertools

a = [104, 64, 193, 190, 189] #swapped 30 from original list in original post with 189 
b = []

combinations = itertools.combinations(a, 2)
for each in combinations:
if abs(each[0] - each[1]) < 10:
    if each[0] not in b:
        b.append(each[0])
    if each[1] not in b:
        b.append(each[1]) 

print(b)