b.txt文件中內(nèi)容:When you are starting the selenium server, open a console in the directory with chromedriver and selenium server and execute the above command
終稿如下:
file = 'b.txt'
with open(file) as f:
line = f.read() # 讀取文件,為一個(gè)字符串
str = []
outstr = {}
for x in line:
if x.isalpha():# 判斷是否為字母,
if x in outstr:# 判斷這個(gè)字母是否添加到字典
outstr[x] += 1# 添加到字典了,則值加一
else:
outstr[x] = 1 #若沒有在字典,則追加一個(gè)鍵值對到字典
print(outstr)
############################################################
初稿如下:
file = 'b.txt'
with open(file) as f:
line = f.read() # 讀取文件,為一個(gè)字符串
str = []
outstr = {}
for x in line:
if x.isalpha():# 判斷是否為字母,是字母再加入到str列表中去
str.append(x)
for j in range(len(str)-1,0,-1):
# for i in range(0,j):
for j in range(0,len(str)-1):
if str[j] in outstr:
#if str[i] == str[j]:
outstr[str[j]] += 1
else:
outstr[str[j]] = 1
print(str)
print(outstr)
###################################################################################################################################
大神稿如下:
letters = {}
with open('b.txt') as f:
for letter in f.read():
'''判斷是否是字母'''
if letter.islower():
'''判斷是否存在元組中'''
if letters.get(letter):
'''是將數(shù)量+1'''
letters[letter] += 1
else:
'''第一次加入,賦值為1'''
letters[letter] = 1
print(letters)
單詞版如下:
file = 'b.txt'
with open(file) as f:
line = f.read().split(' ')
outstr = {}
str = []
for x in line:
str.append(x)
for one in line:
a = str.count(one)
outstr[one] = a
print(outstr)
###################################################################################################
簡單粗暴版如下:
file = 'b.txt'
with open(file) as f:
line = f.read().replace(' ','')
str = []
for x in line:
str.append(x)
outstr = {}
for one in str:
a = str.count(one)
outstr[one] = a
print(str)
print(outstr)
##########################################