0

I joined two tables together and what I like to do is concatenate multi vaule in one records without duplicated value.

  1. Input Table

    Table name: TAXLOT_ZONE
    TID ZONE
    1 A
    1 A
    1 B
    1 C
    2 D
    2 D
    2 E
    3 A
    3 B
    4 C
    5 D

  2. Desirable output table looks like;

    table name: Taxlot_zone_out
    TID ZONE
    1 A, B, C
    2 D, E
    3 A, B
    4 C
    5 D

user295216
  • 51
  • 2
  • 1
    There are no such things as "tables" in pure Python. Are you using a database? If so, what kind of interface? SQL? If so, see http://stackoverflow.com/questions/2451026/minimizing-sql-queries-using-join-with-one-to-many-relationship/2451065#2451065 – Max Shawabkeh Mar 16 '10 at 22:27
  • I use file geodatabase or dbf format which is created by ESRI, GIS company. I just want to create a new table from this joined table. – user295216 Mar 16 '10 at 22:37

1 Answers1

1

Assuming your table is in sorted order and is iterable, you can use itertools.groupby to group rows with the same first element.

l = [(1, 'A'), (1, 'A'), (1, 'B'), (1, 'C'),
     (2, 'D'), (2, 'D'), (2, 'E'),
     (3, 'A'), (3, 'B'),
     (4, 'C'),
     (5, 'D')]

from itertools import groupby
from operator import itemgetter
result = [(taxlot, list(set(v for k,v in g)))
          for taxlot, g in groupby(l, itemgetter(0))]

Result:

[(1, ['A', 'C', 'B']),
 (2, ['E', 'D']),
 (3, ['A', 'B']),
 (4, ['C']),
 (5, ['D'])]
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452