【问题标题】:append only if item isn't already appended?仅在尚未附加项目时附加?
【发布时间】:2022-07-03 00:27:21
【问题描述】:

在我的 Python 应用程序中,我有以下几行:

for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        audio['hls']['group_id'].append(index)

如果以前没有附加索引,我怎样才能只触发附加语句?

提前致谢

【问题讨论】:

  • 发布编解码器的数据
  • 我建议你使用 set ,如果顺序无关紧要

标签: python


【解决方案1】:

只需测试if你的索引not in你的列表:

for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        if index not in audio['hls']['group_id']:
            audio['hls']['group_id'].append(index)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-03
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多