如何创建使用其他两个列表的索引和项创建的列表?

时间:2016-03-13 16:02:56

标签: python list

我有两个清单。一个由句子中的位置组成,另一个由构成句子的单词组成。我想使用poslist和wordlist重新创建变量句子。

recreate = []
sentence = "This and that, and this and this."
poslist = [1, 2, 3, 2, 4, 2, 5]
wordlist = ['This', 'and', 'that', 'this', 'this.']

我想使用for循环来浏览poslist,如果poslist中的项目等于wordlist中单词的位置,它会将它附加到新列表,重新创建原始列表。我的第一次尝试是:

for index in poslist:
    recreate.append(wordlist[index])
print (recreate)

我必须使列表字符串将列表写入文本文件。当我尝试再次拆分它们并使用上面显示的代码时,它不起作用。它说索引需要是不在列表中的切片或整数或切片。我想解决这个问题。谢谢。

单词列表使用:

sentence = input("Enter a sentence >>") #asking the user for an input
sentence_lower = sentence.lower() #making the sentence lower case
wordlist = [] #creating a empty list
sentencelist = sentence.split() #making the sentence into a list

for word in sentencelist: #for loop iterating over the sentence as a list
    if word not in wordlist: 
        wordlist.append(word)

txtfile = open ("part1.txt", "wt")
for word in wordlist:
    txtfile.write(word +"\n")
txtfile.close()

txtfile = open ("part1.txt", "rt")
for item in txtfile:
    print (item)
txtfile.close()
print (wordlist)

这些职位的使用是:

poslist = []

textfile = open ("part2.txt", "wt")
for word in sentencelist:
    poslist.append([position + 1 for position, i in enumerate(wordlist) if i == word])

print (poslist)
str1 = " ".join(str(x) for x in poslist)

textfile = open ("part2.txt", "wt")
textfile.write (str1)
textfile.close()

4 个答案:

答案 0 :(得分:1)

列表是0索引的(第一项具有索引0,第二项是索引1,...),因此如果要使用" human"则必须从索引中减去1。 poslist中的索引:

for index in poslist:
    recreate.append(wordlist[index-1])
print (recreate)

然后,您可以再次将它们粘合在一起并将它们写入文件:

with open("thefile.txt", "w") as f:
    f.write("".join(recreate))

答案 1 :(得分:1)

首先,您的代码可以简化为:

sentence = input("Enter a sentence >>") #asking the user for an input
sentence_lower = sentence.lower() #making the sentence lower case
wordlist = [] #creating a empty list
sentencelist = sentence.split() #making the sentence into a list

with open ("part1.txt", "wt") as txtfile:
    for word in sentencelist: #for loop iterating over the sentence as a list
        if word not in wordlist: 
            wordlist.append(word)
            txtfile.write(word +"\n")

poslist = [wordlist.index(word) for word in sentencelist]
print (poslist)

str1 = " ".join(str(x) for x in poslist)
with open ("part2.txt", "wt") as textfile:
    textfile.write (str1)

在原始代码中,poslist是列表而不是整数列表。

然后,如果你想从poslist(现在是一个int的列表,而不是你提供的代码中的列表列表)和wordlist重建你的句子,你可以执行以下操作:

sentence = ' '.join(wordlist[pos] for pos in poslist)

答案 2 :(得分:0)

您也可以使用生成器表达式和字符串join方法:

sentence = ' '.join(wordlist[pos-1] for pos in poslist if pos if pos <= len(wordlist))
# 'This and that, and this and this.'

答案 3 :(得分:0)

您可以使用operator.itemgetter()

from operator import itemgetter

poslist = [0, 1, 2, 1, 3, 1, 4]
wordlist = ['This', 'and', 'that', 'this', 'this.']

print(' '.join(itemgetter(*poslist)(wordlist)))

请注意,我必须从poslist中的所有项中减去一项,因为Python是零索引语言。如果您需要以编程方式更改poslist,则可以在声明后立即执行poslist = (n - 1 for n in poslist)