试图用句子中的位置替换列表中的单词

时间:2016-12-13 11:53:02

标签: python

from collections import OrderedDict
sentence= ("ask not what your country can do for you ask what you can do for your country").lower()
words = sentence.split(' ')

lst = list(OrderedDict.fromkeys(words))
print(lst)
print(words)

在这段代码中,我将句子中的单词分开,并将其列入句子中出现的单个单词列表中。但是,我希望进一步做的是如何用句子中出现的位置替换列表中的单词。我很难过,很喜欢一些帮助。谢谢:D

例如,所需的输出将在lst变量中,列表将是:

['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you']

或许在另一个变量下,我希望点亮的东西能像:

['0', '1', '2', '3', '4', '5', '6', '7', '8']
因此,在整个句子下:

"ask not what your country can do for you ask what you can do for your country"

它看起来像这样:

['0', '1', '2', '3', '4', '5', '6', '7', '8','0','2','8','4','5','6','3','4']

5 个答案:

答案 0 :(得分:4)

假设您要查找句子中所有唯一单词的所有位置索引,您可以生成dict,如下所示:

import pprint
sentence = ('ask not what your country can do for you ask what you can do for your country').lower()
words = sentence.split(' ')

# Use a dict and map all indices to each unique word
words_ix = {w: [] for w in set(words)}
for ix, w in enumerate(words):
    words_ix[w].append(ix)
pprint.pprint(words_ix)

# Use a list and collect the index of the first occurrence of each word
words_px = [words.index(w) for w in words]
pprint.pprint(words_px)

收率:

{'ask': [0, 9],
 'can': [5, 12],
 'country': [4, 16],
 'do': [6, 13],
 'for': [7, 14],
 'not': [1],
 'what': [2, 10],
 'you': [8, 11],
 'your': [3, 15]}

[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 2, 8, 5, 6, 7, 3, 4]

选择适合您需求的任何解决方案。

答案 1 :(得分:0)

如果您不明确需要OrderedDict,则只需使用setindex即可轻松完成。

sentence = ("ask not what your country can do for you ask what you can do for your country").lower()
words = sentence.split(' ')

pos_dict = {}

for word in set(words):
    pos_dict[word] = words.index(word)

print pos_dict

我们创建一个空字典,然后遍历句子中找到的set个唯一单词。然后我们使用index在我们创建的初始列表中查找单词的位置,以找到它的第一次出现。

编辑:自编辑问题以来,有一个很好的单行来获得结果:

sentence = ("ask not what your country can do for you ask what you can do for your country").lower()
words = sentence.split(' ')

word_pos = [words.index(word) for word in words]

print word_pos

返回[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 2, 8, 5, 6, 7, 3, 4]

答案 2 :(得分:0)

尝试以下代码:

from collections import OrderedDict
sentence= ("ask not what your country can do for you ask what you can do for your country").lower()
words = sentence.split(' ')

lst = list(OrderedDict.fromkeys(words))
numberLst = []
for word in words:
    # print lst.index(word)
    numberLst.append(lst.index(word))

print(words)
print numberLst # numberLst is the output that you want

答案 3 :(得分:0)

也许它会对你有所帮助:

MediaScannerConnection.scanFile(HomeActivity.this,
                            new String[] {imageFile.getAbsolutePath()},
                            new String[] {"image/jpeg"},null);

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));

答案 4 :(得分:0)

此版本将连续数字(从零开始)分配给每个新单词。它使用普通字典来跟踪已经看到的单词,我们可以简单地使用字典的当前大小作为每个新单词的索引号。

sentence = "ask not what your country can do for you ask what you can do for your country"

d = {}
lst = []
words = sentence.lower().split()
for w in words:
    if w in d:
        i = d[w]
    else:
        d[w] = i = len(d)
    print(i, w)
    lst.append(i)

print(lst)

<强>输出

0 ask
1 not
2 what
3 your
4 country
5 can
6 do
7 for
8 you
0 ask
2 what
8 you
5 can
6 do
7 for
3 your
4 country
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 2, 8, 5, 6, 7, 3, 4]

请注意,单词的i个数字不是句子中第一次出现的单词的索引。如果您真的想要,可以使用

lst = [words.index(w) for w in words]

正如jbndl在评论中所说的那样。

看看在将所有独特单词添加到词典之前重复单词时会发生什么:

sentence = "ask not what your country can not do for you"    

<强>输出

0 ask
1 not
2 what
3 your
4 country
5 can
1 not
6 do
7 for
8 you
[0, 1, 2, 3, 4, 5, 1, 6, 7, 8]