Re: a couple of things I don't understand wrt lists

2013-04-18 Thread Chris Angelico
On Thu, Apr 18, 2013 at 11:01 PM, aaB wrote: > def get_index(thisgen, i): > n = len(thisgen)-1 > cell = thisgen[i] > if i is 0: > print "i==0" > prev, next = thisgen[n], thisgen[i+1] > elif i is n: > print "i==%d" % n > prev, next = thisgen[i-1], thisgen[0] > else: >

Re: a couple of things I don't understand wrt lists

2013-04-18 Thread aaB
> The second guess, more likely, is that you're using "is" to compare > numbers, and that's never a safe idea. It might happen to work for > small numbers, but you should be using ==. The second guess was right, changing "is" for "==" solved it, thanks. I still have a lot to learn about python sem

Re: a couple of things I don't understand wrt lists

2013-04-18 Thread Dave Angel
On 04/18/2013 09:01 AM, aaB wrote: Hello, I am still in the process of writing preliminary code for my CA project. I am now running into a behavior that I can't explain. Here is a script which, at least on my system, shows the issue (python2.7 on a linux system). The final project will be wrapp

Re: a couple of things I don't understand wrt lists

2013-04-18 Thread aaB
Hello, I am still in the process of writing preliminary code for my CA project. I am now running into a behavior that I can't explain. Here is a script which, at least on my system, shows the issue (python2.7 on a linux system). The final project will be wrapping these functions (and others) into

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread Steven D'Aprano
Ned Batchelder wrote: > On 4/17/2013 12:10 PM, 8 Dihedral wrote: >> Well, a new object is returned and can be used. >> Then who is going to clean up the object when required? > > This is a key thing to understand about Python: memory is managed > automatically, no one has to clean up the obj

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread Chris Angelico
On Thu, Apr 18, 2013 at 4:36 AM, Ned Batchelder wrote: > > On 4/17/2013 12:10 PM, 8 Dihedral wrote: >> Well, a new object is returned and can be used. >> Then who is going to clean up the object when required? > > > This is a key thing to understand about Python: memory is managed > automatica

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread Ned Batchelder
On 4/17/2013 12:10 PM, 8 Dihedral wrote: Serhiy Storchaka於 2013年4月17日星期三UTC+8下午5時35分07秒寫道: 17.04.13 07:57, Larry Hudson написав(ла): So using a list comprehension you can do it in two lines: def get_rule(num): bs = bin(num)[2:] return [0] * (8 - len(bs)) + [int(i) for i in bs]

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread 88888 Dihedral
Serhiy Storchaka於 2013年4月17日星期三UTC+8下午5時35分07秒寫道: > 17.04.13 07:57, Larry Hudson написав(ла): > > > So using a list comprehension you can do it in two lines: > > > > > > def get_rule(num): > > > bs = bin(num)[2:] > > > return [0] * (8 - len(bs)) + [int(i) for i in bs] > > > > You

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread darnold
On Apr 17, 5:25 am, aaB wrote: > > - the "complement" thing: > I haven't yet tried to reproduce this, but I will, and I will post back if I > see > this happening again, this time with a real log of python's interactive > console, > or a complete script which people can use. > That was happenin

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread Lele Gaifax
aaB writes: > - copy/paste vs retyping: > Several people have remarked that I had retyped instead of copy/pasting. > This is exactly what happened, the fact is I haven't figured out yet how to > enable copy/pasting from urxvt to vim. I used to use rxvt, but since a while I switched to roxterm fo

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread aaB
Hello, Thanks for all your replies, things are getting clearer. - copy/paste vs retyping: Several people have remarked that I had retyped instead of copy/pasting. This is exactly what happened, the fact is I haven't figured out yet how to enable copy/pasting from urxvt to vim. I'll try to get th

Re: a couple of things I don't understand wrt lists

2013-04-17 Thread Serhiy Storchaka
17.04.13 07:57, Larry Hudson написав(ла): So using a list comprehension you can do it in two lines: def get_rule(num): bs = bin(num)[2:] return [0] * (8 - len(bs)) + [int(i) for i in bs] You can do it in one line! def get_rule(num): return list(map(int, '{:08b}'.format(num)))

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread Larry Hudson
On 04/16/2013 08:37 AM, aaB wrote: hello, I represent the CA's rule with a list of integers, of value 1 or 0. Here is the function I use to generate the list: def get_rule(rulenum): rule = [] while rulenum > 0: rule.append(rulenume % 2) rulenum /= 2 while len(rule) < 8:

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread Ethan Furman
On 04/16/2013 08:37 AM, aaB wrote: rule = getrule(int(8)) just rule = getrule(8) is sufficient -- you don't need to cast the integer 8 to an integer. ;) Thanks, and sorry for the rather long post. Not too long at all: it had lots of detail of what you were trying to do, what you were

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread Dave Angel
On 04/16/2013 11:37 AM, aaB wrote: hello, I am a beginner programmer. I started learning programming about a year and a half ago, using C. I picked up python a few months ago, but only wrote very few scripts. I am currently trying to learn more about the python way of doing things by writing a

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread Terry Jan Reedy
On 4/16/2013 11:37 AM, aaB wrote: I represent the CA's rule with a list of integers, of value 1 or 0. Here is the function I use to generate the list: def get_rule(rulenum): rule = [] while rulenum > 0: rule.append(rulenume % 2) rulenum /= 2 divmod(rulenum) will return both th

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread John Gordon
In aaB writes: > but when I do: > for i in rule: > print rule[i] > I get the "complement": > 1 > 1 > 1 > 1 > 0 > 1 > 1 > 1 When you iterate over a list with this statement: for i in rule: i contains each successive list item. However, your code: print rule[i] acts as though i

Re: a couple of things I don't understand wrt lists

2013-04-16 Thread Chris Angelico
On Wed, Apr 17, 2013 at 1:37 AM, aaB wrote: > but when I do: > > for i in rule: > print rule[i] When you iterate over rule, you don't iterate over the indices, but over the values themselves. Try this: for i in rule: print i Incidentally, "for i in range(rule)" isn't actually going to wor

a couple of things I don't understand wrt lists

2013-04-16 Thread aaB
hello, I am a beginner programmer. I started learning programming about a year and a half ago, using C. I picked up python a few months ago, but only wrote very few scripts. I am currently trying to learn more about the python way of doing things by writing a script that generates png images usin