解释如何将数字列表转换回单词以构成原始数组

时间:2017-02-17 11:27:45

标签: python

import re
sentence=input('enter sentence')
punctuation=(re.findall(r"[\w'\"]+|[.,!?;:_-]", sentence))
print(punctuation)`enter code here`
positions = [punctuation.index(x) for x in punctuation]
print(positions)
test1=(", ".join(str(i) for i in positions))
words=" ".join(sorted(set(punctuation), key=punctuation.index))
print(words)
with open('1.txt', 'w') as f:#this creates the name of the file called task 2 which will contain my data ad allows the computer to locate the fle and add in the data. The 'w' makes the file writable so i can add in my data, without it displaying an error, using f as a variable.
     f.write(str(words))
with open('2.txt', 'w') as f:
     f.write(str(test1))
f.close()
openfile=input('what is your file name for the compressed sentence?')
openfile = open(openfile, "r")
print ( openfile.read())
openfile=input('what is your file name for the compressed positions?')
openfile = open(openfile, "r")
print ( openfile.read())

#这是将位置转回数字     openfile = open(' 1.txt'," r")

test=openfile.read()
#print(test)

blankarray=[]

for i in test.split(" "):
    blankarray.append(i)

#print(blankarray)


openfile.close

openfile = open('2.txt', "r")

test=openfile.read()
#print(test)

blankarray2=[]

for i in test.split(","):
    blankarray2.append(int(i))

print(blankarray2)
openfile.close


blankstring=""

for i in range(len(blankarray2)):
    #print(i)
    blankstring=blankstring+blankarray[blankarray2[i]] + " "
print(blankstring)

代码的第二部分将数字列表转换回单词以构成数组。但是,我真的不明白它是如何工作的。任何帮助都是有用的

0 个答案:

没有答案