list: from 2 to 3 dimensions..looking for a nice way
From: sven (salvadorindali_at_gmx.de)
Date: 11/29/03
- Next message: John Benson: "Re: ActiveState Python won't call module function"
- Previous message: Emile van Sebille: "Re: sorting dictionary keys?"
- Next in thread: Peter Otten: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Reply: Peter Otten: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Reply: Mike C. Fletcher: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Maybe reply: Robert Brewer: "RE: list: from 2 to 3 dimensions..looking for a nice way"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 29 Nov 2003 21:51:51 +0000 (UTC)
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
- Next message: John Benson: "Re: ActiveState Python won't call module function"
- Previous message: Emile van Sebille: "Re: sorting dictionary keys?"
- Next in thread: Peter Otten: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Reply: Peter Otten: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Reply: Mike C. Fletcher: "Re: list: from 2 to 3 dimensions..looking for a nice way"
- Maybe reply: Robert Brewer: "RE: list: from 2 to 3 dimensions..looking for a nice way"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]