Further adventures in array slicing.

S

Steven W. Orr

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
 
J

John Machin

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
 
7

7stud

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]
 
A

Alex Martelli

Steven W. Orr said:
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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top