使用python查找重復(fù)值

任務(wù)要點

在詞表中,一些單詞重復(fù),并有重復(fù)例句。找出所有重復(fù)單詞的索引,并將重復(fù)例句合并。最后將整張詞表分割成重復(fù)值和非重復(fù)值部分。

核心代碼

1、使用xlwt和xlrd模塊讀寫Excel

讀取Excel的步驟在于,獲得所有sheet名字的數(shù)組,通過名字讀取某一個sheet的內(nèi)容,然后使用sheet.row_values()和sheet.col_values()獲取某一行或列的內(nèi)容。

initialData = ‘...’ #需要讀取的excel的路徑
workbook = xlrd.open_workbook(initialData)
sheet_names = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheet_names[0])
data = sheet.col_values(4)

寫入EXCEL的步驟在于,使用xlwt.Workbook()新建一個Excel緩存,然后使用.add_sheet()指定名字新建sheet。

book = xlwt.Workbook(encoding='utf-8', style_compression=0)
wSheet1 = book.add_sheet("noRepetition")
wSheet2 = book.add_sheet("repetition")

2、使用set(data)去除所有重復(fù)值

構(gòu)建矩陣allData,儲存所有單詞的序號、重復(fù)次數(shù)、單詞內(nèi)容。

data_unique = set(data)
allData = []
    
for item in data_unique:
    id = data.index(item)
    num = data.count(item)
    allData.append([id,num,data[id].strip()])

3、查找所有例句

核心思想是使用.index()查找重復(fù)單詞的所有例句,.index()只能查找找到的第一個單詞的索引。根據(jù)重復(fù)單詞的重復(fù)次數(shù),把之前找到的單詞有其他內(nèi)容代替,然后循環(huán)查找,就能找到所有例句了。(引自:https://blog.csdn.net/qq_33094993/article/details/53584379,也叫“偷梁換柱”)

nid = id
for n in range(num-1):
    data[nid] = 'quchu'
    print(id, num, data[nid])
    nid = data.index(word)
    nwordData = sheet.row_values(nid)
    wSheet2.write(c2, 1+dlen+4*n, nwordData[6])
    wSheet2.write(c2, 1+dlen+4*n+1, nwordData[7])
    wSheet2.write(c2, 1+dlen+4*n+2, nwordData[8])
    wSheet2.write(c2, 1+dlen+4*n+3, nwordData[9])

所有代碼

import xlwt,xlrd

initialData = 'book.xlsx'
workbook = xlrd.open_workbook(initialData)
sheet_names = workbook.sheet_names()

sheet = workbook.sheet_by_name(sheet_names[0])
data = sheet.col_values(4)
print(len(data))
for i in range(len(data)):
    data[i] = data[i].strip()
    
data_unique = set(data)
allData = []
    
for item in data_unique:
    id = data.index(item)
    num = data.count(item)
    allData.append([id,num,data[id].strip()])

book = xlwt.Workbook(encoding='utf-8', style_compression=0)
wSheet1 = book.add_sheet("noRepetition")
wSheet2 = book.add_sheet("repetition")
c1 = 0
c2 = 0
for d in allData:
    id = d[0]
    
    num = d[1]
    word = d[2]
    
    wordData = sheet.row_values(int(id))
    if num > 1:
        wSheet2.write(c2, 0, num)
        dlen = len(wordData)
        for i in range(dlen):
            wSheet2.write(c2, i+1, wordData[i])
        nid = id
        for n in range(num-1):
            data[nid] = 'quchu'
            print(id, num, data[nid])
            nid = data.index(word)
            nwordData = sheet.row_values(nid)
            wSheet2.write(c2, 1+dlen+4*n, nwordData[6])
            wSheet2.write(c2, 1+dlen+4*n+1, nwordData[7])
            wSheet2.write(c2, 1+dlen+4*n+2, nwordData[8])
            wSheet2.write(c2, 1+dlen+4*n+3, nwordData[9])
        c2 = c2 + 1
    else:
        for i in range(len(wordData)):
            wSheet1.write(c1, i, wordData[i])
        c1 = c1 + 1

savePath = 'book_分離.xls'
book.save(savePath)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容