>>> line = fin.readline()# strip()默认移除前后的空格字符, \n很特殊属于空格字符, 所以不需要 str.strip('\n').>>> word = line.strip()>>> word
'aahed'
你也可以在for循环中使用文件对象.下面的代码读入words.txt并每行打印出一个单词:
fin =open(r'C:\Users\13600\Desktop\words.txt')
count =0# for循环遍历得到的对象line, 等同于readline()得到的值.for line in fin:
word = line.strip()
count = count +1print(word)print(count)# 113 783个单词.
9.2 练习
在下一节里有这些练习的解答.在继续阅读解答之前应当至少尝试一些每一个练习.
1. 练习1
编程一个程序,读入words.txt并且打印出那些长度超出20个字符的单词(不算空白字符).
fin =open(r'C:\Users\13600\Desktop\words.txt')for line in fin:
word = line.strip()iflen(word)>20:print(word)
prohibit_character =input('输入禁止的字母>>>:')defavoids(word, prohibit_letter):for letter in prohibit_letter:if letter in word:returnFalsereturnTrue
fin =open(r'C:\Users\13600\Desktop\words.txt')
prohibit_words_num =0# 遍历文件对象.for line in fin:
line = line.strip()
is_prohibit_words = avoids(line, prohibit_character)if is_prohibit_words:
prohibit_words_num = prohibit_words_num +1print('不包含任何禁止字母的单词个数为:', prohibit_words_num)
能不能找到5个禁止字母的组合,它们排除的单词最少?(计算出所有单词字母出现最少的,把他们禁止掉).
fin =open(r'C:\Users\13600\Desktop\words.txt')# fin.read() 读取所有数据.
all_data = fin.read()for i inrange(97,123):print(chr(i), all_data.count(chr(i)))
a 68574
b 17794
c 34281
d 34548
e 106752
f 12706
g 27832
h 20186
i 77392
j 1780
k 9366
l 47003
m 24739
n 60505
o 54538
p 25789
q 1632
r 64963
s 86526
t 57029
u 31151
v 9186
w 8533
x 2700
y 13473
z 3750
defuser_only(word1, word2):# 遍历第一个单词的元素.for i in word1:# 单词的元素不在字母序列中, 返回False.if i notin word2:returnFalsereturnTrueprint(user_only('hello','eloh'))# Trueprint(user_only('helloA','eloh'))# False
defuser_all(word1, word2):for letter in word2:if letter notin word1:returnFalsereturnTrueprint(user_all('hellohello','helo'))# True
defuser_all(word1, word2):for letter in word2:if letter notin word1:returnFalsereturnTrue
fin =open(r'C:\Users\13600\Desktop\words.txt')
count =0for line in fin:
word = line.strip()
is_qualified = user_all(line,'aeiou')if is_qualified:
count = count +1print(count)# aeiou 的结果为 598# aeiouy 的结果为 42
# 字符串, 必须出现的字母 组.defuses_only(word, available):for letter in word:# not in 字母不在这个字母组中.if letter notin available:returnFalsereturnTrueprint(uses_only('hello','helo'))# True
defuses_only(word, available):for letter in word:# not in 字母不在这个字母组中.if letter notin available:returnFalsereturnTruedefuses_all(word, required):return uses_only(required, word)print(uses_all('hello','h'))# True
defis_abecedarian(word):
previous = word[0]for c in word:if c < previous:returnFalse
previous = c
returnTrueprint(is_abecedarian('abc'))# Trueprint(is_abecedarian('aabbcc'))# Trueprint(is_abecedarian('acb'))# False
defis_abecedarian(word):
i =0while i <len(word)-1:if word[i +1]< word[i]:returnFalse
i = i +1returnTrueprint(is_abecedarian('abc'))# Trueprint(is_abecedarian('aabbcc'))# Trueprint(is_abecedarian('acb'))# False
# 新版本.defis_palindrome(word):
i =0
j =len(word)-1# 结束的条件为 i = jwhile i < j:if word[i]!= word[j]:returnFalse
i = i +1
j = j -1returnTrueprint(is_palindrome('allen'))# Falseprint(is_palindrome('bob'))# True
或者,我们可以将其回归到已解决的问题,可能这么写:(使用8-11调试中的is_reverse)
# 修改正确的is_reverse函数.defis_reverse(word1, word2):iflen(word1)!=len(word2):returnFalse
i =0
j =len(word2)-1while j >=0:if word1[i]!= word2[j]:returnFalse
i = i +1
j = j -1returnTruedefis_palindrome(word):print(is_reverse(word, word))
is_palindrome('allen')# False
is_palindrome('bob')# True
defis_triple_double(word):
i =0
letter_num =len(word)-2
double_num =0while i <= letter_num:
j = i +1print('True', i, j)if word[i]== word[j]:# 但比较出一组相同的字符后, 比较它后面两组的字符是否相同.
double_num = double_num +1
i = i +2if double_num ==3:return word
else:# i = i + 1 减去2 * double_num, 程序末尾详细解答.
i = i +1-2* double_num
print('False', i)# 重置计算器
double_num =0deffind_triple_double():"""读取单词列表并打印带有三个双字母的单词."""
fin =open(r'C:\Users\13600\Desktop\words.txt')for line in fin:
word = line.strip()if is_triple_double(word):print(word)print('三个连续的双字母:')
find_triple_double()"""
b o o k k a e e p e r
0 1 2 3 4 5 6 7 8 9 10
第一次比较: i = 0, j = 1, 比较的结果为False, double_num = 0, 执行 i = 0 + 1 - (2 * 0), i为1.
第二次比较: i = 1, j = 2, 比较的结果为True, double_num = 1, 执行 i = 1 + 2, i为3.
第三次比较: i = 3, j = 4, 比较的结果为True, double_num = 2, 执行 i = 3 + 2, i为5.
第四次比较: i = 5, j = 6, 比较的结果为False, double_num = 2, 执行 i = 5 + 1 - 2 * 2, i为2,
double_num重置为0.
b o o o k k e e p e r
0 1 2 3 4 5 6 7 8 9 10
当 i = 1, j = 2 比较时, 计较结果为True. i 加 2.
当 i = 3, j = 4 比较时, 计较结果为False, 可2和3还是连续的, 所以将i设置为合适的值.
找到合适的规律,
第一次找到两个重复的单词并直接去判断后两组单词是否为重复的.
重复被打断了的后, 就回到第一次重复的的单词的后一个字母处, 让他与自己相邻的(后一个)字母做比较.
"""