引用列表中的项目

时间:2013-01-30 16:15:40

标签: python python-3.x

我有以下python代码:

import regex
original = " the  quick ' brown 1 fox! jumps-over the 'lazy' doG? !  "
s = [i for i in original.split(" ")]

我想编写一个名为get_sentence的函数,该函数接受s中的一个元素,并将该句子作为该元素所属的字符串返回。例如:

"brown" ->  "the  quick ' brown 1 fox!"

如果第一个“the”传递给函数,那么:

"the" -> the  quick ' brown 1 fox!"

如果第二个:

"the" -> "jumps-over the 'lazy' doG?"

作为这样一个函数的参数,你会传递什么?在C ++中,我可能会传入一个std :: vector :: const_iterator。在C中我会传入一个int(数组索引)或者甚至是一个指针。

4 个答案:

答案 0 :(得分:2)

>>> from itertools import product, chain
>>> #Assuming your original sentence is
>>> origional = " the  quick ' brown 1 fox! jumps-over the 'lazy' doG? !  "
>>> #Sentence terminators are
>>> sent_term = "[?!.;]"
>>> #I will use regex to split it into sentences
>>> re.split(sent_term, origional.strip())
["the  quick ' brown 1 fox", " jumps-over the 'lazy' doG", ' ', '']
>>> #And then split it as words
>>> #I could have used str.split, but that would include punctuations
>>> #Which you may not be interested
>>> #For each of the words, I create a mapping with the sentence using product
>>> word_map = ((product(re.split("\W",e),[e])) 
                 for e in re.split(sent_term, origional.strip()))
>>> #Chain it as a single list
>>> word_map = chain(*((product(re.split("\W",e),[e])) 
                        for e in re.split(sent_term, origional.strip())))
>>> from collections import defaultdict
>>> #Create a default dict
>>> words = defaultdict(list)
>>> #And populated all non trivial words
>>> for k, v in word_map:
    if k.strip():
        words[k]+=[v]


>>> words
defaultdict(<type 'list'>, {'brown': ["the  quick ' brown 1 fox"], 'lazy': [" jumps-over the 'lazy' doG"], 'jumps': [" jumps-over the 'lazy' doG"], 'fox': ["the  quick ' brown 1 fox"], 'doG': [" jumps-over the 'lazy' doG"], '1': ["the  quick ' brown 1 fox"], 'quick': ["the  quick ' brown 1 fox"], 'the': ["the  quick ' brown 1 fox", " jumps-over the 'lazy' doG"], 'over': [" jumps-over the 'lazy' doG"]})
>>> #Now to get the first word
>>> words['the'][0]
"the  quick ' brown 1 fox"
>>> #Now to get the second sentence
>>> words['the'][1]
" jumps-over the 'lazy' doG"

答案 1 :(得分:0)

我不完全确定我明白你要做什么,但你可能只是传递一个整数索引。您无法传递对the的引用,因为两者都完全相同。

答案 2 :(得分:0)

“Pythonic”的方式是建立一个字典,其中键是单词,值是句子,或者是键所属的句子列表。

lookup = {}
sentences = split_to_sentences(large_text)
for idx_sentence, sentence in enumerate(sentences):
    for word in split_to_words(sentence):
        if word in sentence:
            s = lookup.setdefault(word, set())
            s.add(idx_sentence)

现在lookup你有一个字典,每个单词都分配了它出现的句子索引。顺便说一句,你可以用一些非常好的列表推导来重写它。

答案 3 :(得分:0)

您可以使用字典索引来执行此操作:

import re
original = " the  quick ' brown 1 fox! jumps-over the 'lazy' doG? !  "

index={}

for sentence in re.findall(r'(\b.*?[.!?])',original):
    for word in re.findall(r'\w+',sentence):
        index.setdefault(word,[]).append(sentence)

print index

打印:

{'brown': ["the  quick ' brown 1 fox!"], 'lazy': ["jumps-over the 'lazy' doG?"], 'jumps': ["jumps-over the 'lazy' doG?"], 'fox': ["the  quick ' brown 1 fox!"], 'doG': ["jumps-over the 'lazy' doG?"], '1': ["the  quick ' brown 1 fox!"], 'quick': ["the  quick ' brown 1 fox!"], 'the': ["the  quick ' brown 1 fox!", "jumps-over the 'lazy' doG?"], 'over': ["jumps-over the 'lazy' doG?"]}

第一个“the”由index['the'][0]表示,第二个由index['the'][1]

表示