[Tutor] for loop results into list

Evert Rol evert.rol at gmail.com
Sun Sep 5 22:16:13 CEST 2010

>>>>> 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.

<snip />

>>>>>  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.
> It removes the lowest number in the group of 4. It's intended to replicate rolling four six sided dice, eliminating the lowest number and adding the remaining 3 dice roll results. I didn't mention it cause I felt it wasn't germane to the problem and it might needlessly complicate the situation.

<snip />

>>> 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.
> I've tried this in every place in the code, outside the loop, inside at each step of the loop and none of them get me what I want. Andre's suggestion that I put the empty list before the loop helped but it's still giving me multiple lists.

Can you send what you currently have? Sounds like you still have the first statement at some incorrect place in your code.

More information about the Tutor mailing list