Python matching-item based concatenation of list of sublists? -


without getting deep, input below represents following:

[geometry,name,z-coord,key-region, ... ]

any key-regions matching between sublists influence merger of sublists geometry fields combined single string, , name fields combined single string. while retaining remainder of both sublists should match.

input:

[['aquitards~:#>0', 'aquitard 1', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>2', 'aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>2', 'aquitard 7', 1, '4', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>0', 'aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', ''],  ['aquitards~:#>1', 'aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''],  ['aquitards~:#>1', 'aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']] 

current merging method:
code below works, merging pairs of sublists one. needs revised or rewritten enable infinite number of matches merged 1 sublist. troubled go here...

        matchlist = []         rawrows = []         idxa,rowa in enumerate(templist):             idxb,rowb in enumerate(templist):                 if idxa!=idxb:                     if int(rowb[3])==int(rowa[3]):                         temprow = [rowa[0]+'}~{'+rowb[0],rowa[1]+';'+rowb[1]]                         reversematchrow = [rowb[0]+'}~{'+rowa[0],rowb[1]+';'+rowa[1]]                         temprow.extend(rowb[2:])                         reversematchrow.extend(rowb[2:])                         if not reversematchrow in rawrows:                             rawrows.append(temprow)                             matchlist.append(rowa)                             matchlist.append(rowb)                             continue                     elif rowb in matchlist: continue                 elif idxa==idxb:                     if not rowb in rawrows:                         if not rowb in matchlist:                             rawrows.append(rowb)                         continue         row in rawrows:             if not row in matchlist:                 self.rows.append(row) 

current output:

the above input , merging method give following result highlight , how things ideally merged.

['aquitards~:#>0}~{aquitards~:#>2', 'aquitard 1;aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>2}~{aquitards~:#>0', 'aquitard 7;aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>1', 'aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>1', 'aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', ''] 

conclusive question:

-how 1 string-merge first 2 items each sublist within list of sublists sublists based on matching index items; furthermore remove merged sublists original source sublists, , retain non-matching sublists - resulting in single cleaned-up list of sublists?

for example, key-matching index each sublist below [3];

idealized input:

    [['aquitards~:#>0', 'aquitard 1', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>2', 'aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>3', 'aquitard 5', 1, '4', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>4', 'aquitard 4', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>2', 'aquitard 7', 1, '4', '', '', '', '', '', '', '', '', '', '', ''], ['aquitards~:#>0', 'aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', ''],  ['aquitards~:#>1', 'aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''],  ['aquitards~:#>1', 'aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']]   

idealized output:

    ['aquitards~:#>0}~{aquitards~:#>2}~{aquitards~:#>4', 'aquitard 1;aquitard 3;;aquitard 5', 1, '2', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>2}~{aquitards~:#>0}~{aquitards~:#>3', 'aquitard 7;aquitard 8;;aquitard 4', 1, '4', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>1', 'aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''] ['aquitards~:#>1', 'aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', ''] 

https://docs.python.org/2/library/itertools.html#itertools.chain

is place want start. make first pass easier understand , tune-up recommend declaring sublists own variables , breaking out slices want use ahead of .chain() call. easier understand 'under hood' usage way until you're confident it.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -