You are not logged in.
Pages: 1
I just had a dumb python question:
say I have a list L, I want to concatenate a char on the beginning of each item in this list and output it.. doing:
L=string.split("this is some random test list junk")
for i in L:
i = '*'+str(i)
print i
outputs like this:
*['this']
*['is']
*['some']
*['random']
*['test']
*['list']
*['junk']
and that's really not what I want... I want output like:
*this
*is
*some
*random
*test
*list
*junk
any helpful pointers? I'm new to python, so I don't fully grasp the nuances yet (I figured <string> + <list item> would do an implicit string conversion to the correct type)
Offline
your code works fine for me in gnome-terminal,
arch + gentoo + initng + python = enlisy
Offline
your code works fine for me in gnome-terminal,
i should clarify...
stuff=[]
L=string.split("this is some random test list junk")
for i in L:
i = '*'+str(i)
stuff.append(i)
print stuff
is closer to what I'm using... each list element is spit off with list artifacts all over it
Offline
If you want a serious answer to this, you should post to comp.lang.python - there's some smarties over there.
If you want your results as a list, try:
["*"+x for x in "this is a test".split()]
or, join it back together if you want it as a string:
" ".join(["*"+x for x in "this is a test".split()])
If the objects in the list are not guaranteed to be strings, you can use the str(x) inside the list comprehension.
HTH,
farphel
Follow the link below, sign up, and accept one promotional offer. If I can get five suckers (err... friends) to do this, I'll get a free iPod. Then you too can try to get a free iPod. Thanks! http://www.freeiPods.com/?r=11363142
Offline
try this
x = len(stuff)
for i in range(x):
print stuff[i]
so it should look like this
stuff=[]
L=string.split("this is some random test list junk")
for i in L:
i = '*'+str(i)
stuff.append(i)
x = len(stuff)
for i in range(x):
print stuff[i]
arch + gentoo + initng + python = enlisy
Offline
Hi,
I can't help you with your python problem, but I feel like I have to mention how it would be done with one simple ruby one-liner:
ruby -e '"this is some random test list junk".split.each{|word| puts "*#{word}"}'
Offline
w00t!
another ruby zealot. Soon we will conquer the world!
muahahahaha
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
ruby -e '"this is some random test list junk".split.each{|word| puts "*#{word}"}'
pffft
[print "*"+word for word in string.split("this is some random test list junk")]
or
[print "*"+word for word in "this is some random test list junk".split()]
yeah it works in python too, but its not exactly what I'm doing - I used this as an example based on what I'm doing... theres much more code in the real thing
Offline
turns out my problem has to do with usage of the os.path.walk call - and directory formatting plus string.join() calls...
I think I fixed it
Offline
Pages: 1