Skip to main content
 首页 » 编程设计

python之为群组创建唯一 ID

2024年10月01日10java哥

我正在解决一个问题,我必须对相关项目进行分组并为它们分配一个唯一的 ID。我已经用 python 编写了代码,但它没有返回预期的输出。我需要帮助来完善我的逻辑。代码如下:

data = {} 
child_list = [] 
 
 
for index, row in df.iterrows(): 
    parent = row['source'] 
    child = row['target'] 
    #print 'Parent: ', parent 
    #print 'Child:', child 
    child_list.append(child) 
    #print child_list 
    if parent not in data.keys(): 
        data[parent] = [] 
    if parent != child: 
        data[parent].append(child) 
    #print data 
 
op = {} 
gid = 0 
 
 
def recursive(op,x,gid): 
    if x in data.keys() and data[x] != []: 
        for x_child in data[x]: 
            if x_child in data.keys(): 
                op[x_child] = gid 
                recursive(op,x_child,gid) 
            else: 
                op[x] = gid 
    else: 
        op[x] = gid 
 
 
for key in data.keys(): 
    #print "Key: ", key 
    if key not in child_list: 
        gid = gid + 1 
        op[key] = gid 
        for x in data[key]: 
            op[x] = gid 
            recursive(op,x,gid) 
 
related = pd.DataFrame({'items':op.keys(), 
                  'uniq_group_id': op.values()}) 
mapped.sort_values('items') 

下面的例子

Input: 
source  target 
a        b 
b        c 
c        c 
c        d 
d        d 
e        f 
a        d 
h        a 
i        f   
 
Desired Output: 
item     uniq_group_id 
a         1  
b         1 
c         1 
d         1 
h         1 
e         2 
f         2 
i         2 

我的代码给出了以下错误的输出。

item    uniq_group_id 
a       3 
b       3 
c       3 
d       3 
e       1 
f       2 
h       3 
i       2  

另一个例子

Input: 
df = pd.DataFrame({'source': ['a','b','c','c','d','e','a','h','i','a'], 
                'target':['b','c','c','d','d','f','d','a','f','a']}) 
Desired Output: 
item    uniq_group_id 
a       1 
b       1 
c       1 
d       1 
e       2 
f       2 
 
My code Output: 
item    uniq_group_id 
e   1 
f   1 

行的顺序或组 ID 无关紧要。这里重要的是为相关项目分配相同的唯一标识符。整个问题是找到相关的项目组并为它们分配一个唯一的组 ID。

请您参考如下方法:

我没有仔分割析你的代码,但看起来错误是因为你填充 data 字典的方式。它将子节点存储为其父节点的邻居,但它还需要将父节点存储为子节点的邻居。

我没有尝试修复您的代码,而是决定改编 this pseudocode由 Aseem Goyal 撰写。下面的代码从简单的 Python 列表中获取输入数据,但应该很容易使其适应 Pandas 数据框。

''' Find all the connected components of an undirected graph ''' 
 
from collections import defaultdict 
 
src = ['a', 'b', 'c', 'c', 'd', 'e', 'a', 'h', 'i', 'a'] 
tgt = ['b', 'c', 'c', 'd', 'd', 'f', 'd', 'a', 'f', 'a'] 
 
nodes = sorted(set(src + tgt)) 
print('Nodes', nodes) 
 
neighbors = defaultdict(set) 
for u, v in zip(src, tgt): 
    neighbors[u].add(v) 
    neighbors[v].add(u) 
 
print('Neighbors') 
for n in nodes: 
    print(n, neighbors[n]) 
 
visited = {} 
def depth_first_traverse(node, group_id): 
    for n in neighbors[node]: 
        if n not in visited: 
            visited[n] = group_id 
            depth_first_traverse(n, group_id) 
 
print('Groups') 
group_id = 1 
for n in nodes: 
    if n not in visited: 
        visited[n] = group_id 
        depth_first_traverse(n, group_id) 
        group_id += 1 
    print(n, visited[n]) 

输出

Nodes ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'] 
Neighbors 
a {'a', 'd', 'b', 'h'} 
b {'a', 'c'} 
c {'d', 'b', 'c'} 
d {'d', 'a', 'c'} 
e {'f'} 
f {'i', 'e'} 
h {'a'} 
i {'f'} 
Groups 
a 1 
b 1 
c 1 
d 1 
e 2 
f 2 
h 1 
i 2 

这段代码是为 Python 3 编写的,但也可以在 Python 2 上运行。如果你在 Python 2 上运行它,你应该在导入语句的顶部添加 from __future__ import print_function;这不是绝对必要的,但它会使输出看起来更好。