list: from 2 to 3 dimensions..looking for a nice way
Mike C. Fletcher
mcfletch at rogers.com
Sat Nov 29 17:08:33 EST 2003
You want to collect those items in a with equal values for item[1], so:
>>> c = {}
>>> for item in a:
... c.setdefault(item[1],[]).append( item )
...
>>> c.values()
[[[1, 'house'], [2, 'house']], [[3, 'garden']]]
HTH,
Mike
sven wrote:
>I've got a nested list->
>a = [[1,'house'],[2,'house'],[3,'garden']]
>
>
>and I want to get one level deeper with the lists having the same value in
>index value 1
>
>b =[[[1, 'house'], [2, 'house']], [[3, 'garten']]]
>
>
>I' achieving this with an ugly syntax:
>
>a = [[1,'house'],[2,'house'],[3,'garden']]
>b = [[]]
>b[0].append(a.pop(0))
>
>for id in range(len(a)):
> if(b[-1][0][1]==a[id][1]):
> b[-1].append(a[id])
> else:
> b.append([a[id]])
>
>What is the pythonic way to do this?
>Thanks for any insight
>
>
--
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list