一部电影最多统计两个三人组合
时间: 2024-01-30 14:02:56 浏览: 63
好的,那么在统计三人组合的时候,可以增加一个判断条件来判断是否已经有两个三人组合涵盖了这个二人组合的参演电影。如果已经涵盖,则不再统计这个二人组合。
修改后的代码如下:
```python
import openpyxl
# 打开Excel文件
workbook = openpyxl.load_workbook('电影信息统计.xlsx')
# 获取电影信息sheet
movies_sheet = workbook['电影信息']
# 获取演员信息sheet
actors_sheet = workbook['演员信息']
# 新增演员组合参演统计sheet
combos_sheet = workbook.create_sheet('演员组合参演统计')
# 初始化演员组合参演数据
combo_data = {}
# 统计三人组合
for i in range(2, actors_sheet.max_row + 1):
actor1 = actors_sheet.cell(row=i, column=2).value
for j in range(i + 1, actors_sheet.max_row + 1):
actor2 = actors_sheet.cell(row=j, column=2).value
for k in range(j + 1, actors_sheet.max_row + 1):
actor3 = actors_sheet.cell(row=k, column=2).value
combo = ','.join(sorted([actor1, actor2, actor3]))
movies = set()
for row in range(2, movies_sheet.max_row + 1):
if actor1 in movies_sheet.cell(row=row, column=4).value and \
actor2 in movies_sheet.cell(row=row, column=4).value and \
actor3 in movies_sheet.cell(row=row, column=4).value:
movies.add(movies_sheet.cell(row=row, column=1).value)
if combo in combo_data:
combo_data[combo]['movies'].update(movies)
else:
combo_data[combo] = {'count': 0, 'movies': movies}
if len(combo_data[combo]['movies']) >= 2:
combo_data[combo]['count'] += 1
if len(combo_data[combo]['movies']) >= 1:
for sub_combo in combo_data.keys():
if combo != sub_combo and len(sub_combo.split(',')) == 2 and \
actor1 in sub_combo and actor2 in sub_combo and len(combo_data[sub_combo]['movies']) < 2 and \
len(combo_data[combo]['movies'].difference(combo_data[sub_combo]['movies'])) == 0:
break
else:
continue
for sub_combo in combo_data.keys():
if combo != sub_combo and len(sub_combo.split(',')) == 2 and \
actor2 in sub_combo and actor3 in sub_combo and len(combo_data[sub_combo]['movies']) < 2 and \
len(combo_data[combo]['movies'].difference(combo_data[sub_combo]['movies'])) == 0:
break
else:
continue
for sub_combo in combo_data.keys():
if combo != sub_combo and len(sub_combo.split(',')) == 2 and \
actor1 in sub_combo and actor3 in sub_combo and len(combo_data[sub_combo]['movies']) < 2 and \
len(combo_data[combo]['movies'].difference(combo_data[sub_combo]['movies'])) == 0:
break
else:
combo_data[combo]['count'] += 1
# 统计二人组合
for i in range(2, actors_sheet.max_row + 1):
actor1 = actors_sheet.cell(row=i, column=2).value
for j in range(i + 1, actors_sheet.max_row + 1):
actor2 = actors_sheet.cell(row=j, column=2).value
combo = ','.join(sorted([actor1, actor2]))
if combo in combo_data:
continue
movies = set()
for row in range(2, movies_sheet.max_row + 1):
if actor1 in movies_sheet.cell(row=row, column=4).value and \
actor2 in movies_sheet.cell(row=row, column=4).value:
movies.add(movies_sheet.cell(row=row, column=1).value)
combo_data[combo] = {'count': len(movies), 'movies': movies}
# 将演员组合参演数据写入Excel
combos_sheet.cell(row=1, column=1, value='演员组合')
combos_sheet.cell(row=1, column=2, value='演员组合参演电影数量')
combos_sheet.cell(row=1, column=3, value='演员组合参演电影列表')
row_num = 2
for combo in sorted(combo_data.keys(), key=lambda x: (-combo_data[x]['count'], x)):
if len(combo.split(',')) == 2 and combo_data[combo]['count'] == 0:
continue
combos_sheet.cell(row=row_num, column=1, value=combo)
combos_sheet.cell(row=row_num, column=2, value=combo_data[combo]['count'])
combos_sheet.cell(row=row_num, column=3, value=','.join(sorted(combo_data[combo]['movies'])))
row_num += 1
# 保存Excel文件
workbook.save('电影信息统计.xlsx')
```
在统计三人组合的时候,增加了三个for循环和一个判断语句。这三个for循环用于判断一个二人组合是否已经被两个三人组合涵盖,判断语句用于判断是否需要统计这个二人组合。如果已经被涵盖,则不再统计这个二人组合。
阅读全文