首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python从列表中的元素复制记录

使用Python从列表中的元素复制记录
EN

Stack Overflow用户
提问于 2021-11-23 09:26:04
回答 1查看 43关注 0票数 0

我一直在开发一种功能,使用以下片段从数据集或示例中获取同义词,并希望打印概念和相关的同义词,但实际输出是打印两次,这使它难以理解。

代码:

代码语言:javascript
复制
for rt in self.raw_tokens:
            concept = None
            if rt.startswith('<'):
                # if it's a concept (entity):
                if 'value' in ElementTree.fromstring(rt).attrib:
                    string_token = ElementTree.fromstring(rt).text
                    concept = ElementTree.fromstring(rt).tag
                # if it's a synonym, just use the text and strip the tag from the sample:
                else:
                    string_token = ElementTree.fromstring(rt).text
                    #print('Token: [' + rt + ']')
                    print("Concepts & Synonyms are present in the sample :<%s> = %s" %(ElementTree.fromstring(rt).tag,string_token))
                    #print('CONCEPT: ' + concept)
            else:
                    string_token = rt
            toks.append((concept, string_token))
代码语言:javascript
复制
Expected output :
Concepts & Synonyms are present in the sample :<<I_LOVE>I like</I_LOVE>> = I like
Concepts & Synonyms are present in the sample :<<I_WANTS>NEED</I_WANTS>> = NEED
Concepts & Synonyms are present in the sample :<<NEW_YORK>NEW YORK</NEW_YORK>> = NEW YORK
Concepts & Synonyms are present in the sample :<<I_WANTS>wish</I_WANTS>> = wish
Concepts & Synonyms are present in the sample :<<NEW_YORK>BIG APPLE</NEW_YORK>> = BIG APPLE
代码语言:javascript
复制
Current :

Concepts & Synonyms are present in the sample :<<I_LOVE>I like</I_LOVE>> = I like
Concepts & Synonyms are present in the sample :<<I_WANTS>NEED</I_WANTS>> = NEED
Concepts & Synonyms are present in the sample :<<NEW_YORK>NEW YORK</NEW_YORK>> = NEW YORK
Concepts & Synonyms are present in the sample :<<I_WANTS>wish</I_WANTS>> = wish
Concepts & Synonyms are present in the sample :<<NEW_YORK>BIG APPLE</NEW_YORK>> = BIG APPLE
Concepts & Synonyms are present in the sample :<<I_LOVE>I like</I_LOVE>> = I like
Concepts & Synonyms are present in the sample :<<I_WANTS>NEED</I_WANTS>> = NEED
Concepts & Synonyms are present in the sample :<<NEW_YORK>NEW YORK</NEW_YORK>> = NEW YORK
Concepts & Synonyms are present in the sample :<<I_WANTS>wish</I_WANTS>> = wish
Concepts & Synonyms are present in the sample :<<NEW_YORK>BIG APPLE</NEW_YORK>> = BIG APPLE

有什么建议可以让它打印独特的样本吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-23 09:51:44

这就是我所说的使用Set的意思

代码语言:javascript
复制
toks = set()
for rt in self.raw_tokens:
            concept = None
            if rt.startswith('<'):
                # if it's a concept (entity):
                if 'value' in ElementTree.fromstring(rt).attrib:
                    string_token = ElementTree.fromstring(rt).text
                    concept = ElementTree.fromstring(rt).tag
                # if it's a synonym, just use the text and strip the tag from the sample:
                else:
                    string_token = ElementTree.fromstring(rt).text
            else:
                    string_token = rt
            if not (concept, string_token) in toks:
                print("Concepts & Synonyms are present in the sample :<%s> = %s" %(concept,string_token))
                toks.add((concept, string_token))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70078291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档