You are not logged in.

#1 2004-11-03 15:20:11

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

python anyone?

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

#2 2004-11-03 15:51:33

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python anyone?

your code works fine for me in gnome-terminal,


arch + gentoo + initng + python = enlisy

Offline

#3 2004-11-03 16:29:19

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python anyone?

xerxes2 wrote:

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

#4 2004-11-03 16:50:20

farphel
Forum Fellow
From: New Hampshire - USA
Registered: 2003-09-18
Posts: 250
Website

Re: python anyone?

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

#5 2004-11-03 17:06:51

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python anyone?

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

#6 2004-11-03 17:46:25

jochen
Member
From: Germany
Registered: 2004-06-01
Posts: 102

Re: python anyone?

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}"}'

wink

Offline

#7 2004-11-03 18:03:44

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: python anyone?

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

#8 2004-11-03 19:26:45

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python anyone?

jochen wrote:
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

#9 2004-11-03 19:38:21

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python anyone?

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

Board footer

Powered by FluxBB