473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Further adventures in array slicing.

This is more for my education and not so much for practicality.

I have a structure that sort of looks like this:

mdict = {33:{'name': 'Hello0',
'fields':'fields0',
'valid': 'valid0'
55:{'name': 'Hello1',
'fields':'fields1',
'valid': 'valid1'},
14:{'name': 'Hello2',
'fields':'fields2',
'valid': 'valid2'}}

i.e, each element of the dictionary is itself a dictionary with three
common keys.

I need to unpack this into three seperate arrays called name, fields,
valid. The old code looked like this:

names = []; fields = []; valid = []
for ii in mdict:
names.append(mdict[ii]['name'])
fields.append(mdict[ii]['fields'])
valid.append(mdict[ii]['valid'])

I tried this (to see if it would work) and it seems to work just fine:

def u2(m):
aa = [ms[ii][jj] for jj in 'name','fields','valid' for ii in m]
return tuple(zip(aa[0::3], aa[1::3], aa[2::3]))
names,fields,valid = u2(mdict)

I was very pleased with myself, except that the real world example of
'fields' and 'valid' is that they can be (but not always) a sequence.
e.g.,

mdefs = {0:{'name': 'Hello0',
'fields':(('Address', 8),
('Control', 8)),
'valid': {'Address': (1,255),
'Control': (33,44)}},
1:{'name': 'Hello1',
'fields':'fields1',
'valid': 'valid1'},
2:{'name': 'Hello2',
'fields':'fields2',
'valid': 'valid2'}}

Is there a way to do this with possibly a more concise technique than the
first for loop above?

A second question is: When can you use += vs .append(). Are the two always
the same?

Thanks. :-)

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
May 4 '07 #1
3 1183
On May 5, 7:03 am, "Steven W. Orr" <ste...@syslang.netwrote:
This is more for my education and not so much for practicality.
[snip]
>
A second question is: When can you use += vs .append(). Are the two always
the same?
Formally, they can never be the same. They can be used to produce the
same result, in limited circumstances, e.g. these:
alist += bseq
and
alist.append(anelement)
will give the same result if bseq == [anelement]

You can use alist.extend(bseq) instead of alist += bseq

I suggest that you
(a) read the manual sections on each of the 3 possibilities
(b) explore their behaviours at the Python interactive prompt

May 4 '07 #2
A second question is: When can you use += vs .append().
Are the two always the same?
They are never the same unless you only add one item to the list.
append() will only increase the length of a list by 1.

la = [1,2]
lb = [3, 4, 5]
la += lb
print la

lc = [1,2]
lc.append(lb)
print lc

--output:--
[1, 2, 3, 4, 5]
[1, 2, [3, 4, 5]]
print la[2]
print lc[2]

--output:--
3
[3, 4, 5]

May 4 '07 #3
Steven W. Orr <st****@syslang.netwrote:
...
I need to unpack this into three seperate arrays called name, fields,
valid. The old code looked like this:
You're using lists, not arrays. If you DID want arrays, you'd have to
import standard library module array, and you'd be limited to a few
elementary types as items; it's pretty obvious that this is not what you
want -- nevertheless, why use the wrong name?
names = []; fields = []; valid = []
for ii in mdict:
names.append(mdict[ii]['name'])
fields.append(mdict[ii]['fields'])
valid.append(mdict[ii]['valid'])
The order of keys in dictionary mdict is totally arbitrary, therefore
the order of items in those lists is going to be equally arbitrary; you
sure you _want_ that? Normally order IS significant in lists.
I was very pleased with myself, except that the real world example of
'fields' and 'valid' is that they can be (but not always) a sequence.
Why would that make any difference at all?
e.g.,

mdefs = {0:{'name': 'Hello0',
'fields':(('Address', 8),
('Control', 8)),
'valid': {'Address': (1,255),
'Control': (33,44)}},
1:{'name': 'Hello1',
'fields':'fields1',
'valid': 'valid1'},
2:{'name': 'Hello2',
'fields':'fields2',
'valid': 'valid2'}}

Is there a way to do this with possibly a more concise technique than the
first for loop above?
not by much:

names = [v['name'] for v in mdict.itervalues()]
fields = [v['fields'] for v in mdict.itervalues()]
valid = [v['valid'] for v in mdict.itervalues()]

but this just saves a few characters, if that.
Alex
May 5 '07 #4

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

Similar topics

1
by: beliavsky | last post by:
Suppose I define a trivial class composed of x and y coordinates and create a Numeric array of instances of that class. class xy: def __init__(self,x=0.0,y=0.0): self.x = x self.y = y def...
7
by: LutherRevisited | last post by:
I'm wanting to do something with a list that is basically a 2 dimensional array. I'm not so good with lists so can someone give me an example of how I might implement this in Python? thanks.
2
by: Yun Mao | last post by:
Hi python gurus, I have some questions when I'm using python numeric: 1. When I do v = u, it seems u and v still point to the same memory. e.g. When I do v=0, u will be zero out as well....
10
by: amparikh | last post by:
Ok, my question is not about Virtual destructors and why, but more on the significance. Generally we have a virtual destructor in the base class ( and inadvertently in the derived class) so that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.