如何在Python中为列表中的每个字符串创建子列表?

2023-12-14

例如,我有这个列表:

word1 =  ['organization', 'community']

我有一个函数可以从列表中的单词中获取同义词:

from nltk.corpus import wordnet as wn


def getSynonyms(word1):
    synonymList1 = []
    for data1 in word1:
        wordnetSynset1 = wn.synsets(data1)
        tempList1 = []
        for synset1 in wordnetSynset1:
            synLemmas = synset1.lemma_names()
            for s in synLemmas:
                word = s.replace('_', ' ')
                if word not in tempList1:
                    tempList1.append(word)
        synonymList1.append(tempList1)
    return synonymList1


syn1 = getSynonyms(word1)
print(syn1)

这是输出:

[
    ['organization', 'organisation', 'arrangement', 'system', 'administration',
     'governance', 'governing body', 'establishment', 'brass', 'constitution',
     'formation'],
    ['community', 'community of interests', 'residential district',
     'residential area', 'biotic community']
]

^ 上面的输出显示两个的每个同义词集'organization' and 'community'在列表中列出。然后我减少列表的级别:

newlist1 = [val for sublist in syn1 for val in sublist]

这是输出:

['organization', 'organisation', 'arrangement', 'system', 'administration',
 'governance', 'governing body', 'establishment', 'brass', 'constitution',
 'formation', 'community', 'community of interests', 'residential district',
 'residential area', 'biotic community']

^ 现在所有同义词集都保持相同的字符串,没有子列表。我现在想做的就是使所有同义词集newlist1彼此进行子列表。我预计输出会是这样的:

[['organization'], ['organisation'], ['arrangement'], ['system'],
 ['administration'], ['governance'], ['governing body'], ['establishment'],
 ['brass'], ['constitution'], ['formation'], ['community'],
 ['community of interests'], ['residential district'], ['residential area'],
 ['biotic community']]

我正在尝试这段代码:

uplist1 = [[] for x in syn1]
uplist1.extend(syn1)
print(uplist1)

但结果并不是我所期望的:

[[], [],
 ['organization', 'organisation', 'arrangement', 'system', 'administration',
  'governance', 'governing body', 'establishment', 'brass', 'constitution',
  'formation'],
 ['community', 'community of interests', 'residential district',
  'residential area', 'biotic community']]

它显示了两个空列表和两个同义词集列表'organization' and 'community'

如何使同义词集的每个字符串成为一个子列表?


将每个元素放入syn1并包裹在[]使其成为子列表。

要么与append:

uplist1 = []
for i in syn1:
    uplist1.append([i])

或者使用等效的列表理解:

uplist1 = [[i] for i in syn1]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python中为列表中的每个字符串创建子列表? 的相关文章

  • Python:记录垃圾收集器

    我有一个 python 应用程序 有一些性能问题 我想将垃圾收集器的事件 特别是何时调用 添加到我的日志中 是否可以 thanks http docs python org library gc html gc set debug http
  • Python 中的安全解除引用

    Groovy 有一个很好的安全取消引用运算符 这有助于避免 NullPointerExceptions variable method The method仅当以下情况时才会被调用variable is not null 有没有办法在 Py
  • Python的reduce()短路了吗?

    If I do result reduce operator and False 1000 得到第一个结果后它会停止吗 自从False anything False 相似地 result reduce operator or True 10
  • 为什么导入 pdb 时出现此错误? “模块”对象没有属性“ascii_letters”

    尝试调试我的代码 我正在导入库pdb import sys from subprocess import check call import pdb functions if name main Code 我收到此错误 File reg p
  • 如何在 ReportLab 段落中插入回车符?

    有没有办法在 ReportLab 的段落中插入回车符 我试图将 n 连接到我的段落字符串 但这不起作用 Title Paragraph Title n Page myStyle 我想要这样做 因为我将名称放入单元格中 并且想要控制单元格中的
  • 迭代字符串 R 的字符

    有人可以解释一下为什么这不会在 R 中单独打印所有数字 numberstring lt 0123456789 for number in numberstring print number 字符串不就是字符数组吗 在 R 中该怎么做 In
  • Pyspark 数据框逐行空列列表

    我有一个 Spark 数据框 我想创建一个新列 其中包含每行中具有 null 的列名称 例如 原始数据框是 col 1 col 2 col 3 62 45 null 62 49 56 45 null null null null null
  • Python 正则表达式部分匹配或“hitEnd”

    我正在编写一个扫描器 因此我将任意字符串与正则表达式规则列表进行匹配 如果我可以模拟 Java hitEnd 功能 不仅知道正则表达式何时不匹配 还知道何时匹配 这将非常有用 can t匹配 当正则表达式匹配器在决定拒绝输入之前到达输入末尾
  • 错误:无法访问文件“$libdir/plpython2”:没有这样的文件或目录

    我正在运行 postgresql 9 4 PostgreSQL 9 4 4 on x86 64 unknown linux gnu compiled by gcc GCC 4 1 2 20070626 Red Hat 4 1 2 14 64
  • str.translate 给出 TypeError - Translate 采用一个参数(给定 2 个参数),在 Python 2 中工作

    我有以下代码 import nltk os json csv string cPickle from scipy stats import scoreatpercentile lmtzr nltk stem wordnet WordNetL
  • 如何使用 PySpark 有效地将这么多 csv 文件(大约 130,000 个)合并到一个大型数据集中?

    我之前发布了这个问题并得到了一些使用 PySpark 的建议 如何有效地将这一大数据集合并到一个大数据框中 https stackoverflow com questions 60259271 how can i merge this la
  • Python 视频框架

    我正在寻找一个 Python 框架 它将使我能够播放视频并在该视频上绘图 用于标记目的 我尝试过 Pyglet 但这似乎效果不是特别好 在现有视频上绘图时 会出现闪烁 即使使用双缓冲和所有这些好东西 而且似乎没有办法在每帧回调期间获取视频中
  • Python正则表达式从字符串中获取浮点数

    我正在使用正则表达式来解析字符串中的浮点数 re findall a zA Z d d t 是我使用的代码 这段代码有问题 如果数字和任何字符之间没有空格 则不会解析该数字 例如 0 1 2 3 4 5 6 7 8 9 的预期输出为 0 1
  • 如何检查列表是否为空?

    这个问题的答案是社区努力 help privileges edit community wiki 编辑现有答案以改进这篇文章 目前不接受新的答案或互动 例如 如果通过以下内容 a 我如何检查是否a是空的 if not a print Lis
  • 如何强制 Y 轴仅使用整数

    我正在使用 matplotlib pyplot 模块绘制直方图 我想知道如何强制 y 轴标签仅显示整数 例如 0 1 2 3 等 而不显示小数 例如 0 0 5 1 1 5 2 等 我正在查看指导说明并怀疑答案就在附近matplotlib
  • 大型数据集上的 Sklearn-GMM

    我有一个很大的数据集 我无法将整个数据放入内存中 我想在这个数据集上拟合 GMM 我可以用吗GMM fit sklearn mixture GMM 重复小批量数据 没有理由重复贴合 只需随机采样您认为机器可以在合理时间内计算的尽可能多的数据
  • 为什么 bot.get_channel() 会产生 NoneType?

    我正在制作一个 Discord 机器人来处理公告命令 当使用该命令时 我希望机器人在特定通道中发送一条消息 并向用户发送一条消息以表明该命令已发送 但是 我无法将消息发送到频道 我尝试了这段代码 import discord import
  • 最小硬币找零问题——回溯

    我正在尝试用最少数量的硬币解决硬币找零问题 采用回溯法 我实际上已经完成了它 但我想添加一些选项 按其单位打印硬币数量 而不仅仅是总数 这是我下面的Python代码 def minimum coins coin list change mi
  • 在Python中从日期时间中减去秒

    我有一个 int 变量 它实际上是秒 让我们调用这个秒数X 我需要得到当前日期和时间 以日期时间格式 减去的结果X秒 Example If X是 65 当前日期是2014 06 03 15 45 00 那么我需要得到结果2014 06 03
  • 在Python 3.2中,我可以使用http.client打开并读取HTTPS网页,但urllib.request无法打开同一页面

    我想打开并阅读https yande re https yande re with urllib request 但我收到 SSL 错误 我可以使用以下方式打开并阅读页面http client用这个代码 import http client

随机推荐