仅当项目IS IS IS IS已附加时附加?

发布于 2025-02-12 14:44:26 字数 265 浏览 0 评论 0原文

在我的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)

如果以前尚未附加索引,我如何仅触发附加语句?

In my Python application, I have the following lines:

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)

How can I only trigger the append statement if the index hasn't been already appended previously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2025-02-19 14:44:26

我认为您会倒退,非常效率。现在,对于每个名称,您可以循环浏览整个作业['audio']序列以查找匹配项。这是O(n^2)。

取而代之的是,您可以通过coedecs进行一次通行证以记录每个事件的第一次出现:

codec_map = {codec: len(codecs) - i - 1 for i, codec in enumerate(reversed(codecs))}

反向允许您选择第一个索引而不是最后一个索引,因为字典将包含遇到的最后一个键。如果您不在乎它是第一个还是最后一个,则可以简化:

codec_map = {codec: i for i, codec in enumerate(codecs)}

现在,内部循环只需要运行一次:

for audio in job['audio']:
    if audio['hls']['codec_name'] in codec_map:
        audio['hls']['group_id'].append(codec_map[audio['hls']['codec_name']])

此解决方案是O(n),并允许您更有效地检查事物。

I think you're going about this backwards, very inefficiently. Right now, for each name, you loop through the entire job['audio'] sequence to find a match. This is O(n^2).

Instead, you could do a single pass over coedecs to record the first occurrence of each one:

codec_map = {codec: len(codecs) - i - 1 for i, codec in enumerate(reversed(codecs))}

Reversing allows you to select the first index instead of the last, since the dictionary will contain the last key encountered. If you don't care whether it's first or last, you can simplify:

codec_map = {codec: i for i, codec in enumerate(codecs)}

Now the inner loop only needs to run once:

for audio in job['audio']:
    if audio['hls']['codec_name'] in codec_map:
        audio['hls']['group_id'].append(codec_map[audio['hls']['codec_name']])

This solution is O(N), and allows you to check things more efficiently.

千秋岁 2025-02-19 14:44:26

只需测试如果您的索引不在您的列表中:

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)

Simply test if your index not in your list:

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)
债姬 2025-02-19 14:44:26

只是添加

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

just add

for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        if index in audio['hls']['group_id']:
            pass
        else:
           audio['hls']['group_id'].append(index)
满身野味 2025-02-19 14:44:26

在中使用list工作中使用运算符,但在set set中的操作符中的效率不如。您可以使用设定的数据结构来提高效率:

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

Using in operator in list works, but it is less efficient than in operator in set. You can use the set data structure to improve the efficiency:

visited_indices = set()
for index, codec in enumerate(codecs):
    for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']):
        if index not in visited_indices:
            audio['hls']['group_id'].append(index)
            visited_indices.add(index)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文