Further adventures in array slicing.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven W. Orr

    #1

    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':'field s0',
    'valid': 'valid0'
    55:{'name': 'Hello1',
    'fields':'field s1',
    'valid': 'valid1'},
    14:{'name': 'Hello2',
    'fields':'field s2',
    '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(md ict[ii]['name'])
    fields.append(m dict[ii]['fields'])
    valid.append(md ict[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,va lid = 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':(('Add ress', 8),
    ('Control', 8)),
    'valid': {'Address': (1,255),
    'Control': (33,44)}},
    1:{'name': 'Hello1',
    'fields':'field s1',
    'valid': 'valid1'},
    2:{'name': 'Hello2',
    'fields':'field s2',
    '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
  • John Machin

    #2
    Re: Further adventures in array slicing.

    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(an element)
    will give the same result if bseq == [anelement]

    You can use alist.extend(bs eq) 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

    Comment

    • 7stud

      #3
      Re: Further adventures in array slicing.

      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]

      Comment

      • Alex Martelli

        #4
        Re: Further adventures in array slicing.

        Steven W. Orr <steveo@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(md ict[ii]['name'])
        fields.append(m dict[ii]['fields'])
        valid.append(md ict[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':(('Add ress', 8),
        ('Control', 8)),
        'valid': {'Address': (1,255),
        'Control': (33,44)}},
        1:{'name': 'Hello1',
        'fields':'field s1',
        'valid': 'valid1'},
        2:{'name': 'Hello2',
        'fields':'field s2',
        '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.itervalue s()]
        fields = [v['fields'] for v in mdict.itervalue s()]
        valid = [v['valid'] for v in mdict.itervalue s()]

        but this just saves a few characters, if that.


        Alex

        Comment

        Working...