如何为列表中的单词指定编号?

时间:2016-06-19 17:57:27

标签: python list

这是我目前的进展。我对Python有些陌生,而且我有点迷失。我真的不知道如何解决这个问题,如果标题有点误导,我道歉。我会尽力解释我的问题。

输出需要以列表形式显示:

"WHAT", "IS", "MINE", "IS", "YOURS", "AND", "WHAT", "IS", "YOURS", "IS", "MINE"

"1", "2", "3", "2", "4", "5", "1", "2", "4", "2", "3"

"1""WHAT""2""IS" ......依此类推。如果单词出现多次,则它将保持相同的数字。

代码

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE";

sentence = sentence.lower();

sentence = sentence.split();

uniqueWord = [];

store = [];

for i in sentence:
    if i not in uniqueWord:
        uniqueWord.append(i);

lengthOfUniqueWord = len(uniqueWord);

print(sentence);

print(uniqueWord);

for i in range(lengthOfUniqueWord):
    i = str(i+1);
    store.append(i);

print(store);

for positions in enumerate(uniqueWord, 1):
     print(positions);

输出

['what', 'is', 'mine', 'is', 'yours', 'and', 'what', 'is', 'yours', 'is', 'mine']
['what', 'is', 'mine', 'yours', 'and']
['1', '2', '3', '4', '5']
(1, 'what')
(2, 'is')
(3, 'mine')
(4, 'yours')
(5, 'and')

4 个答案:

答案 0 :(得分:1)

这应该有效:

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE";    
sentence = sentence.lower();
sentence = sentence.split();

uniqueWord = [];

for i in sentence:
    if i not in uniqueWord:
        uniqueWord.append(i);

for word in sentence:
    print uniqueWord.index(word) + 1

这是index

文档的链接

答案 1 :(得分:0)

虽然其他人也输入了答案;-)我想出了 - 希望它还包含有关Python编码的进一步提示:

#! /usr/bin/env python
from __future__ import print_function


def word_indexer(text):
    """Split the text in words maintaining order and return two
    aligned lists: 1) the words all in sequence, and 2) the matching
    unique 1-based case insensitive index (insert based rank)."""

    words_in_order = text.split()
    word_index = []
    unique_word_rank = {}
    rank = 1
    for word in words_in_order:
        normalized_word = word.lower()
        if normalized_word not in unique_word_rank:
            unique_word_rank[normalized_word] = rank
            rank += 1
        word_index.append(unique_word_rank[normalized_word])

    return words_in_order, word_index


def main():
    """Do the word indexing."""
    sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE"
    words_in_order, word_index = word_indexer(sentence)
    # print as in question in two lines:
    print(words_in_order)
    print(word_index)
    # to display like a table:
    for ndx, word in zip(word_index, words_in_order):
        print(ndx, word)

if __name__ == '__main__':
    main()

这在我的系统上提供(使用Python 2.7.11,但也使用Python 3.5.1运行):

['WHAT', 'IS', 'MINE', 'IS', 'YOURS', 'AND', 'WHAT', 'IS', 'YOURS', 'IS', 'MINE']
[1, 2, 3, 2, 4, 5, 1, 2, 4, 2, 3]
1 WHAT
2 IS
3 MINE
2 IS
4 YOURS
5 AND
1 WHAT
2 IS
4 YOURS
2 IS
3 MINE

希望这有帮助 - 并且快乐的黑客攻击!

答案 2 :(得分:0)

设置count迭代器,只有在dict d中插入新单词时才会递增。根据每个单词的匹配编号构建最终列表:

from itertools import count

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE"  
s =  sentence.split()
c = count(1)
d = {}

for word in s:
    if word.lower() not in d:
         d[word.lower()] = next(c)

result = [str(d[word.lower()]) for word in s]
# ['1', '2', '3', '2', '4', '5', '1', '2', '4', '2', '3']

答案 3 :(得分:0)

这是实现同一目标的另一种方法:

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE".lower().split()
uniqueWord = list(set(sentence))

print(sentence);
print(uniqueWord);

store = [uniqueWord.index(x) for x in sentence]

print(store);