基于Python的語料庫數(shù)據(jù)處理(四)
《Python玩轉語料庫數(shù)據(jù)》專欄·第4篇
文?| 段洵??
2759字 | 10?分鐘閱讀
【數(shù)據(jù)科學與人工智能】已開通Python語言社群,學用Python,玩弄數(shù)據(jù),求解問題,以創(chuàng)價值。喜樂入群者,請加微信號shushengya360,或掃描文末二維碼,添加為好友,同時附上Python-入群。有朋自遠方來,不亦樂乎,并誠邀入群,以達相互學習和進步之美好心愿。
一、列表
(一)列表的概念
列表List是一個序列對象,是一個或多個數(shù)據(jù)的集合。比如,一個列表可以包含一個或多個字符串或數(shù)值元素;一個列表也可以包含一個或多個列表或元
組等元素。列表的數(shù)據(jù)是可變的 mutable),也就是說,列表的元素可以增加、修改、刪除等。
我們通常將列表的元素置于方括號中,比如列表['We','use','Python']由三個
字符串元素組成,而列表[1,2,3,4,5]由五個整數(shù)數(shù)字元素組成。
range(x,y)函數(shù)生成從x到y(tǒng)-1構成的整數(shù)列表。比如 range(1,6)生成列表[1,
2,3,4,5]。請看下面的代碼示例:
list1?=?range(1,?6)
for?i?in?list1:
????print(i,?'*',?i,?'=',?i?*?i)
當用逗號連接print()函數(shù)的參數(shù)時,其打印結果自動在參數(shù)間添加空格:
1?*?1?=?1
2?*?2?=?4
3?*?3?=?9
4?*?4?=?16
5?*?5?=?25
(二)列表下標
與字符串下標類似,我們可以在列表變量后面加[x:y],x,y為整數(shù),以訪問列表元素。列表下標從0開始,如1ist[0]返回列表it的第一個元素。
list[0:x]返回列表list的第一個至第x-1個元素;
list[x:y]返回列表list的第x個至第y-1個元素;
list[x:]返回列表lst的第x個至最后一個元素;
list[-1]返回列表list的最后一個元素。
我們來看下面的范例:
list1?=?range(1,?6)
print(list1[0])???????#?1
print(list1[-1])??????#?5
for?i?in?list1[0:2]:
????print(i)??????????#?print?1?and?2二、列表與字符串的相互轉換
在進行數(shù)據(jù)處理時,我們經常需要對列表數(shù)據(jù)和字符串數(shù)據(jù)進行相互轉換。
本小節(jié)我們討論列表和字符串數(shù)據(jù)相互轉換的常用函數(shù)。
若要將字符串轉換成列表,可以使用split()函數(shù)和list()函數(shù),其基本句法分別為:
string.split()
list(string)
示例:
str1?=?'??Life?is'
print(str1.split())???????#?['Life',?'is']
str2?=?'2013-10-06'
print(str2.split('-'))????#?['2013',?'10',?'06']
string?=?"Python"
print(list(string))
若要將列表轉換成字符串,可以使用join()函數(shù),其基本句法為:
'x'.join(list)
示例:
list1?=?['Life',?'is',?'short']
print(''.join(list1))???????????#?Lifeisshort
print('?'.join(list1))??????????#?Life?is?short
print('--'.join(list1))?????????#?Life--is--short
三、常用列表函數(shù)
(一)len()
len()函數(shù)是計算字符串長度的,即計算一個字符串中包含的字符數(shù)目。
示例:
str1?=?'''My?father's?family?name?being?Pirrip,?and?my?Christian?name?Philip,?my?infant?tongue?could?make?of?both?names?nothing?longer?or?more?explicit?than?Pip.?So,?I?called?myself?Pip,?and?came?to?be?called?Pip.'''
list1?=?str1.split()
print(len(list1))???????#?37
(二)append()
append()函數(shù)可以對某個列表增加新的元素。新增加的元素置于列表末尾。
示例1:假設我們現(xiàn)在需要將一個文本(如一首詩)的每一行前面加上一個流水序號。解決此問題的一個可能算法是,將詩文本讀入一個列表中,該列表的第一個元素是詩的第一行,其下標為0;列表的第二個元素是詩的第二行,其下標為1;余類推。因此,每一行前面所加的序號實際上是該列表元素下標數(shù)值+1,最后一行的序號是列表長度數(shù)值。請看下面的代碼。
#?add_line_number.py
#?this?is?to?add?a?line?number?to?each?line?of?a?text
file_in?=?open("../texts/poem.txt",?"r")
file_out?=?open("../poem2.txt",?"a")
list0?=?[]
for?line?in?file_in.readlines():
????list0.append(line)
list0_max?=?len(list0)
i?=?0
for?line?in?list0:
????if?i?????????line_out?=?str(i?+?1)?+?'\t'?+?line
????????file_out.write(line_out)
????????i?=?i?+?1
file_in.close()
file_out.close()
示例2:對一段話的文本單詞進行判斷,挑選長度大于或等于6的單詞。請看下面代碼。
str1?=?'''My?father's?family?name?being?Pirrip,?and?my?Christian?name?Philip,?my?infant?tongue?could?make?of?both?names?nothing?longer?or?more?explicit?than?Pip.?So,?I?called?myself?Pip,?and?came?to?be?called?Pip.'''
list1?=?str1.split()
list2?=?[]
for?word?in?list1:
????if?len(word)?>=?6:
????????list2.append(word)
print(list2)
(三)set()
set()函數(shù)是用來刪除重復元素的,但會將列表轉換為集合。
示例:
list3?=?['a',?'c',?'b',?'b',?'a']
print(set(list3))
print(list(set(list3)))????#將集合轉換成列表
(四)pop()
pop()函數(shù)表示刪除列表中的最后一個元素。
示例:
list3?=?['a',?'c',?'b',?'b',?'a']
list3.pop()
print(list3)???????????????????????#?['a',?'c',?'b',?'b']
list3.pop()
print(list3)???????????????????????#?['a',?'c',?'b']
(五)sorted()函數(shù)
sorted()函數(shù)可以對列表元素進行排序。
示例:
list3?=?[12,?1,?8,?5]
print(sorted(list3))???????#?[1,?5,?8,?12]
list4?=?['a',?'BB',?'Aa',?'ba',?'c',?'A',?'Cb',?'b',?'CC']
print(sorted(list4))????#?['A',?'Aa',?'BB',?'CC',?'Cb',?'a',?'b',?'ba',?'c']
(六)count()
count()函數(shù)對列表中某個元素出現(xiàn)的頻次進行計數(shù)。
示例:
list3?=?['a',?'c',?'b',?'b',?'a']
print(list3.count('a'))
四、列表相關文本處理實例
(一)制作詞表
寫代碼制作一個基于ge.txt文本的按字母順序排序的單詞表。要完成此任務,可進行如下操作:①逐行讀取文本,將每行字符串全部轉換成小寫,并按空格對字符串進行切分,將之轉換成一個單詞列表(lit1);②將列表(list)元素寫入一個空列表(ist0);③重復上述第一和第二步,直至將文本的所有單詞都寫入列表list0中;④刪除list0列表中的重復項,并存為一個新列表(list2);⑤對list列表中的元素按照字母順序排序,并存為一個新列表(list3);⑥將list3列表中的元素全部寫出到 ge_wordlist.txt中。
示例:
#?wordlist1.py
file_in?=?open("../texts/ge.txt",?"r")
file_out?=?open("../ge_wordlist.txt",?"a")
list0?=?[]?????????????????????????????#?an?empty?list?to?store?all?words?
for?line?in?file_in.readlines():???????#?read?in?all?lines?of?the?text
????line_new?=?line.lower()????????????#?change?line?into?lower?case
????list1?=?line_new.split()???????????#?split?the?line?into?words?by?space
????
????for?word?in?list1:
????????list0.append(word)?????????????#?append?the?words?into?list0
list2?=?list(set(list0))???????????????#?delete?repetitions?of?list0
list3?=?sorted(list2,?key?=?str.lower)?#?alphabeticall?sort?list2
for?word?in?list3:
????file_out.write(word?+?'\n')????????#?write?out?the?words
file_in.close()
file_out.close()
(二)顛倒單詞字母順序(回文詞)
示例:
w?=?"word"
w_length?=?len(w)???????????????#?length?of?the?word
index_end?=?w_length?-?1????????#?length?minus?1,?i.e.?index?of?the?word’?last?letter
w_new?=?[]
i?=?index_end
while?i?>=?0:
????w_new.append(w[i])?????????#?write?the?last?letter?into?the?w_new?list
????i?=?i?-?1??????????????????#?index?of?the?word’s?last?letter?but?1
print(''.join(w_new))
(三)刪除文本中的空段落
示例:
file_in?=?open("../texts/ge.txt",?"r")
file_out?=?open("../ge_compact.txt",?"a")
for?line?in?file_in.readlines():
????if?not?line.isspace():????????#isspace()函數(shù)可判斷一個字符串是否僅由換行符、空白或制表符等字符組成。
????????file_out.write(line)
file_in.close()
file_out.close()推薦閱讀:
? ? ? ? ? ??? ?
公眾號推薦
數(shù)據(jù)思踐
數(shù)據(jù)思踐公眾號記錄和分享數(shù)據(jù)人思考和踐行的內容與故事。
Python語言群
誠邀您加入
請掃下方二維碼加我為好友,備注Python-入群。有朋自遠方來,不亦樂乎,并誠邀入群,以達相互學習和進步之美好心愿。
