像计算机科学家一样 思考Python 12.元祖
若要新建只包含一个元素的元祖,需要在后面添加一个逗号
也可以使用tuple函数,不能修改元祖的元素
内置函数divmod接收两个参数,并返回两个值的元祖,即商和余数,可以将结果存为一个元祖
import random
def make_histogram(word):
hist = {}
for i in word:
hist[i] = hist.get(i,0) + 1
return hist
def most_frequent(word):
s = make_histogram(word)
t = []
for x,fqre in s.iteritems():
t.append((fqre,x))
t.sort(reverse = True)
res = []
for fqre,x in t:
res.append(x)
return res
def read_file(filename):
return open(filename).read()
if __name__ == '__main__':
t = read_file('words.txt')
s = most_frequent(t)
for x in s:
print x
def signature(s):
t = list(s):
t.sort()
t = ''.join(t)
return t
def all_aragrams(filname):
d ={}
for line in open(filname):
word = line.strip().lower()
t = signature(word)
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d
def print_anagram_set_in_order(d):
t = []
for v in d.values():
if len(v) > 1:
t.append((len(v),v))
t.sort()
for x in t :
print x
def filter_length(d,n):
res = {}
for word,anagrams in d.iteritems():
if len(word) == n:
res[word] = anagrams
return res
if __name__ == '__main__':
d = all_aragrams('words.txt')
print_anagram_set_in_order(d)
eight_letters = filter_length(d, 8)
print_anagram_sets_in_order(eight_letters)
from anagram_sets import *
def metathesis_pair(d):
for anagrams in d.itervalues():
for word1 in anagrams:
for word2 in anagrams:
if word1 < word2 and word_distance(word1,word2) == 2:
print word1,word2
def word_distance(word1,word2):
assert len(word1) == len(word2)
count = 0
for c1,c2 in zip(word1,word2):
if c1 != c2:
count += 1
return count
if __name__ == '__main__':
d = all_anagrams('words.txt')
metathesis_pair(d)
def make_word_dict():
"""Reads the words in words.txt and returns a dictionary
that contains the words as keys."""
d = dict()
fin = open('words.txt')
for line in fin:
word = line.strip().lower()
d[word] = word
# have to add single letter words to the word list;
# also, the empty string is considered a word.
for letter in ['a', 'i', '']:
d[letter] = letter
return d
memo = {}
memo[''] = ['']
def is_reducible(word,word_dict):
if word in memo:
return memo[word]
res = []
for child in children(word,word_dict):
t = is_reducible(child,word_dict)
if t:
res.append(child)
memo[word] = res
return res
def children(word,word_dict):
res = []
for i in range(len(word)):
child = word[:i] +word[i+1:]
if child in word_dict:
res.append(child)
return res
def all_reducible(word_dict):
res = []
for word in word_dict:
t = is_reducible(word,word_dict)
if t != []:
res.append(word)
return res
def print_trail(word):
if len(word) == 0 :
return
print word,
t = is_reducible(word,word_dict)
print_trail(t[0])
def print_longest_words(word_dict):
words = all_reducible(word_dict)
t = []
for word in words:
t.append((len(word),word))
t.sort(reverse = True)
for length,word in t[0:5]:
print_trail(word)
print '\n'
if __name__ == '__main__':
word_dict = make_word_dict()
print_longest_words(word_dict)
还是不太会!
也可以使用tuple函数,不能修改元祖的元素
内置函数divmod接收两个参数,并返回两个值的元祖,即商和余数,可以将结果存为一个元祖
import random
def make_histogram(word):
hist = {}
for i in word:
hist[i] = hist.get(i,0) + 1
return hist
def most_frequent(word):
s = make_histogram(word)
t = []
for x,fqre in s.iteritems():
t.append((fqre,x))
t.sort(reverse = True)
res = []
for fqre,x in t:
res.append(x)
return res
def read_file(filename):
return open(filename).read()
if __name__ == '__main__':
t = read_file('words.txt')
s = most_frequent(t)
for x in s:
print x
def signature(s):
t = list(s):
t.sort()
t = ''.join(t)
return t
def all_aragrams(filname):
d ={}
for line in open(filname):
word = line.strip().lower()
t = signature(word)
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d
def print_anagram_set_in_order(d):
t = []
for v in d.values():
if len(v) > 1:
t.append((len(v),v))
t.sort()
for x in t :
print x
def filter_length(d,n):
res = {}
for word,anagrams in d.iteritems():
if len(word) == n:
res[word] = anagrams
return res
if __name__ == '__main__':
d = all_aragrams('words.txt')
print_anagram_set_in_order(d)
eight_letters = filter_length(d, 8)
print_anagram_sets_in_order(eight_letters)
from anagram_sets import *
def metathesis_pair(d):
for anagrams in d.itervalues():
for word1 in anagrams:
for word2 in anagrams:
if word1 < word2 and word_distance(word1,word2) == 2:
print word1,word2
def word_distance(word1,word2):
assert len(word1) == len(word2)
count = 0
for c1,c2 in zip(word1,word2):
if c1 != c2:
count += 1
return count
if __name__ == '__main__':
d = all_anagrams('words.txt')
metathesis_pair(d)
def make_word_dict():
"""Reads the words in words.txt and returns a dictionary
that contains the words as keys."""
d = dict()
fin = open('words.txt')
for line in fin:
word = line.strip().lower()
d[word] = word
# have to add single letter words to the word list;
# also, the empty string is considered a word.
for letter in ['a', 'i', '']:
d[letter] = letter
return d
memo = {}
memo[''] = ['']
def is_reducible(word,word_dict):
if word in memo:
return memo[word]
res = []
for child in children(word,word_dict):
t = is_reducible(child,word_dict)
if t:
res.append(child)
memo[word] = res
return res
def children(word,word_dict):
res = []
for i in range(len(word)):
child = word[:i] +word[i+1:]
if child in word_dict:
res.append(child)
return res
def all_reducible(word_dict):
res = []
for word in word_dict:
t = is_reducible(word,word_dict)
if t != []:
res.append(word)
return res
def print_trail(word):
if len(word) == 0 :
return
print word,
t = is_reducible(word,word_dict)
print_trail(t[0])
def print_longest_words(word_dict):
words = all_reducible(word_dict)
t = []
for word in words:
t.append((len(word),word))
t.sort(reverse = True)
for length,word in t[0:5]:
print_trail(word)
print '\n'
if __name__ == '__main__':
word_dict = make_word_dict()
print_longest_words(word_dict)
还是不太会!
还没人转发这篇日记