473,704 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FLexible formatted text involving nested lists?

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[i])
print fmtString %(bigList[i][ 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.
Oct 9 '08 #1
4 1129
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

On Thu, Oct 9, 2008 at 2:36 PM, RossRGK <no****@nospam. nowaywrote:
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[i])
print fmtString %(bigList[i][ 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.
--
http://mail.python.org/mailman/listinfo/python-list


--
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
kr***@yumaed.or g (928) 502-4240
..·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.
Oct 9 '08 #2
On Oct 10, 4:36*am, RossRGK <nob...@nospam. nowaywrote:
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[i])
* *print fmtString %(bigList[i][ 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])

Oct 10 '08 #3
Kerri Reno wrote:
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.
Oct 10 '08 #4
davidsands wrote:
>
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.
Oct 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
12699
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will increase. Does Python have a limit on the number of nested for-loops? Thanks.
15
2663
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
2
3102
by: GriffithsJ | last post by:
Hi I have been given some text that needs to be displayed on a web page. The text is pre-formatted (includes things like lists etc) and displays okay if I wrap it using the <pre/> tag. However, the font used is rather "naff" and looks too different to the rest of my web page. I'm not sure how I can (or even whether I can) override the font used with the <pre/> tag. If not, is there another tag I can use to display pre-formatted...
4
8421
by: Fabian | last post by:
I have a three tier nested array, used to define a map for a javascript game, and can be edited within the web page. Is there a way I can generate a visible copy of this array that I can then c&p into a file? I think the best solution would be to write into a popup window (this popup would be purely for map development use, so I don't feel worried by popup blockers, as only myself would be seeing the popup). However, I have no idea how to:...
7
4423
by: Jane Withnolastname | last post by:
I have a number of unordered lists nested within ULs nested within ULs nested within ULs. I have set the style for LI to be .7em. Mozilla gives me the layout as I imagined it, with the lists indented and all at .7em. MSIE, however, shows the indented lists, but the font gets smaller and smaller as the lists nest deeper and deeper. Presumably, it is inheriting the properties of the list it is nested in, thereby giving me 70% of the...
4
7132
by: Lee K. Seitz | last post by:
I'm still relatively new to stylesheets. I'm trying to do something that seemed fairly simple on the surface, but is proving to be a challenge. I have a set of nested lists: <ul> <li>Side One</li> <ul> <li>Pac-Man Fever</li> <li>Froggy's Lament</</li> <li>Ode to a Centipede</</li>
2
10321
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
7
2470
by: pkirk25 | last post by:
My data is in a big file that I have no control over. Sometimes its over 30 MB and often there are several of them. It is machine generated and is nicely formatted. Example text follows: AuctioneerSnapshotDB = { = { = 20, = 1, = {
7
3643
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML verification ability will not like: <ul> <li><a href="#">link</a></li>
0
8766
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8684
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9134
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8975
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.