On Sun, Sep 5, 2010 at 8:51 PM, Micheal Beatty <mike...@gmail.com> wrote: > On 09/05/2010 01:26 PM, Evert Rol wrote: >>> >>> Hello all, >>> >>> 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, 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 > > but these both only create a list of the last number.
Put the attrib_list = [] before the beginning of the outer loop, and it should work as intended. Now you are creating a new list each time, which is not what you want. -- André Engels, andreeng...@gmail.com _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor