我目前有一个列表列表,如下所示:
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 循环将每个项目变成一个字典来尝试这个,但是这些字典随后被它们的标签重新排列,遗憾的是,由于这件事的性质,这不可能发生......实验需要一切 保持相同的顺序,因为最终我需要测量标签相对于其他标签的接近度,但只能在同一个句子(列表)中。
我考虑过使用 NLTK(我对它的经验很少)来实现这一点,但看起来它比我需要的要复杂得多,而且像我这样的新手不容易自定义标签。
我认为这可以通过迭代这些项目中的每一个来完成,使用 if 语句来确定它们应该有什么标签,然后用单词及其关联的标签制作一个元组,这样它就不会在其列表中移动。
我设计了这个..但我不知道如何重建我的列表列表并使它们保持有序:(。
for i in My_List: #For each list in the list of lists
for h in i: #For each item in each list
if h in GG_List: # Check for the tag
MyDicts = {"GG":h for h in i} #Make Dict from tag + word
非常感谢您的帮助!
最佳答案
将标签放入字典中会起作用:
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]
现在:
>>> res
[[('This', 'GG'),
('Is', 'FF'),
('A', 'FF'),
('Sample', 'EE'),
('Text', 'FF'),
('Sentence', 'GG')],
[('This', 'GG')],
[]]
关于python-2.7 - PYTHON 2.7 - 修改列表列表并重新组装而不改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512811/