Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:12:22+00:00 2026-05-16T06:12:22+00:00

(First, I chose to do this in Python because I never programmed in it

  • 0

(First, I chose to do this in Python because I never programmed in it and it would be good practice.)

Someone asked me to implement a little “combination” program that basically outputs all possible combinations of a set of group of numbers. Example, if you have:
(1,2,3) as the first set,
(4,5,6) as the second, and

(7,8,9) as the third, then one combination would be (1,4,7) and so on, with a total of 27 possible combinations.
This person just wants to do a 6rows x 6cols matrix or a 5rows x 6cols matrix. However, I want to make my little program as flexible as possible.
The next requirement is to only output combinations with X even numbers. If he wants 0 even numbers, then a possible combination would be (1,5,7). You get the idea.
For the permutation part, I used itertools.product(), which works perfectly.
It would be easy if I just assume that the number of numbers in each set (cols) is fixed as 6.
In that case, I could manually create 6 lists and append each combination to the right list.
However and again, I want this to work with N number of cols.

I’m thinking of 2 ways I might be able to do this, but tried with no luck.
So my question is:
How can I create?

li_1 = [] 
li_2 = [] 
...
li_x = [] 

The one way I tried using “lists of lists”:

for combination in itertools.product(*li):
    total_combinations = total_combinations + 1
    #Counts number of even numbers in a single combination
    for x in range(numberInRows):
        if combination[x] % 2 == 0:
            even_counter = even_counter + 1
    print "Even counter:",even_counter
    num_evens[even_counter].append(combination)
    print "Single set:",num_evens
    even_counter = 0

    print combination
print "Num_evens:",num_evens

print '\nTotal combinations:', total_combinations
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:12:23+00:00Added an answer on May 16, 2026 at 6:12 am
    num_evens = {} 
    for combination in itertools.product(*li):
        even_counter = len([ y for y in combination if y & 1 == 0 ])
        num_evens.setdefault(even_counter,[]).append(combination)
    
    import pprint
    pprint.pprint(num_evens)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try to embed a JTextArea object within a JTable cell May 16, 2026 at 1:55 pm
  • Editorial Team
    Editorial Team added an answer In your view controller's .h: @interface MyViewController { UILabel* label;… May 16, 2026 at 1:55 pm
  • Editorial Team
    Editorial Team added an answer <%= render :partial => "shared/subscription", :locals => {:foo => "bar",… May 16, 2026 at 1:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm learning Python as my second programming language (my first real one if you
I am trying to use networkx with Python. When I run this program it
This is my first post on StackOverflow. This community has provided me with some
I need to decode and specifically target the first url only in this array:
I'm going crazy with this my little app... Please help me!!! this is the
I have strings similar to this INSERT INTO `log_action` VALUES (1,'a',1,4),(2,'a',1,1),(3,'a',4,4),(4,'a',1,1),(5,'a',6,4); where I would
I've noticed that I can not use all unicode characters in my python source
How do I force Python's print function to flush the buffered output to the
I am having real trouble installing SUDS in python 2.6.4. I have tried to
I'm very new to Python and just crawling my way through it to accomplish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.