Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This does the job, if I undesrtood correctly:</p> <pre><code>l = [[10312, -13.069404602050781], [10313, -28.044403076171875], [10314, -32.765602111816406], [10315, -47.353294372558594], [10312, -63.069404602050781], [10313, -78.044403076171875], [10314, -82.765602111816406], [10315, -97.353294372558594]] from pprint import pprint d = {} for i,(x,n) in enumerate(l): print i,x,n if x in d: l[d[x]].append(n) del l[i][:] else: d[x] = i l = filter(None,l) pprint (l) </code></pre> <p>.</p> <h3>Edit</h3> <p>Here's a better algorithm because there's no more the filtering of the list done by the instruction <code>l = filter(None,l)</code> , so the transformation is in-place.</p> <p>This instruction <code>l = filter(None,l)</code> creates a new list, that is to say a new object at another address in the memory: then the above code don't realize an in-place transformation of the list.<br> The following one performs such an in-place transformation, as it is put in evidence by the printing of the identities (id est addresses) of the list <code>l</code> before and after its treatment.</p> <pre><code>l = [[10312, -13.069404602050781], [10313, -28.044403076171875], [10314, -32.765602111816406], [10312, -63.069404602050781, -55.4444], [20666, -91, -92, -93, -94], [10315, -47.353294372558594], [10314, -82.765602111816406], [10315, -97.353294372558594], [10313, -78.044403076171875], [20666, -40.33, -94, -50.33, -91, -93] ] from pprint import pprint d = {} to_del = [] print 'id(l) before : ',id(l) pprint (l) for i,subli in enumerate(l): if subli[0] in d: d[subli.pop(0)].extend(subli) to_del.insert(0,i) else: d[subli[0]] = subli for i in to_del: del l[i] print '\nid(l) after : ',id(l) pprint (l) </code></pre> <ul> <li><p>Note that in the former code, the values of <code>d</code> were the indexes of the sublists of <code>l</code>.<br> Now <strong>in this new code, the values of <code>d</code> are directly the sublists of <code>l</code></strong>.<br> It is more pythonic to reference objects directly, instead of referecing them indirectly through their indexes in the list whose they are elements of.</p></li> <li><p>The list <code>to_del</code> records the indexes of the sublists that will be deleted after the first loop. Each index added is inserted at the beginning of <code>to_del</code> (not appended to it) so that the second loop ( <code>for i in to_del</code> )will <strong>run retrogressively</strong> through the list <code>l</code> , which <strong>is the condition that must be respected when elements of a list are deleted on the basis of their indexes</strong>.</p></li> </ul> <p>The instruction <code>d[subli.pop(0)].extend(subli)</code> may seem somewhat a little difficult to understand. </p> <p>The operations begins with the execution of <code>subli.pop(0)</code> : this instruction triggers the extraction of the element indexed 0 from the sublist <code>subli</code> and returns it.<br> Then <code>d[subli.pop(0)]</code> furnishes the object <strong>subli.pop(0)</strong> to <code>d</code> as a key, meanwhile this objects is removed from the sublist <code>subli</code>.<br> So, at this point, the sublist <code>subli</code> has been shortened of its first element, as if the instruction <code>subli[:] = subli[1:]</code> had been performed <sup>see remark at the bottom</sup>. </p> <p>Next, the sublist <code>d[subli.pop(0)]</code> , that had been precedently encountered in <code>l</code> during the iteration through list <code>l</code> , is extended with the elements remaining in the sublist <code>subli</code> after this one has been shortened, that is to say with the elements that were indexed <code>1</code> to <code>len(subli)-1</code> BEFORE it was shortened. But, as <code>subli</code> has been shortened, we only write <code>subli</code>, not <code>subli[1:]</code>. </p> <p>And it works ! Result:</p> <pre><code>id(l) before : 18732936 [[10312, -13.069404602050781], [10313, -28.044403076171875], [10314, -32.765602111816406], [10312, -63.06940460205078, -55.4444], [20666, -91, -92, -93, -94], [10315, -47.353294372558594], [10314, -82.7656021118164], [10315, -97.3532943725586], [10313, -78.04440307617188], [20666, -40.33, -94, -50.33, -91, -93]] id(l) after : 18732936 [[10312, -13.069404602050781, -63.06940460205078, -55.4444], [10313, -28.044403076171875, -78.04440307617188], [10314, -32.765602111816406, -82.7656021118164], [20666, -91, -92, -93, -94, -40.33, -94, -50.33, -91, -93], [10315, -47.353294372558594, -97.3532943725586]] </code></pre> <p>.</p> <p>If you want that only non-redudant elements be added to a previously existing sublist, it must be:</p> <pre><code>for i,subli in enumerate(l): print 1,subli if subli[0] in d: precsubli = d[subli.pop(0)] precsubli.extend(el for el in subli if el not in precsubli) to_del.insert(0,i) else: d[subli[0]] = subli </code></pre> <p>.</p> <h3>Important remark</h3> <p>Note the difference :</p> <pre><code>N = [10000,2,3,4] initial_id = id(N) print initial_id, N N = N[1:] print id(N), N print '%d==%d : %s' %\ (initial_id, id(N), initial_id==id(N) ) print '------------------' A = ['HEY','b','c','d'] initial_id = id(A) print initial_id, A A[:] = A[1:] print id(A), A print '%d==%d : %s' %\ (initial_id, id(A), initial_id==id(A) ) </code></pre> <p>result</p> <pre><code>18669480 [10000, 2, 3, 4] 11868480 [2, 3, 4] 18669480==11868480 : False ------------------ 18731816 ['HEY', 'b', 'c', 'd'] 18731816 ['b', 'c', 'd'] 18731816==18731816 : True </code></pre> <p>That means that <code>A</code> is modified in-place, while object referenced by <code>N</code> is not: the instruction <code>N[1:]</code> builds a new object, at a location in the memory elsewhere than the location of the object referenced by <code>N</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload