list: from 2 to 3 dimensions..looking for a nice way

S

sven

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
 
M

Mike C. Fletcher

You want to collect those items in a with equal values for item[1], so:
.... c.setdefault(item[1],[]).append( item )
.... [[[1, 'house'], [2, 'house']], [[3, 'garden']]]

HTH,
Mike
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
 
P

Peter Otten

sven said:
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']]]


How about:

a = [[1,'house'],[2,'house'],[3,'garden']]

d = {}
for n, s in a:
d.setdefault(s,[]).append(n)

b = [[[n, s] for n in nlist] for s, nlist in d.iteritems()]
print b

Peter
 
P

Peter Otten

Peter said:
sven said:
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']]]


How about:

a = [[1,'house'],[2,'house'],[3,'garden']]

d = {}
for n, s in a:
d.setdefault(s,[]).append(n)

b = [[[n, s] for n in nlist] for s, nlist in d.iteritems()]
print b

Or rather

a = [[1,'house'],[2,'house'],[3,'garden']]

d = {}
for ns in a:
d.setdefault(ns[1], []).append(n)
b = d.items()

Peter
 
P

Peter Otten

Peter said:
Peter said:
sven said:
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']]]
[...]

Or rather

a = [[1,'house'],[2,'house'],[3,'garden']]

d = {}
for ns in a:
d.setdefault(ns[1], []).append(n)
b = d.items()

So many errors in so few lines :-(
Hope I get it right this time:

a = [[1,'house'],[2,'house'],[3,'garden']]

d = {}
for ns in a:
d.setdefault(ns[1], []).append(ns)
b = d.values()

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top