import openpyxl
import pandas as pd
def one_lie():
# 打开 Excel 文件
# workbook = openpyxl.load_workbook('test.xlsx')
workbook = openpyxl.load_workbook('testmain.xlsx')
# 获取第一个工作表
worksheet = workbook.worksheets[0]
# 获取第一列的所有单元格对象
column_cells = worksheet['A:A']
# 提取单元格的值
column_values = [cell.value for cell in column_cells][1:]
# 输出第一列的所有值
# print(column_values)
# print(len(column_values))
# print(type(column_values))
return column_values
def two_lie():
two_list = []
# 打开 Excel 文件
# workbook = openpyxl.load_workbook('test.xlsx')
workbook = openpyxl.load_workbook('testmain.xlsx')
# 获取第一个工作表
worksheet = workbook.worksheets[0]
# 获取第二列的所有单元格对象
column_cells = worksheet['B:B']
# 提取单元格的值
column_values = [cell.value for cell in column_cells][1:]
# 输出第二列的所有值
# print(column_values)
# print(len(column_values))
for i in column_values:
# print(i)
new_i = i.strip('[').strip(']').replace('\'', '')
new_ii = new_i.split(',')
# print(new_ii)
two_list.append(new_ii)
# exit()
return two_list
def main():
oneLs = one_lie()
twoLs = two_lie()
# print(len(oneLs), oneLs)
# print(len(twoLs), twoLs)
cc = twoLs
couls = []
for indexi, val0 in enumerate(oneLs):
twoLs1 = cc # 最好使用yeild(减少开辟内存)
if val0 is None:
continue
cou = 0
# print(val0)
inon = twoLs1.pop(indexi)
for val1 in twoLs1:
# del twoLs[indexi]
# twoLs.remove(twoLs[indexi - 1])
inter = [i for i in inon if i in val1] # 判断数组中的任意数是否在另外一个数组内
if (val0 in val1) and len(inter) == 0:
cou += 1
# with open('a.txt', 'a', encoding='utf-8') as f:
# # print(f'{val0} 出现 {cou}次')
# f.write(f'{val0} 出现 {cou}次\n')
print(f'{val0} 出现 {cou}次\n')
couls.append(cou)
# exit()
print(len(couls))
# return couls # 重复列表
if __name__ == '__main__':
main()
# cck = main()
# # 创建数据
# data = {'重复数': [1, 2, 3, 4, 5]} # 这里返回的重复数列表
# data = {'重复数': cck} # 这里返回的重复数列表
#
# # 创建DataFrame对象
# df = pd.DataFrame(data)
#
# # 创建Excel文件写入器
# writer = pd.ExcelWriter('output.xlsx')
#
# # 将数据写入指定的列
# df.to_excel(writer, sheet_name='Sheet1', startcol=3, index=False)
#
# # 保存文件
# writer.save()
excel自动记录重复数
发布时间 2023-04-17 01:11:37作者: 她说没有光