473,473 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Jul 18 '05 #1
4 1288
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']]]

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
Jul 18 '05 #2
Peter Otten wrote:
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']]]

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
Jul 18 '05 #3
Peter Otten wrote:
Peter Otten wrote:
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']]]

[...]
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

Jul 18 '05 #4
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/


Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
0
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
0
by: spencer | last post by:
Hi, I'm using NumPy to build an array of a list of names that will be of multiple dimensions.I did do a google on this subject, but couldn't find what I was looking for. The problem I'm having...
9
by: crispy | last post by:
I am looking for a free (or inexpensive) script to list the book and journal publications created within a college department and be able to sort them by author and year of publication. I've...
1
by: Muhammad Aftab Alam | last post by:
Hi All, I need to know how can I make a list contorl like of windows explorer. From windows explorer view I mean when we open My Computer a list view is shown and the headers are shown as follows...
1
by: Christina | last post by:
Hi, I've been looking at some code for dependent list boxes to adapt to a State and City list. There will only be 2 states for the first list box, and 3 cities in the second list box. When the...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
2
by: David F | last post by:
I a looking for a tool that creates a cross reference table for all global functions, and optionally class methods. That is, for each function, which function(s) does it call and what function(s) is...
1
by: luttkens | last post by:
I'm using mod_rewrtie to have nice look urls. One of my rules are: RewriteRule ^category/(+)/?$ /tidskrifter_lista.php?category=$1 ..which turns www/category/misc into...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.