0

I've build the following code:

def pairs(num_list, n):
    list1 = []
    for num1 in num_list:
        for num2 in num_list:
            if num1 + num2 == n:
                list1.append([num1, num2])
    return(list1)

print(pairs([2,3,4,5],7))

The output is:

[[2, 5], [3, 4], [4, 3], [5, 2]]

BUT I only need NON REPETITIVE PAIRS TO SHOW for ex. [[2, 5], [3, 4]] (doesn't matter which pair)

I figured that I need to go through the numbers more efficiently like so: the first loop goes through 2. second loop compares it to 3,4,5 (without 2) then the first loop goes through 3. second loop goes through 4,5 (without 2 or 3) and so on. How can I actually do this with a code?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    You forgot the input that produces your output ;) – timgeb Nov 03 '18 at 11:50
  • If you create a table with elements of numlist as headers in both the columns and rows, and put the sum of the row/column header in the cell that intersects both.. then you'll find that the sums are symetric across the diagonal (thus you only need to calculate the bottom left, or upper right, triangle). – thebjorn Nov 03 '18 at 11:53
  • @thebjorn what do you mean by bottom left or upper right? i mean i do understand the analogy but how do I create this in the code itself –  Nov 03 '18 at 11:55
  • 1
    @timgeb fixed thankyou –  Nov 03 '18 at 12:00
  • Indent 4 spaces to get code formatting (either manually, or by using the {} icon). – thebjorn Nov 03 '18 at 12:03
  • @thebjorn for some reason I get an error in line: if num_list[i] + num_list[j] == n: IndexError: list index out of range. do you know why? (used the code without yield) –  Nov 03 '18 at 12:20
  • I answered that under your comment on my answer ;-) – thebjorn Nov 03 '18 at 12:25
  • @thebjorn replied again haha sry for the bouncing from there to here. anyway error still occurs –  Nov 03 '18 at 12:29

1 Answers1

2

Here is a way to calculate only the cells in the upper right triangle:

def pairs(num_list, n):
    for i in range(len(num_list)):
        for j in range(i, len(num_list)):
            if num_list[i] + num_list[j] == n:
                yield [num_list[i], num_list[j]]


lst = [2,3,4,5]
print(list(pairs(lst, 7)))

output

[[2, 5], [3, 4]]

without yield

def pairs(num_list, n):
    res = []
    for i in range(len(num_list)):
        for j in range(i, len(num_list)):
            if num_list[i] + num_list[j] == n:
                res.append([num_list[i], num_list[j]])
    return res

print(pairs(lst, 7))

yield lets a function "return multiple times" (lot's of handwaving), it's not necessary.

thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • thank you. can you explain what yield is? tbh I'm not sure if I can use it in my code. is it necessary? –  Nov 03 '18 at 12:03
  • I've updated the code with a non-yield version, check e.g. the first answer to https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do for an explanation of yield. – thebjorn Nov 03 '18 at 12:08
  • for some reason I get an error in line: if num_list[i] + num_list[j] == n: IndexError: list index out of range –  Nov 03 '18 at 12:12
  • with what input? I assume you'll need to examine the two lines above more closesly... the `i` variable goes from `0` to the last index in num_list (all indexes that must be legal), the `j` variable starts from the current value of `i` and ends at the last index in num_list... – thebjorn Nov 03 '18 at 12:19
  • i typed print(pairs([2, 3, 4, 5], 7)) with the exact same code. @thebjorn –  Nov 03 '18 at 12:23
  • You must have done a copy-n-paste error (it's running fine here..) – thebjorn Nov 03 '18 at 12:43
  • cool. If this answer helped you, you should up-vote it, and if it answered your question you should click the check mark under the voting buttons. This will help future visitors, ..and give me points ;-) – thebjorn Nov 03 '18 at 14:53