gpt4 book ai didi

python - 从 Python 中的嵌套列表中提取字符串

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:24 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Flatten (an irregular) list of lists in Python

我正在尝试使用 python 中的 nltk 库,更具体地说是 wordnet 语料库,来提取广泛语义类别(如“动物”)中的所有单词。我已经设法编写了一个函数,它遍历所有类别并提取其中的单词,但我最终得到的是列表中的大量列表。这些列表的长度或深度不可预测,它们看起来像这样:

['pet', 'pest', 'mate', 'young', 'stunt', 'giant', ['hen', 'dam', 'filly'], ['head', 'stray', 'dog', ['puppy', 'toy', 'spitz', 'pooch', 'doggy', 'cur', 'mutt', 'pug', 'corgi', ['Peke'], ['chow'], ['feist', 'fice'], ['hound', ['Lhasa', 'cairn']], ['boxer', 'husky']], ['tabby', 'tabby', 'queen', 'Manx', 'tom', 'kitty', 'puss', 'pussy', ['gib']]]

我想要的是能够从中获取每个字符串,并返回一个未嵌套的列表。有什么建议吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com