本文目錄一覽:
Python中的9個代碼小實例!
1、 串聯比較
2、串聯函數調用
3、複製列表
4、字典獲取元素值
5、 按值排序字典
6、 For Else
7、列錶轉換為逗號分隔的字符串
8、 合併字典
9、尋找列表中最大和最小元素的索引
若有不明白的地方,請移步Python視頻教程繼續學習!!
求解 python 練習題
import string
def makeWordList(input_file, output_file):
table = string.maketrans(“”, “”)
try:
word_list = dict()
for line in open(input_file, ‘r’):
line = line.translate(table, string.punctuation).rstrip(‘\r\n’).split(‘ ‘)
for word in line:
if not word in word_list:
word_list[word] = 1
else:
word_list[word] += 1
f = open(output_file, ‘w’)
for k,v in word_list.items():
line = ‘%s %s\r\n’ % (k,v)
f.write(line)
except:
print input_file,’not exist’
makeWordList(‘input.txt’, ‘output.txt’)
推薦幾個適合新手練手的Python項目
《Python實戰:四周實現爬蟲系統》百度網盤免費下載
鏈接:
提取碼: xbdu
Python實戰:四周實現爬蟲系統
原創文章,作者:AW9MD,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130450.html