Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54630 > unrolled thread

parse list recurisively

Started byandypu@zoho.com
First post2013-09-23 05:53 -0700
Last post2013-09-23 15:10 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  parse list recurisively andypu@zoho.com - 2013-09-23 05:53 -0700
    Re: parse list recurisively Chris Angelico <rosuav@gmail.com> - 2013-09-23 23:00 +1000
      Re: parse list recurisively andypu@zoho.com - 2013-09-23 07:07 -0700
        Re: parse list recurisively andypu@zoho.com - 2013-09-23 07:23 -0700
    Re: parse list recurisively Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-23 15:10 +0100

#54630 — parse list recurisively

Fromandypu@zoho.com
Date2013-09-23 05:53 -0700
Subjectparse list recurisively
Message-ID<9b5d915b-3750-43ab-9691-1b1445332c43@googlegroups.com>
Hello,

i use a load of lists and often i dont know how deep it is, how can i parse that lists elegantly (without a bunch of for loops)

[toc] | [next] | [standalone]


#54632

FromChris Angelico <rosuav@gmail.com>
Date2013-09-23 23:00 +1000
Message-ID<mailman.264.1379941213.18130.python-list@python.org>
In reply to#54630
On Mon, Sep 23, 2013 at 10:53 PM,  <andypu@zoho.com> wrote:
> Hello,
>
> i use a load of lists and often i dont know how deep it is, how can i parse that lists elegantly (without a bunch of for loops)

You can write a function that calls itself - that's what "recursive"
usually means in programming. Start with the tutorial on functions, or
just search the web for 'python recursive function'. Once you have a
bit of code down, you'll be able to ask a more specific question.

Have fun!

ChrisA

[toc] | [prev] | [next] | [standalone]


#54638

Fromandypu@zoho.com
Date2013-09-23 07:07 -0700
Message-ID<9f139899-334b-4885-bc5b-09860f511f82@googlegroups.com>
In reply to#54632
thanks i was not able to figure it out some days before but now i think i can do it myself: a nice function that searches a string in a list.

def parsesb(lis, string):
	print lis 
	for num, nam in enumerate (lis):
		print num, nam
		if type(nam) == list: parsesb(lis[num],string)
		if type(nam) == str: # do something
			if nam == string: print 'hit!!!'

[toc] | [prev] | [next] | [standalone]


#54642

Fromandypu@zoho.com
Date2013-09-23 07:23 -0700
Message-ID<0629f7b7-4416-4225-bf7b-9d53b02e9e8d@googlegroups.com>
In reply to#54638
i have a list and i want to search for a certain string and replace it.

i think i got it now...

sbxe1 = list([['type','ter',[[[['lala']]]]],'name'])
def parsesb(lis, string, replacement):
	for num, nam in enumerate (lis):
		if type(nam) == list: 
			parsesb(lis[num],string,replacement)
		if type(nam) == str: # do something
			if nam == string: lis[num] = replacement
parsesb(sbxe1 ,'name', 'booooogyman')
print sbxe1		

[toc] | [prev] | [next] | [standalone]


#54640

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-09-23 15:10 +0100
Message-ID<mailman.266.1379945457.18130.python-list@python.org>
In reply to#54630
On 23 September 2013 13:53,  <andypu@zoho.com> wrote:
> Hello,
>
> i use a load of lists and often i dont know how deep it is, how can i parse that lists elegantly (without a bunch of for loops)

I don't really understand what you mean. Can you show some code that
illustrates what you're doing?

http://sscce.org/


Oscar

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web