Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43752 > unrolled thread

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

Started byaaB <mecagonoisician@gmail.com>
First post2013-04-17 12:25 +0200
Last post2013-04-17 06:43 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: a couple of things I don't understand wrt lists aaB <mecagonoisician@gmail.com> - 2013-04-17 12:25 +0200
    Re: a couple of things I don't understand wrt lists darnold <darnold992000@yahoo.com> - 2013-04-17 06:43 -0700

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

FromaaB <mecagonoisician@gmail.com>
Date2013-04-17 12:25 +0200
SubjectRe: a couple of things I don't understand wrt lists
Message-ID<mailman.719.1366194346.3114.python-list@python.org>
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 this done before posting output from an interactive session.


- iterating a list:
If I understand what you've told me,

bitpattern = [1, 0, 0, 1, 1, 0, 1]
for bit in bitpattern: 
  print bit

and 

bitpattern = [1, 0, 0, 1, 1, 0, 1]
for i in range(len(bitpattern)): 
  print bitpattern[i]

are equivalent, the former being more "python" and the latter being something
like C written using python syntax.


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


- list comprehension:
I wasn't at all aware of this, thanks a lot for pointing it out.
I had an other function that generated a list of cells for the CA, each cell
being randomly alive or dead.
I have rewritten it using list comprehension,

def populate(n):
  random.seed()
  return [random.randint(0,1) for i in range(n)]

which works the same as my previous, C-like, version but feels much better.

I might come back to you all but you've already given me a good push forward, so
I guess I'll be trying to use it as much as I can before asking for an other.

[toc] | [next] | [standalone]


#43758

Fromdarnold <darnold992000@yahoo.com>
Date2013-04-17 06:43 -0700
Message-ID<6c644737-28c2-4437-b584-449c55df262d@i5g2000yqh.googlegroups.com>
In reply to#43752
On Apr 17, 5:25 am, aaB <mecagonoisic...@gmail.com> 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 happening when you incorrectly used bit as an index back into
bitpattern.
When you do that, the behavior actually changes depending on the value
of bitpattern:

a bitpattern that starts with [1, 0, ...] will yield its complement:

>>> bitpattern = [1, 0, 0, 1, 1, 0, 1]
>>> for bit in bitpattern:
	print 'bitpattern[%s] : %s' % (bit, bitpattern[bit])


bitpattern[1] : 0
bitpattern[0] : 1
bitpattern[0] : 1
bitpattern[1] : 0
bitpattern[1] : 0
bitpattern[0] : 1
bitpattern[1] : 0
>>>

while a bitpattern that starts with [0, 1, ...] will yield the
expected results:

>>> bitpattern = [0, 1, 0, 1, 1, 0, 1]
>>> for bit in bitpattern:
	print 'bitpattern[%s] : %s' % (bit, bitpattern[bit])


bitpattern[0] : 0
bitpattern[1] : 1
bitpattern[0] : 0
bitpattern[1] : 1
bitpattern[1] : 1
bitpattern[0] : 0
bitpattern[1] : 1
>>>

HTH,
Don


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web