【问题标题】:Finding the original positions of words in a sentence when the word occurs more than once当单词出现不止一次时,找到单词在句子中的原始位置
【发布时间】:2016-07-05 17:10:28
【问题描述】:

我需要找到用户输入的句子中单词的位置,如果单词出现不止一次,则只打印第一次出现的单词

到目前为止我有代码-

sentence=input("Enter a sentence: ")
sentence=sentence.lower()
words=sentence.split()
place=[]

for c,a in enumerate(words):
    if words.count(a)>2 :
        place.append(words.index(a+1))
    else:
        place.append(c+1)

print(sentence)
print(place)

但它会打印句子中单个单词的位置,而不是重复出现多次的单词的原始位置

谁能帮我解决这个问题???

【问题讨论】:

  • 你的问题不清楚;也许一两个例子会有所帮助。

标签: python


【解决方案1】:

如果您使用的是 python 2,则使用 raw_input 而不是 input 否则它将进行评估。这不是问题,只是一个观察结果(那时您可能正在使用 python 3,所以我将保持这种状态)。

您可以创建一个字典来跟踪找到的字数和位置。这基本上是列表的字典。 dict 是单词到位置列表的映射。

sentence=input("Enter a sentence: ")
sentence=sentence.lower()
words=sentence.split()

place={}
for pos, word in enumerate(words):
    try:
        place[word].append(pos)
    except KeyError:
        place[word] = [pos] 

print(sentence)
print(place)

另外,如果你想在句子解析方面做一些更高级的事情,你可以这样做:

import re
words = re.split('\W+',sentence)

基本上使用所有非字母数字(逗号、冒号等)作为拆分。请注意,您可以通过这种方式获得一个空白条目(可能在最后)。

【讨论】:

  • raw_input 在 python3 中被删除。
  • @Arnial 啊,我还在使用 2.7。我会更新的。很快有一天我会与时俱进。谢谢:-)
  • @woot,我认为 OP 想要一个类似于我答案中的输出。 >>>*但它会打印句子中单个单词的位置,而不是重复出现不止一次的单词的原始位置*。你怎么看?
  • 哦,我想我明白了。而不仅仅是第一个位置...打印唯一的单词和它们所在的位置列表。这很容易,我编辑了它。我认为它足够灵活,可以取出或添加类似的小东西。
  • 虽然有位置列表,但计数是多余的,所以我想我会删除它
【解决方案2】:

您的代码需要一些修改才能实现您想要做的事情:

  • if words.count(a)>2 :应该是if words.count(a)>1,因为如果单词重复,count 会大于 1。

  • place.append(words.index(a+1)):应该是place.append(words.index(a)+1),因为你想找到a的索引,然后给它加1。

根据建议修改的代码:

sentence=input("Enter a sentence: ")

sentence=sentence.lower()
words=sentence.split()
place=[]


for c,a in enumerate(words):
    if words.count(a)>1 :
        place.append(words.index(a)+1)
    else:
        place.append(c+1)

print(sentence)
print(place)

输出:

Enter a sentence: "hello world hello people hello everyone"
hello world hello people hello everyone
[1, 2, 1, 4, 1, 6]

【讨论】:

    【解决方案3】:

    拆分字符串

    >>> s = '''and but far and so la ti but'''
    >>> s = s.split()
    >>> s
    ['and', 'but', 'far', 'and', 'so', 'la', 'ti', 'but']
    

    使用set查找唯一词,使用list.index方法查找每个唯一词的第一个位置。

    >>> map(s.index, set(s))
    [0, 5, 2, 1, 4, 6]
    

    zip 将结果与唯一的单词关联起来,将单词与其位置相关联。

    >>> zip(set(s),map(s.index, set(s)))
    [('and', 0), ('la', 5), ('far', 2), ('but', 1), ('so', 4), ('ti', 6)]
    >>> 
    

    我想列表理解可能更容易阅读;

    >>> s = '''and but far and so la ti but'''
    >>> s = s.split()
    >>> result = [(word, s.index(word)) for word in set(s)]
    >>> result
        [('and', 0), ('la', 5), ('far', 2), ('but', 1), ('so', 4), ('ti', 6)]
    >>>
    

    按位置排序

    >>> import operator
    >>> position = operator.itemgetter(1)
    >>> result.sort(key = position)
    >>> result
    [('and', 0), ('but', 1), ('far', 2), ('so', 4), ('la', 5), ('ti', 6)]
    >>> 
    

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多