[Tutor] for loop results into list

Evert Rol evert.rol at gmail.com
Sun Sep 5 22:48:35 CEST 2010

```On 5 Sep 2010, at 22:31 , Micheal Beatty wrote:

> On 09/05/2010 03:16 PM, Evert Rol wrote:
>>>>>>> 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.
>>
>>
> Here is what I have.
>
> # Program to determine frequency of each possible 4d6 die roll
>
> attrib_list = []
> fourdsix = [1, 2, 3, 4, 5, 6] #list of possible 1d6 die roll  results
> for i in fourdsix:
>    for j in fourdsix:
>        for k in fourdsix:
>            for l in fourdsix:
>                fourdsix_result = [i, j, k, l] # creating list from loop results
>                attribs = sum(fourdsix_result) - min(fourdsix_result)
>                attrib_list.append(attribs)
>                print attrib_list
>
>
> This gets me multiple lists running through the loop.

Have you tried printing attrib_list at the very end of your code, *outside* all the nested loops?

I also noticed a mistake in a previous answer:

>>> map(sum, combinations_with_replacement(range(1,7), 4))

doesn't really work, because it produces eg [1, 1, 1, 2], but skips [2, 1, 1, 1], [1, 2, 1, 1] & [1, 1, 2, 1].

itertools.product does produce all of these combinations (and the docs even say "Equivalent to nested for-loops in a generator expression." for product()).
Of course, when you want to throw away the lowest number, it becomes something like:

>>> map(lambda x: sum(x)-min(x), itertools.product(range(1,7), repeat=4))
(list with 1296 elements)

Just for future reference.

```