FLexible formatted text involving nested lists?

R

RossRGK

I'm having trouble getting my head around a solution for a situation
where I need to flexibly format some text with a varying number of
embedded fields.

Here's a simplified description of my challenge...

I have a list of lists called bigList:

bigList = [ little, small, tiny]

The sub-lists have varying sizes. I won't know how many items they have
but it will be between 0 and 3

So perhaps little = [3, 2, 7]
small = [6,4]
tiny = [2]

The values in those sub lists correspond to formatted print strings. The
formatting strings will change over time and they are in a list called
"fmts" where

fmts = [fmtA, fmtB, fmtC] where

fmtA = 'oats %0d kilos over %0d days with %0d workers'
fmtB = 'barley %0d lbs for %0d hours'
fmtC = 'apples %0d baskets'

If I knew how many fields were in each 'sub-list' in bigList ahead of
time, and it never changed I could awkwardly do this:

print fmtA %(little[0], little[1], little[2])
print fmtB %(small[0], small[1])
print fmtC %(tiny[0])

or equivalently,

print fmts[0] %(bigList[0][0], bigList[0][1], bigList[0][2])
print fmts[1] %(bigList[1][0], bigList[1][1])
print fmts[2] %(bigList[2][0])

Both approaches would yield:
oats 3 kilos over 2 days with 7 workers
barley 6 lbs for 4 hours
apples 2 baskets


Now my challenge: since the number of fields is unknown at design time,
my app needs to add be able to flexibly handle this.

I though maybe I could use a loop that figures things out as it goes
along. e.g...

i=0
for fmtString in fmts
numbOfFields = len(fmt)
print fmtString %(bigList[ need "for 0 to numbOffields" worth of
indices!] )

But I don't know how to have a number of items in the print expression
that align to the numbOfFields value!? Is there some other approach I
can use?

I thought perhaps it would accomodate extra elements in the %(...) part
of the formatted print expression which would be ignored, but that
doesn't work.

Maybe I have to break my fmts up and do a field at a time? Any thoughts
are appreciated :)

-Ross.
 
K

Kerri Reno

Ross,

I'm no expert in python, so excuse me if this is inane.

What I would do is have fmts be a dictionary where
fmts = { 3 = 'oats %0d kilos over %0d days with %0d workers',
2 = 'barley %0d lbs for %0d hours',
1 = 'apples %0d baskets'}

then something like
for x in bigList:
print fmts[len(x)] % x

I didn't test this, but in theory it should work.

Hope this helps,
Kerri

I'm having trouble getting my head around a solution for a situation where I
need to flexibly format some text with a varying number of embedded fields.

Here's a simplified description of my challenge...

I have a list of lists called bigList:

bigList = [ little, small, tiny]

The sub-lists have varying sizes. I won't know how many items they have but
it will be between 0 and 3

So perhaps little = [3, 2, 7]
small = [6,4]
tiny = [2]

The values in those sub lists correspond to formatted print strings. The
formatting strings will change over time and they are in a list called
"fmts" where

fmts = [fmtA, fmtB, fmtC] where

fmtA = 'oats %0d kilos over %0d days with %0d workers'
fmtB = 'barley %0d lbs for %0d hours'
fmtC = 'apples %0d baskets'

If I knew how many fields were in each 'sub-list' in bigList ahead of time,
and it never changed I could awkwardly do this:

print fmtA %(little[0], little[1], little[2])
print fmtB %(small[0], small[1])
print fmtC %(tiny[0])

or equivalently,

print fmts[0] %(bigList[0][0], bigList[0][1], bigList[0][2])
print fmts[1] %(bigList[1][0], bigList[1][1])
print fmts[2] %(bigList[2][0])

Both approaches would yield:
oats 3 kilos over 2 days with 7 workers
barley 6 lbs for 4 hours
apples 2 baskets


Now my challenge: since the number of fields is unknown at design time, my
app needs to add be able to flexibly handle this.

I though maybe I could use a loop that figures things out as it goes along.
e.g...

i=0
for fmtString in fmts
numbOfFields = len(fmt)
print fmtString %(bigList[ need "for 0 to numbOffields" worth of
indices!] )

But I don't know how to have a number of items in the print expression that
align to the numbOfFields value!? Is there some other approach I can use?

I thought perhaps it would accomodate extra elements in the %(...) part of
the formatted print expression which would be ignored, but that doesn't
work.

Maybe I have to break my fmts up and do a field at a time? Any thoughts are
appreciated :)

-Ross.




--
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
(e-mail address removed) (928) 502-4240
..·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.
 
D

davidsands

I'm having trouble getting my head around a solution for a situation
where I need to flexibly format some text with a varying number of
embedded fields.

Here's a simplified description of my challenge...

I have a list of lists called bigList:

bigList = [ little, small, tiny]

The sub-lists have varying sizes.  I won't know how many items they have
but it will be between 0 and 3

So perhaps little = [3, 2, 7]
small = [6,4]
tiny = [2]

The values in those sub lists correspond to formatted print strings. The
formatting strings will change over time and they are in a list called
"fmts" where

fmts = [fmtA, fmtB, fmtC]   where

fmtA = 'oats %0d kilos over %0d days with %0d workers'
fmtB = 'barley %0d lbs for %0d hours'
fmtC = 'apples %0d baskets'

If I knew how many fields were in each 'sub-list' in bigList ahead of
time, and it never changed I could awkwardly do this:

print fmtA %(little[0], little[1], little[2])
print fmtB %(small[0], small[1])
print fmtC %(tiny[0])

or equivalently,

print fmts[0] %(bigList[0][0], bigList[0][1], bigList[0][2])
print fmts[1] %(bigList[1][0], bigList[1][1])
print fmts[2] %(bigList[2][0])

Both approaches would yield:
oats 3 kilos over 2 days with 7 workers
barley 6 lbs for 4 hours
apples 2 baskets

Now my challenge: since the number of fields is unknown at design time,
my app needs to add be able to flexibly handle this.

I though maybe I could use a loop that figures things out as it goes
along. e.g...

i=0
for fmtString in fmts
   numbOfFields = len(fmt)
   print fmtString %(bigList[ need "for 0 to numbOffields" worth of
indices!] )

But I don't know how to have a number of items in the print expression
that align to the numbOfFields value!?  Is there some other approach I
can use?

I thought perhaps it would accomodate extra elements in the %(...) part
of the formatted print expression which would be ignored, but that
doesn't work.

Maybe I have to break my fmts up and do a field at a time?  Any thoughts
are appreciated   :)

-Ross.


The tuple() type-conversion function will do what you need:

print fmts[0] % tuple(bigList[0])
print fmts[1] % tuple(bigList[1])
print fmts[2] % tuple(bigList[2])
 
R

RossRGK

Kerri said:
Ross,

I'm no expert in python, so excuse me if this is inane.

What I would do is have fmts be a dictionary where
fmts = { 3 = 'oats %0d kilos over %0d days with %0d workers',
2 = 'barley %0d lbs for %0d hours',
1 = 'apples %0d baskets'}

then something like
for x in bigList:
print fmts[len(x)] % x

I didn't test this, but in theory it should work.

Hope this helps,
Kerri


Thx for the suggestion - i think that would match the number of fields
to the number of parameters in the specific example but not the general
case. ie fmts[3] could have 3fields this time, but might be 2 another
time or something else.

Plus I don't think print will accept a list 'x' in the %x part of it.
 
R

RossRGK

davidsands said:
The tuple() type-conversion function will do what you need:

print fmts[0] % tuple(bigList[0])
print fmts[1] % tuple(bigList[1])
print fmts[2] % tuple(bigList[2])

I never thought of the tuple type conversion - that looks promising.
Thanks for that!

R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top