我是絕對新的python和編程,我做了這個bifid cipher,我想聽取關於如何改進和使它看起來更優雅的意見,在此先感謝。讓這段代碼看起來更優雅
我一直在學習Codecademy和Udacity的課程,並且我學到了很多東西。
import itertools
#Genera coodernadas-Generate Coordinates
coordinates = [[x,y] for x in range(1,6) for y in range(1,6)]
#Genera alfabeto-Generate Alphabet
alfa = []
for i in range(97,123):
alfa.append(chr (i))
alfa.remove("i")
#Genera diccionario de coordenadas y alfabeto - Generate dictionary and coordinates alphabet
alfacor = {}
alfacor = dict(zip(alfa,coordinates))
#Leer Txt - Read txt
document = open("Z:\\R\\Desktop\\BIFIDO\\easy.txt")
contenido = document.read()
print (contenido)
document.close()
#Encripta fase1 - Get's coordinates of txt
encripta = []
for e in contenido:
encripta.append(alfacor[e])
#Unir lista encripta - Merge content of encropita in a new list
merged = list(itertools.chain.from_iterable(encripta))
#Divido lista merge en partes iguales - Divide meged list to get new coordinates
B = merged[:len(merged)/2]
C = merged[len(merged)/2:]
#Unir B y C - Zip B and C to get a new list of coordinates
zipped = zip(B,C)
#Make a new list from zipped to convert from tuple to list
final_list = [list(elem) for elem in zipped]
#Convert contect of alfacor to tuples
inv_alfacor = {}
for letter, coordinate in alfacor.iteritems():
inv_alfacor[tuple(coordinate)] = letter
#Substitude coordinates of final_list from elements of inv_alfacor
encripta_f = []
for element in final_list:
element = tuple(element)
if element in inv_alfacor:
encripta_f.append(inv_alfacor[element])
print "Tu palabra ",encripta_f
這個問題看起來更切合主題爲[代碼審查(HTTP ://codereview.stackexchange.com/help/on-topic)。詢問關於代碼風格的觀點在這裏是無關緊要的。 – Radiodef
感謝您的提示,不知道它,將在未來的職位上想到。 – etsous