<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def comm_grid(patch, cols, splits, divs, metric='Sorensen'):
 """ Calculates commonality as a function of distance for a gridded patch Parameters {0} divs : str Description of how to divide x_col and y_col. Unlike SAR and EAR, only one division can be given at a time. See notes. metric : str One of Sorensen or Jaccard, giving the metric to use for commonality calculation Returns ------- {1} Result has three columns, pair, x, and y, that give the locations of the pair of patches for which commonality is calculated, the distance between those cells, and the Sorensen or Jaccard result. Notes ----- {2} For gridded commonality, cols must also contain x_col and y_col, giving the x and y dimensions along which to grid the patch. {3} """


    (spp_col, count_col, x_col, y_col), patch = \
        _get_cols(['spp_col', 'count_col', 'x_col', 'y_col'], cols, patch)

    # Loop through each split
    result_list = []
    for substring, subpatch in _yield_subpatches(patch, splits):

        # Get spatial table and break out columns
        spatial_table = _yield_spatial_table(subpatch, divs, spp_col,
                                             count_col, x_col, y_col)
        spp_set = spatial_table['spp_set']
        cell_loc = spatial_table['cell_loc']
        n_spp = spatial_table['n_spp']

        # Get all possible pairwise combinations of cells
        pair_list = []
        dist_list = []
        comm_list = []
        for i in range(len(spatial_table)):
            for j in range(i+1, len(spatial_table)):

                iloc = np.round(cell_loc[i], 6)
                jloc = np.round(cell_loc[j], 6)
                pair_list.append('('+str(iloc[0])+' '+str(iloc[1])+') - '+
                                 '('+str(jloc[0])+' '+str(jloc[1])+')')

                dist_list.append(_distance(cell_loc[i], cell_loc[j]))

                ij_intersect = spp_set[i] & spp_set[j]
                if metric.lower() == 'sorensen':
                    comm = 2*len(ij_intersect) / (n_spp[i] + n_spp[j])
                elif metric.lower() == 'jaccard':
                    comm = len(ij_intersect) / len(spp_set[i] | spp_set[j])
                else:
                    raise ValueError, ("Only Sorensen and Jaccard metrics are "
                                      "available for gridded commonality")
                comm_list.append(comm)

        # Append subset result
        subresult = pd.DataFrame({'pair': pair_list, 'x': dist_list,
                                  'y': comm_list})
        result_list.append((substring, subresult))

    # Return all results
    return result_list