Python 创建索引子列表,每个子列表引用元组列表中的唯一元组集
我试图通过将元组的索引与元组列表中的任何公共元素分组,或者将唯一元组索引分开,来创建索引的子列表。唯一元组是元组中没有元素的定义与列表中其他元组相同位置的元素相同。 示例:列出将同一家公司分组在一起的列表,同一家公司定义为同一名称或同一注册号或同一CEO姓名Python 创建索引子列表,每个子列表引用元组列表中的唯一元组集,python,list,indexing,tuples,sublist,Python,List,Indexing,Tuples,Sublist,我试图通过将元组的索引与元组列表中的任何公共元素分组,或者将唯一元组索引分开,来创建索引的子列表。唯一元组是元组中没有元素的定义与列表中其他元组相同位置的元素相同。 示例:列出将同一家公司分组在一起的列表,同一家公司定义为同一名称或同一注册号或同一CEO姓名 company_list = [("companyA",0002,"ceoX"), ("companyB"),0002,"ceoY&quo
company_list = [("companyA",0002,"ceoX"),
("companyB"),0002,"ceoY"),
("companyC",0003,"ceoX"),
("companyD",004,"ceoZ")]
所需的输出将是:
[[0,1,2],[3]]
有人知道这个问题的解决方案吗?这些公司形成了一个图表。您希望从连接的公司创建集群 试试这个:
company_list = [
("companyA",2,"ceoX"),
("companyB",2,"ceoY"),
("companyC",3,"ceoX"),
("companyD",4,"ceoZ")
]
# Prepare indexes
by_name = {}
by_number = {}
by_ceo = {}
for i, t in enumerate(company_list):
if t[0] not in by_name:
by_name[t[0]] = []
by_name[t[0]].append(i)
if t[1] not in by_number:
by_number[t[1]] = []
by_number[t[1]].append(i)
if t[2] not in by_ceo:
by_ceo[t[2]] = []
by_ceo[t[2]].append(i)
# BFS to propagate group to connected companies
groups = list(range(len(company_list)))
for i in range(len(company_list)):
g = groups[i]
queue = [g]
while queue:
x = queue.pop(0)
groups[x] = g
t = company_list[x]
for y in by_name[t[0]]:
if g < groups[y]:
queue.append(y)
for y in by_number[t[1]]:
if g < groups[y]:
queue.append(y)
for y in by_ceo[t[2]]:
if g < groups[y]:
queue.append(y)
# Assemble result
result = []
current = None
last = None
for i, g in enumerate(groups):
if g != last:
if current:
result.append(current)
current = []
last = g
current.append(i)
if current:
result.append(current)
print(result)
公司列表=[
(“公司”,2,“CEO”),
(“公司B”,2,“首席执行官”),
(“companyC”,3,“CEO”),
(“companyD”,4,“ceoZ”)
]
#编制索引
按名称={}
按_编号={}
首席执行官{}
对于枚举中的i,t(公司列表):
如果t[0]不在按_名称中:
按名称[t[0]=[]
按名称[t[0]]追加(i)
如果t[1]不在by_编号中:
按编号[t[1]=[]
按编号[t[1]]追加(i)
如果t[2]未由首席执行官提交:
由首席执行官[t[2]=[]
由首席执行官[t[2]]签署。附加(i)
#BFS向关联公司宣传集团
组=列表(范围(len(公司列表)))
对于范围内的i(len(公司列表)):
g=组[i]
队列=[g]
排队时:
x=队列.pop(0)
群[x]=g
t=公司名单[x]
对于y,按名称[t[0]]:
如果g<组[y]:
queue.append(y)
对于y in by_编号[t[1]]:
如果g<组[y]:
queue.append(y)
对于y,由首席执行官[t[2]]提出:
如果g<组[y]:
queue.append(y)
#汇编结果
结果=[]
电流=无
最后一个=无
对于枚举中的i,g(组):
如果g!=最后:
如果当前:
结果追加(当前)
当前=[]
last=g
当前.附加(i)
如果当前:
结果追加(当前)
打印(结果)
Fafl的答案肯定更有效。如果您不担心性能,这里有一个蛮力解决方案,可能更容易阅读。试图用一些评论来说明这一点
def find_index(res, target_index):
for index, sublist in enumerate(res):
if target_index in sublist:
# yes, it's present
return index
return None # not present
def main():
company_list = [
('companyA', '0002', 'CEOX'),
('companyB', '0002', 'CEOY'),
('companyC', '0003', 'CEOX'),
('companyD', '0004', 'CEOZ'),
('companyE', '0004', 'CEOM'),
]
res = []
for index, company_detail in enumerate(company_list):
# check if this `index` is already present in a sublist in `res`
# if the `index` is already present in a sublist in `res`, then we need to add to that sublist
# otherwise we will start a new sublist in `res`
index_to_add_to = None
if find_index(res, index) is None:
# does not exist
res.append([index])
index_to_add_to = len(res) - 1
else:
# exists
index_to_add_to = find_index(res, index)
for c_index, c_company_detail in enumerate(company_list):
# inner loop to compare company details with the other loop
if c_index == index:
# same, ignore
continue
if company_detail[0] == c_company_detail[0] or company_detail[1] == c_company_detail[1] or company_detail[2] == c_company_detail[2]:
# something matches, so append
res[index_to_add_to].append(c_index)
res[index_to_add_to] = list(set(res[index_to_add_to])) # make it unique
print(res)
if __name__ == '__main__':
main()
看看这个,我为此做了很多努力。可能是我遗漏了一些测试用例。就性能而言,我认为它很好。
我使用了set()
并弹出属于同一组的内容
company_list = [
("companyA",2,"ceoX"),
("companyB",2,"ceoY"),
("companyC",3,"ceoX"),
("companyD",4,"ceoZ"),
("companyD",3,"ceoW")
]
index = {val: key for key, val in enumerate(company_list)}
res = []
while len(company_list):
new_idx = 0
temp = []
val = company_list.pop(new_idx)
temp.append(index[val])
while new_idx < len(company_list) :
if len(set(val + company_list[new_idx])) < 6:
temp.append(index[company_list.pop(new_idx)])
else:
new_idx += 1
res.append(temp)
print(res)
公司列表=[
(“公司”,2,“CEO”),
(“公司B”,2,“首席执行官”),
(“companyC”,3,“CEO”),
(“companyD”,4,“ceoZ”),
(“companyD”,3,“ceoW”)
]
索引={val:key for key,枚举(公司列表)中的val}
res=[]
而len(公司名单):
new_idx=0
温度=[]
val=公司列表.pop(新的idx)
临时追加(索引[val])
而新的idx
您能否添加解决此问题的尝试,以及它到底出了什么问题?StackOverflow专注于特定的编码问题和这些问题的解决方案。事实上,这个问题对于本网站来说过于笼统,可能更适合处理更高级别讨论和问题解决的其他StackExchange网站之一。