On Fri, Jul 15, 2011 at 12:04 AM, Ellerbee, Edward <eeller...@bbandt.com> wrote:
> Hey Chris,
>
> I was reading over this again, trying to understand the logic (I'm a
> n00b)
>
> Could you explain this a bit? I'd like to build this emit function, but
> I still don't have a firm grasp on functions. All of my code is line by
> line. I'll convert it as a learn more.

I'm responding on-list as I believe others will wish to weigh in (if
only to point out some improvements to my code - it's hastily put
together). Caution, post is long.

Defining functions in Python is, broadly speaking, just a matter of
collecting up a bunch of statements and giving it a name. Compare:

if x>5:
   do_this()
   do_that()
   etc()

with:

def abcde():
   do_this()
   do_that()
   etc()

The first one does the three statements, in order, if and only if the
condition is true. The second one gives a name to those three
statements, so you can use it as a new statement:

abcde()

It'll do the same three things, every time you call it. It's
effectively the same as putting the function's body in where you call
it (that's an extremely sloppy explanation, but near enough).

> So, I'd want this to go after the sort step, and before the format step.
> I can figure that piece out, just trying to get this little block of
> code to work.

Yes, that would be the place to put it.

Here's a reworked version that you can test in IDLE:


def combine(list_of_numbers, position):
 lastprefix=tails=lastsuffix=None
 result=[]
 for cur in list_of_numbers:
  prefix=cur[:position]; tail=cur[position]; suffix=cur[position+1:]
  if prefix!=lastprefix or suffix!=lastsuffix:
   if lastprefix!=None:
    if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
    else: result.append(lastprefix+tails+lastsuffix)
   lastprefix,tails,lastsuffix=prefix,"",suffix
  tails+=tail
 if lastprefix!=None:
  if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
  else: result.append(lastprefix+tails+lastsuffix)
 return result

It incorporates some of the enhancements I mentioned in the original post.

>>> combine(['252205','252206','252208'],5)
['25220[568]']
>>> combine(['252205','252215','252225'],4)
['2522[012]5']

Notice that the 'emit' function is now 'result.append()' - it builds
up a list to return. You can now chain the calls; start with a list of
numbers and then call combine() in a loop.

# List of numbers from your previous post
numbers = ['252205', '252206', '252208', '252220', '252221', '252222',
'252223', '919745', '919725', '919785', '704770', '704771', '704772',
'704773', '704774', '704775', '704776', '704777', '704778', '704779',
'704780', '704781', '704782', '704783', '704784', '704785', '704786',
'704787', '704788', '704789', '704790', '704791', '704792', '704793',
'704794', '704795', '704796', '704797', '704798', '704799']


numbers = combine(numbers,5)

numbers = combine(numbers,4)

numbers = combine(numbers,3)

numbers = combine(numbers,2)

numbers = combine(numbers,1)

numbers = combine(numbers,0)

If you do these statements one at a time in IDLE and inspect the
'numbers' list each time, you'll see the combinations sorting
themselves out. (With this starting list, only the first two will have
any effect.)

The last set of calls can be turned into a for loop:
for pos in range(5,-1,-1):
   numbers = combine(numbers,pos)

In fact, you could actually take it out of being a function, if you wanted to:

list_of_numbers = ['252205', '252206', '252208', '252220', '252221',
'252222', '252223', '919745', '919725', '919785', '704770', '704771',
'704772', '704773', '704774', '704775', '704776', '704777', '704778',
'704779', '704780', '704781', '704782', '704783', '704784', '704785',
'704786', '704787', '704788', '704789', '704790', '704791', '704792',
'704793', '704794', '704795', '704796', '704797', '704798', '704799']

for position in range(5,-1,-1):
 lastprefix=tails=lastsuffix=None
 result=[]
 for cur in list_of_numbers:
  prefix=cur[:position]; tail=cur[position]; suffix=cur[position+1:]
  if prefix!=lastprefix or suffix!=lastsuffix:
   if lastprefix!=None:
    if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
    else: result.append(lastprefix+tails+lastsuffix)
   lastprefix,tails,lastsuffix=prefix,"",suffix
  tails+=tail
 if lastprefix!=None:
  if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
  else: result.append(lastprefix+tails+lastsuffix)
 list_of_numbers = result

That's what the function definition does - it takes a block of code
and gives it a new name. (There's a lot more to it than that, thank
you pedants I know, but in this simple example that's what it's
doing.)

Hope that's of use!

Chris Angelico
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to