n00bie wants advice.
oj
ojeeves at gmail.com
Wed Jul 2 08:55:34 EDT 2008
On Jul 2, 7:25 am, bsag... at gmail.com wrote:
> This simple script writes html color codes that can be viewed in a
> browser. I used short form hex codes (fff or 000, etc) and my list
> has only six hex numbers otherwise the results get rather large. I
> invite criticism as to whether my code is "pythonic". Are there other
> ways to generate the hex combos besides the nested "for" loops? Thanks
> in advance, Bill
>
> list = ['3','6','9','b','d','f']
>
> s = '<html><head><style>h1{margin:0}</style></head><body>\n'
>
> for a in list:
> for b in list:
> for c in list:
> s += '<h1 style="background:#'+ a + b + c +'">'+ a + b + c +'</h1>
> \n'
>
> s += '</body></html>'
>
> f = open('c:/x/test.htm', 'w')
> f.write(s)
> f.close()
You could write the loop like this:
for red, green, blue in [(r, g, b) for r in list for g in list for b
in list]:
s += blah blah blah
but, arguably, that isn't easier to read or understand. It's a matter
of taste, I guess.
As has already been mentioned, list is not a good name, because it is
already used.
Also, personally, I find it easier to read strings that aren't
constructed with concatenation, but using pythons string formatting
gubbins:
'<h1 style="background: #%s%s%s">' % (red, green, blue)
Again, I think this is mostly personal preference.
More information about the Python-list
mailing list