crazy pairs list request

This is the place for queries that don't fit in any of the other categories.

crazy pairs list request

Postby dogdaynoon » Fri May 23, 2014 5:41 am

Hopefully this makes sense:
I have a list that consists of the following: NUMS = [1, 2, 3, 4, 5]
i want to cycle through that list and return the following pairs:

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

how the heck to do that?

i am pretty sure it is a for loop with a bunch of predefined vars... but i am fairly new to programming so heres what i got so far.
Code: Select all
PAIRS_LIST = []
a = 0
b = 1
c = 2
d = 3
e = 4
for each in NUMS:
    PAIRS_LIST.append(NUMS[a], NUMS[b])
        if NUMS[b] > len(NUMS):
           probably better do something here...


see. I am new.
Thanks in advance!

james(dogdaynoon)
dogdaynoon
 
Posts: 12
Joined: Thu May 23, 2013 11:56 pm

Re: crazy pairs list request

Postby Mekire » Fri May 23, 2014 8:49 am

itertools is a nice thing to learn about.

Code: Select all
import itertools

NUMS = [1, 2, 3, 4, 5]
print(list(itertools.combinations(NUMS, 2)))
Result
Code: Select all
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: crazy pairs list request

Postby dogdaynoon » Fri May 23, 2014 9:17 pm

You are a genius!
Thank you, that is what i was looking for!
dogdaynoon
 
Posts: 12
Joined: Thu May 23, 2013 11:56 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests