Python 引用列表中的项目
我有以下python代码:Python 引用列表中的项目,python,python-3.x,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_-sential的函数,它接受s中的一个元素,并将该句子作为该元素所属的字符串返回。例如: "brown" -> "the quick ' brown 1 fox!" 如果将第一个“the”传
import regex
original = " the quick ' brown 1 fox! jumps-over the 'lazy' doG? ! "
s = [i for i in original.split(" ")]
我想编写一个名为get_-sential
的函数,它接受s
中的一个元素,并将该句子作为该元素所属的字符串返回。例如:
"brown" -> "the quick ' brown 1 fox!"
如果将第一个“the”传递给函数,则:
"the" -> the quick ' brown 1 fox!"
如果第二个选项为:
"the" -> "jumps-over the 'lazy' doG?"
您会将什么作为参数传递给这样的函数?在C++中,我可以传递一个STD::vector::在C语言中,我会传递一个int(数组索引)甚至一个指针。我不完全确定我是否理解您的意图,但您可能只传递一个整数索引。您不能传递对
的引用,因为这两者完全相同。“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
你有了一本字典,每个单词都有它出现的句子索引。顺便说一句,您可以用一些非常好的列表理解来重写它。>>来自itertools导入产品chain
>>> 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"
>>>假设你原来的句子是
>>>origional=“那只敏捷的棕色1号狐狸!跳过了那只懒惰的狗?!”
>>>#句子结束符是
>>>已发送的_term=“[?!;]”
>>>#我会用正则表达式把它分成几个句子
>>>关于拆分(已发送项,origional.strip())
[“敏捷的'棕色1狐狸',”跳过'懒惰'狗','','',']
>>>#然后将其拆分为文字
>>>#我本可以使用str.split,但那会包括标点符号
>>>#你可能不感兴趣
>>>#对于每个单词,我使用product创建一个句子映射
>>>单词映射=((产品(关于拆分(“\W”,e),[e]))
对于re.split中的e(发送项,origional.strip())
>>>#将其链接为单个列表
>>>word_map=chain(*(产品(重新拆分(“\W”,e),[e]))
对于re.split中的e(发送项,origional.strip())
>>>从集合导入defaultdict
>>>#创建默认dict
>>>words=defaultdict(列表)
>>>#并填充所有非琐碎的单词
>>>对于word_图中的k,v:
如果k.strip():
字[k]+=[v]
>>>言语
defaultdict(,{'brown':[“敏捷的'棕色1狐狸”],'lazy':[“跳过'懒惰的'狗”],'jumps':[“跳过'懒惰的'狗”],'fox':[“敏捷的'棕色1狐狸”],'doG':[“跳过'懒惰的'狗”],'1':[“敏捷的'棕色1狐狸”],'quick':[“敏捷的'棕色1狐狸”],'the':[“敏捷的'棕色1狐狸”,','over':[“跳过‘懒’狗”]})
>>>#现在来听第一个词
>>>单词['the'][0]
“敏捷的‘棕色1号狐狸’”
>>>现在来读第二句
>>>单词['the'][1]
“跳过‘懒惰’的狗”
您可以通过一个句子列表的词典索引来实现这一点:
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”由索引['The'][0]
表示,第二个由索引['The'][1]
表示。我修复了代码中原文的拼写。我希望你不介意。另外,在re
模块中可以找到标准的python正则表达式解析器,regex
模块有点不同(以防万一…)@Keyser我看到的更奇怪@谢谢你!我将regex用于regex.sub('\P{alpha})在我生成上面的s之后,我继续以新列表的形式进一步清理它。因此,当我使用“doG?”时,我实际上会在我的算法中使用“doG”-例如,在确定词频时。然而,在某些情况下,我需要确定“doG”在哪里来自。正如您指出的,我可以使用int。请注意,set()
与set([])
是一样的,不需要创建额外的列表就可以扔掉。@Jakub M。但是字典没有顺序信息。我不知道我指的是关于这两个“the”的哪个元素。@Baz:lookup[“the”]==set([0,1])
,“the”在第一句和第二句中。您还需要什么信息?将整个句子复制到内存中每次您在字典中插入一个新词时,它看起来就像一个内存炸弹