0

I am working on a cluster algorithm, and when after it cluster the data, it return the list which contain single document (class doc) and group (class group) of documents, for example:

Group(Document(id='NSVcteD-5', name=u'1332410487000-2ed0728e9015028e7c41341011a1bd82'), Group(Document(id='NSVcteD-11', name=u'1332410485000-18ae371b18b3790874fb886085c770af'), Group(Document(id='NSVcteD-12', name=u'1332410484000-dc544efc146674289b126062b000a302'), Group(Document(id='NSVcteD-6', name=u'1332410487000-25e815a47779642df2a416495bd5174c'), Group(Document(id='NSVcteD-7', name=u'1332410485000-eb66881f5b1c633dd1609ad6fc18a45c'), Group(Document(id='NSVcteD-2', name=u'1332410487000-a39e2076ca4477e8a324081732bd36c0'), Group(Document(id='NSVcteD-9', name=u'1332410485000-db1acc63d72a63f65623610242394877'), Group(Group(Document(id='NSVcteD-13', name=u'1332410152000-13ea7da3c74917b86bb70e59ff356397'), Document(id='NSVcteD-3', name=u'1332410487000-6287c3d86e6416cb421b6f176a367e23')), Group(Document(id='NSVcteD-10', name=u'1332410485000-508937f6a4cae9ed79dbd54f016ca61c'), Group(Document(id='NSVcteD-4', name=u'1332410487000-4b16fa5633a9df1341690d9a32a4f06d'), Group(Document(id='NSVcteD-1', name=u'1332410487000-b6696b10ad4415c87e41e5367fd4bcfa'), Group(Document(id='NSVcteD-8', name=u'1332410485000-e3f77be9cddcb9efc07914654454d817'), Group(Document(id='NSVcteD-14', name=u'1332410151000-cc13783d0980106d686d64082121f6ac'), Document(id='NSVcteD-15', name=u'1332410151000-a91330e828e41ed3b8503f3133f61fc7'))))))))))))))

To make it's easy to understand, just bring a real obj, generated by my script. It is multi-tered list, and I have no idea how to iterate to manipulate with it, such as convert to the json style string.

Any help is much appreciated.

mrblue
  • 807
  • 2
  • 12
  • 24
  • 3
    If your language is specifically python, why not just include some clean and clear python code examples of your data instead of this generic hard to read text example? – jdi Mar 23 '12 at 17:34
  • Thank you for your comment, I just included an object, with generated by my company framework.Regards – mrblue Mar 23 '12 at 17:44

1 Answers1

1

You mean something like that? Note that this is totally inefficient and will break after a certain depth..

>>> def recursive_iterate(iterable):
...     iterated_object=[]
...     for elem in iterable:
...         if hasattr(elem,"__iter__"):
...             iterated_object.append(recursive_iterate(elem))
...         else:
...             iterated_object.append(elem)
...     return iterated_object
... 
>>> recursive_iterate([1,2,3,[4,5,6]])
[1, 2, 3, [4, 5, 6]]
>>> recursive_iterate([1,2,3,xrange(10)])
[1, 2, 3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> recursive_iterate([1,2,3,[4,5,6,[xrange(10)]]])
[1, 2, 3, [4, 5, 6, [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]]
luke14free
  • 2,529
  • 1
  • 17
  • 25
  • hi luke, in general, this is what I am looking for. but can you provide me some more tips on performance of this function? or any other way around to iterate recursively? thanks so much – mrblue Mar 24 '12 at 02:09
  • Well, recursive iteration is usually bad because it increases disproportionately the algorithm complexity of your code. – luke14free Mar 24 '12 at 10:15
  • You could try to remove Tail Recursion, but this is only a patch to the above code and wont help you that much. Perhaps you should write your code in c/c++/cython. Another option is to transform your nested objects into json as described here http://stackoverflow.com/questions/2343535/easiest-way-to-serialize-a-simple-class-object-with-simplejson – luke14free Mar 24 '12 at 10:26