识别句子中单个单词的程序,将这些单词存储在列表中,并用列表中该单词的位置替换每个单词

时间:2016-04-20 20:28:23

标签: python string python-3.x

我正在开发一个程序,用于识别句子中的单个单词,将这些单词存储在列表中,并将原始句子中的每个单词替换为该单词在列表中的位置,以便可以从这些单词的位置重新创建句子。使用序列1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5列出此列表中的单词。到目前为止,我的代码如下,但我需要一些关于如何使其更有效和更短的建议:

import time

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split() 
another = [0]
time.sleep(0.5)
print(sentence)
for count, i in enumerate(s): 
    if s.count(i) < 2:
        another.append(max(another) + 1)
    else:
        another.append(s.index(i) +1)
another.remove(0)
time.sleep(0.5)
print(another)

4 个答案:

答案 0 :(得分:2)

这是一个线性算法:

position = {} # word -> position
words = sentence.split()
for word in words:
    if word not in position: # new word
       position[word] = len(position) + 1 # store its position
print(*map(position.__getitem__, words), sep=",")
# -> 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5

print()调用使用Python 3 *语法来解包map()返回的结果,该结果返回此处相应单词的位置。见What does ** (double star) and * (star) do for parameters?

答案 1 :(得分:1)

要获取sentence中的单词位置列表,并从此列表中重新创建原始句子:

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
recreated = [s[i-1] for i in positions]
# the reconstructed sentence
print(" ".join(recreated))
# the list of index numbers/word positions
print(positions)
# the positions as a space separated string of numbers
print(" ".join(positions)

列表为零索引,因此第一个元素是索引0,而不是1.如果您希望从1开始,您可以在列表解析中为所有索引添加1。

要获得与脚本产生完全相同的输出:

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
print(sentence)
print(positions)

输出:

ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]

答案 2 :(得分:1)

sentence = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
words = sentence.split()

# Counting backwards through the words means the last seen one will have 
# the lowest index without needing 'if' tests or incrementing counters.
positions = {word:index for index, word in reversed(list(enumerate(words, 1)))}

print(' '.join(str(positions.get(word)) for word in words))

在repl.it上尝试:https://repl.it/CHvy/0

答案 3 :(得分:0)

效率不高,但有两行。

words = sentence.split()
positions = [words.index(word) + 1 for word in words]

请注意,list.index(entry)将返回第一次出现entry的索引。如果您对基于0的索引没有问题,那么以下内容非常简洁:

positions = list(map(words.index, words))