Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python2.7-修改列表列表并在不进行变异的情况下重新组装_Python 2.7_Dictionary_Iterator_Nested Lists_Listiterator - Fatal编程技术网

Python 2.7 Python2.7-修改列表列表并在不进行变异的情况下重新组装

Python 2.7 Python2.7-修改列表列表并在不进行变异的情况下重新组装,python-2.7,dictionary,iterator,nested-lists,listiterator,Python 2.7,Dictionary,Iterator,Nested Lists,Listiterator,我目前有一个列表,如下所示: My_List = [[This, Is, A, Sample, Text, Sentence] [This, too, is, a, sample, text] [finally, so, is, this, one]] 现在我需要做的是用3个标签中的一个来“标记”这些单词,在本例中是任意的,例如“EE”、“FF”或“GG”,基于单词所在的列表,然后将它们重新组合成它们出现的相同顺序。我的最终代码需要如下所示: GG_List = [This, Sentence

我目前有一个列表,如下所示:

My_List = [[This, Is, A, Sample, Text, Sentence] [This, too, is, a, sample, text] [finally, so, is, this, one]]
现在我需要做的是用3个标签中的一个来“标记”这些单词,在本例中是任意的,例如“EE”、“FF”或“GG”,基于单词所在的列表,然后将它们重新组合成它们出现的相同顺序。我的最终代码需要如下所示:

GG_List = [This, Sentence]
FF_List = [Is, A, Text]
EE_List = [Sample]

My_List = [[(This, GG), (Is, FF), (A, FF), (Sample, "EE), (Text, FF), (Sentence, GG)] [*same with this sentence*] [*and this one*]]
我尝试使用for循环将每个项目转换为dict,但dict随后被标签重新排列,遗憾的是,由于这件事的性质,这无法发生。。。这个实验需要所有的东西保持相同的顺序,因为最终我需要测量标签相对于其他标签的接近程度,但只在同一个句子中(列表)

我曾想过用NLTK(我对NLTK几乎没有经验)来做这件事,但它看起来比我需要的要复杂得多,而且像我这样的新手不容易定制标签

我认为这可以通过迭代这些项中的每一项来实现,使用if语句来确定它们应该有什么标记,然后用单词及其相关标记生成一个元组,这样它就不会在列表中来回移动

我想出了这个。。但是我不知道如何重建我的列表并使它们保持有序:(


非常感谢您的帮助!

将标签放入字典中可以:

My_List = [['This', 'Is', 'A', 'Sample', 'Text', 'Sentence'],
           ['This', 'too', 'is', 'a', 'sample', 'text'],
           ['finally', 'so', 'is', 'this', 'one']]
GG_List = ['This', 'Sentence']
FF_List = ['Is', 'A', 'Text']
EE_List = ['Sample']

zipped = zip((GG_List, FF_List, EE_List), ('GG', 'FF', 'EE'))
tags = {item: tag for tag_list, tag in zipped for item in tag_list}
res = [[(word, tags[word]) for word in entry if word in tags] for entry in My_List]
现在:


字典通过键-值对工作。每个键都被分配一个值。要搜索字典,您可以通过键搜索索引,例如

>>> d = {1:'a', 2:'b', 3:'c'}
>>> d[1]
'a'
在上述情况下,我们总是按字典的键(即整数)搜索字典

如果要将标记/标签分配给每个单词,则要按关键字搜索并查找“值”,即标记/标签,因此词典必须如下所示(假设字符串是作为标记/标签的单词和数字):

这样,当您使用列表理解遍历单词并找到合适的标记时,标记的顺序将遵循单词的顺序

因此,当您以错误的方式索引初始词典时,问题就会出现,即键->标签,值->单词,例如:

>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> [d[word] for word in sent]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'a'

但需要注意的是,
ChainMap
仅在Python3.5中可用(这是升级Python的另一个原因;p)。对于Python,您的需求令人困惑。您想将它们标记为“基于单词所在的列表”,但您的输出似乎不遵循该模式?@erewok-hmm…以第一句话为例:[This,Is,A,Sample,Text,句子]。由于单词“Sample”位于另一个名为“GG_list”的列表中,因此单词“Sample”变成(Sample,GG)。这说明了吗?你想要的输出是什么?你能举个例子吗?另外,我认为这与
nltk
没有任何关系;我不介意删除
nltk
标记,因为它似乎不相关。无耻的插件:我们为基本python教程编写了列表和python容器的简短介绍,这可能会对你有更好的帮助了解如何在NLP中使用它们。看看“理解容器”谢谢你的输入!我还不太清楚我在寻找什么样的输出,迈克的解决方案让我更接近我所需要的,基于我所拥有的。非常感谢你所有的解释和例子-他们帮助我解释了很多!!没问题,我很高兴答案有帮助。问题是迈克,这让我更容易理解ut我的列表。为了使这是一个有效的实验,我必须保持列表的灵活性。你知道我该如何处理吗?相应地更新我的答案。
>>> d = {1:'a', 2:'b', 3:'c'}
>>> d[1]
'a'
>>> d = {'a':1, 'b':1, 'c':3}
>>> d['a']
1
>>> sent = 'a b c a b'.split()
>>> sent
['a', 'b', 'c', 'a', 'b']
>>> [d[word] for word in sent]
[1, 1, 3, 1, 1]
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> [d[word] for word in sent]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'a'
>>> from collections import ChainMap
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
>>> d_inv
{'h': 2, 'c': 3, 'a': 1, 'x': 3, 'b': 2, 'd': 1}
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> sent = 'a b c a b'.split()
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
>>> [d_inv[word] for word in sent]
[1, 2, 3, 1, 2]
>>> sentences = ['a b c'.split(), 'h a x'.split()]
>>> [[d_inv[word] for word in sent] for sent in sentences]
[[1, 2, 3], [2, 1, 3]]