Hacking Secret Ciphers with Python

(Ann) #1

342 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


The extend() List Method


The extend() list method is very similar to the append() list method. While the append()
method adds a single value passed to it to the end of the list, the extend() method will add
every item in a list argument to the end of the list. Try typing the following into the interactive
shell:





spam = []
eggs = ['cat', 'dog', 'mouse']
spam.extend(eggs)
spam
['cat', 'dog', 'mouse']
spam.extend([1, 2, 3])
spam
['cat', 'dog', 'mouse', 1, 2, 3]





Notice the difference if you pass a list to the append() list method. The list itself gets appended
to the end instead of the values in the list:





spam = []
eggs = ['cat', 'dog', 'mouse']
spam.append(eggs)
spam
[['cat', 'dog', 'mouse']]
spam.append([1, 2, 3])
spam
[['cat', 'dog', 'mouse'], [1, 2, 3]]





vigenereHacker.py
117. # See getMostCommonFactors() for a description of seqFactors.
118. seqFactors = {}
119. for seq in repeatedSeqSpacings:
120. seqFactors[seq] = []
121. for spacing in repeatedSeqSpacings[seq]:
122. seqFactors[seq].extend(getUsefulFactors(spacing))


While repeatedSeqSpacings is a dictionary that maps sequence strings to lists of integer
spacings, we actually need a dictionary that maps sequence strings to lists of factors of those
integer spacings. Lines 118 to 122 do this.

Free download pdf