0

Me and a friend decided that our applescript version of a script was too slow, and ported it to python.

The script is located here:

https://gist.github.com/anonymous/aba4b07237eb2ccec30b

There's a small problem, too many for-loops, and it's not dynamic. Because it's not dynamic the python program reports too many nested for-loops. How can me and my friend avoid this error and possibly enter any number to get the output from the list collections?

I know this can be done somehow with a function, but we're new to python and scripting languages and just trying to push the limits to see what can be done, and learn a little in the process.

ANONYMOUS
  • 15
  • 6
  • 1
    What is this script trying to do? Are you trying to print all the values of `collections` to STDOUT? Also, that is hilarious telling the user to be "very patient" and then just sleep for 5 seconds. – MrAlias Jul 13 '14 at 05:40
  • 1
    The program is designed to create a long list of every possible combination in the input list for a 40 digit string, check the for loop: for x40 in collections, that'll show where it puts the current result to a temporary file. – ANONYMOUS Jul 13 '14 at 06:04
  • If you're looking for permutations of a list [this](http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python) would likely be where you should have looked. – MrAlias Jul 13 '14 at 06:06
  • The reason for saying to be patient is that, for even 5 digits it can take an hour to process that list on the hardware i have available to me for this. – ANONYMOUS Jul 13 '14 at 06:07
  • It sounds like you have a lot to do, you might not want to wait around with `time.sleep(5)` :) – MrAlias Jul 13 '14 at 06:09
  • If you're actually thinking that you can exhaustively iterate over every possible permutation of 40 items ... you're in for a bit of a shock. 40! (factorial) is 815915283247897734345611269596115894272000000000 ... permutations. That's 8 * 10 to the 48th power which is about thirty orders of magnitude larger than the age of the universe in seconds. So a successful operation every nanosecond would still leave you with a wait that's about 20 orders of magnitude longer than the Universe has been around. – Jim Dennis Jul 13 '14 at 06:25
  • I had that in there for debugging it originally, -- and the tuple returned from itertools.permutations is not exactly easy to parse? Nor does it give all of the possibilities, it starts off the list a little odd. – ANONYMOUS Jul 13 '14 at 06:25
  • Jim Dennis, I'd still like to try to accomplish something with this. Sure, may be impossible, but doesn't mean me and my friend can't learn how to efficiently create a list like this. Whether it be 40 digits or just 9; 40 is simply an example. Not a necessity. – ANONYMOUS Jul 13 '14 at 06:27
  • First I think you'll want to read up on the precise meanings of the terms "combination" and "permutation" so that you can more precisely describe to others what you're hoping to accomplish. I'm not sure that you really mean by "long list of every possible combination" which could mean combination in the technical sense (where there are n**m combinations of n "digits" long selected from m possible "digits') or every possible arrangement (which would be n!). Try this: https://en.wikipedia.org/wiki/Combination ... and this: https://en.wikipedia.org/wiki/Permutations – Jim Dennis Jul 13 '14 at 06:35
  • Permutations, sorry... – ANONYMOUS Jul 13 '14 at 06:39

3 Answers3

1

Not very sure what you are trying to do there, but my guess is you have misunderstood "for in" . You can read more about it here, Python for loop

My guess is you are trying to create a string of all elements in the list. You can instead use

myFileData = "\n" + "".join(collections)

myFile = open(myFilePath, "a")
myFile.write(myfileData)
myFile.close()

print(myFileData)

What that would do is create a string by adding "" between each item in the list. If you want the items in the list space separated, then you change, "" with " ".

myFileData = " ".join(collections)
  • not exactly what i'm trying to accomplish. i commented on my post, that should explain it a bit – ANONYMOUS Jul 13 '14 at 06:06
  • Thanks for the clarification. For obtaining permutations MrAlias's suggestion sounds appropriate. Without a library module, I could have used recursion to solve an issue like yours. But then itertools.permutation would definitely cover more cases and libraries are always better. – PankajPundir Jul 13 '14 at 17:43
0

That's not how you iterate over a list.

import string
collections = list(string.ascii_letters)
collections.append([digit for digit in string.digits])
# much easier than typing it out by hand
for char in collections:
    print char

This populates the list, then iterates through it and prints each item. You'll need to figure out exactly what you want your program to do (I couldn't).

MattDMo
  • 100,794
  • 21
  • 241
  • 231
0

I think you want the cartesian product of collections:

import itertools
collections = ['a','A','b','B' ]
for (x1,x2,x3) in itertools.product( collections, repeat= 3 ) :
    print x1,x2,x3

Out:

'a', 'a', 'a'
'a', 'a', 'A'
'a', 'a', 'b'
'a', 'a', 'B'
'a', 'A', 'a'
'a', 'A', 'A'
...

Or if you have lots of variables, use the tuple directly:

for t in itertools.product( collections, repeat= 3 ) :
    print t

Out:

('a', 'a', 'a')
('a', 'a', 'A')
('a', 'a', 'b')
('a', 'a', 'B')
('a', 'A', 'a')
('a', 'A', 'A')
...
usual me
  • 8,338
  • 10
  • 52
  • 95
  • Well, this would've worked, except: for (x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,x35,x36,x37,x38,x39,x40) in itertools.product(collections,repeat=40): ValueError: too many values to unpack – ANONYMOUS Jul 13 '14 at 06:05
  • Why not simply use the `tuple` returned from `itertools.product` and access it like you would with a list? `x1` would be `t[0]`, `x2` would be `t[1]`, etc – usual me Jul 13 '14 at 06:15