>>> I'm having a little problem figuring out how to accomplish this simple 
>>> task. I'd like to take a list of 6 numbers and add every permutation of 
>>> those numbers in groups of four. For example for 1, 2, 3, 4, 5, 6 add 1 + 1 
>>> + 1 +1 then 1 + 1 + 1 +2 etc. until reaching 6 + 6 + 6 + 6. Using a for 
>>> loop, that was the easy part, now I'd like to take the results and count 
>>> the number of times each number occurs.
>>> My problem occurs when I try to create a list from the results of the for 
>>> loop, it puts each individual number into its own list. I've looked 
>>> everywhere for the solution to this and can find nothing to help.
>>> 
>>> Any suggestions would be much appreciated
>> If you had some code, that would be very helpful. Now it's a bit of 
>> guesswork what exactly you have (code tends to be clearer than a full 
>> paragraph or two of text).
>> At least, I currently don't understand what your problem is (or what your 
>> for-loop involves).
>> Eg, are you looping and calling a function recursively, do you have four 
>> nested loops (or nested list comprehensions)? Or some other convenient loop 
>> to step through all combinations?
>> 
>> Anway, if you have a recent Python version (2.7 or 3.1), the itertools 
>> module provides a handy utiity: 
>> http://docs.python.org/py3k/library/itertools.html#itertools.combinations_with_replacement
>> Eg,
>> 
>>>>> map(sum, combinations_with_replacement(range(1,7), 4))
>> [4, 5, 6, 7, 8, 9, 6, 7, 8, 9, 10, 8, 9, 10, 11, 10, 11, 12, 12, 13, 14, 7, 
>> 8, 9, 10, 11, 9, 10, 11, 12, 11, 12, 13, 13, 14, 15, 10, 11, 12, 13, 12, 13, 
>> 14, 14, 15, 16, 13, 14, 15, 15, 16, 17, 16, 17, 18, 19, 8, 9, 10, 11, 12, 
>> 10, 11, 12, 13, 12, 13, 14, 14, 15, 16, 11, 12, 13, 14, 13, 14, 15, 15, 16, 
>> 17, 14, 15, 16, 16, 17, 18, 17, 18, 19, 20, 12, 13, 14, 15, 14, 15, 16, 16, 
>> 17, 18, 15, 16, 17, 17, 18, 19, 18, 19, 20, 21, 16, 17, 18, 18, 19, 20, 19, 
>> 20, 21, 22, 20, 21, 22, 23, 24]
>> 
>> seems to do what you want.
>> 
>> But, I'd still say to adopt your own code first, and when you've learned 
>> from that, just use the one-liner above. You're most welcome to ask your 
>> question, best done in combination with code, actual output and expected 
>> output. Then we can point you in the right direction.
>> 
>> Cheers,
>> 
>>   Evert
>> 
> Thanks Evert, here is the code.
> 
> 
> fourdsix = [1, 2, 3, 4, 5, 6]
> for i in fourdsix:
>    for j in fourdsix:
>        for k in fourdsix:
>            for l in fourdsix:
>                fourdsix_result = [i, j, k, l]
>                attribs = sum(fourdsix_result) - min(fourdsix_result)
>                print attribs
> 
> This gives me the proper results,

I'm not sure I understand that, because now you have a subtraction here; not 
sure what that does.

> now it's just a matter of getting it into a list so I can further work with 
> the data.
> I've tried the following
> attrib_list = [attribs]
> 
> and
> attrib_list = []
> attrib_list.append(attribs)
> print attrib_list

This one should work, unless you've put it incorrectly inside the code. But 
otherwise, it will create the list you're looking for.
So then it's a matter of stepping through the numbers inside the list and 
counting their occurrences.
In fact, that could be done straight inside the quadruple nested for loops. One 
way I can think of, is using a dictionary, where the sum of the four-element 
list is the key, and the value increases by one each time. Eg:

try:
   counter[attribs] += 1
except KeyError:
   counter[attribs] = 1

or with dict.setdefault:

counter2[attribs] = counter2.setdefault(attribs, 0) + 1


(oh, and sorry: I missed the part where you wanted to count the number of times 
the sum appears in your initial email.)

> but these both only create a list of the last number.
> 
> I'm sure there is a pre-existing module that will do all of this for me but 
> I'm trying to learn by doing.

Definitely. 
It's often fun to be surprised later by the amount of utilities available in 
the Python standard library, that would have solved a problem in a one-liner 
(also a bit frustrating, but not too much).

Cheers,

  Evert

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to