• <small id='FbLLs'></small><noframes id='FbLLs'>

    <legend id='FbLLs'><style id='FbLLs'><dir id='FbLLs'><q id='FbLLs'></q></dir></style></legend>
    <i id='FbLLs'><tr id='FbLLs'><dt id='FbLLs'><q id='FbLLs'><span id='FbLLs'><b id='FbLLs'><form id='FbLLs'><ins id='FbLLs'></ins><ul id='FbLLs'></ul><sub id='FbLLs'></sub></form><legend id='FbLLs'></legend><bdo id='FbLLs'><pre id='FbLLs'><center id='FbLLs'></center></pre></bdo></b><th id='FbLLs'></th></span></q></dt></tr></i><div id='FbLLs'><tfoot id='FbLLs'></tfoot><dl id='FbLLs'><fieldset id='FbLLs'></fieldset></dl></div>

        <tfoot id='FbLLs'></tfoot>

          <bdo id='FbLLs'></bdo><ul id='FbLLs'></ul>
      1. 如何过滤一组(int,str)元组,仅返回第一个元素中具有最小值的元组?

        时间:2023-08-31
          <tbody id='UGdtT'></tbody>
        • <bdo id='UGdtT'></bdo><ul id='UGdtT'></ul>

          1. <i id='UGdtT'><tr id='UGdtT'><dt id='UGdtT'><q id='UGdtT'><span id='UGdtT'><b id='UGdtT'><form id='UGdtT'><ins id='UGdtT'></ins><ul id='UGdtT'></ul><sub id='UGdtT'></sub></form><legend id='UGdtT'></legend><bdo id='UGdtT'><pre id='UGdtT'><center id='UGdtT'></center></pre></bdo></b><th id='UGdtT'></th></span></q></dt></tr></i><div id='UGdtT'><tfoot id='UGdtT'></tfoot><dl id='UGdtT'><fieldset id='UGdtT'></fieldset></dl></div>
            <tfoot id='UGdtT'></tfoot>

              <legend id='UGdtT'><style id='UGdtT'><dir id='UGdtT'><q id='UGdtT'></q></dir></style></legend>

              1. <small id='UGdtT'></small><noframes id='UGdtT'>

                  本文介绍了如何过滤一组(int,str)元组,仅返回第一个元素中具有最小值的元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有一组用分数"表示 URL 的元组:

                  Suppose I have a set of tuples representing URLS with "scores":

                  {(0.75, 'http://www.foo.com'), (0.33, 'http://www.bar.com'), (0.5, 'http://www.foo.com'), (0.66, 'http://www.bar.com')}.

                  我有什么简洁的方法可以过滤掉重复的 URL,只返回得分最低的 URL?也就是从上面的例子集合中,我想得到如下集合,其中每个 URL 只出现一次,与原始集合对应的分数最低:

                  What is a concise way for me to filter out duplicate URLS, returning only the URL with the lowest score? That is, from the example set above, I want to get the following set, where each URL appears only once, with the lowest corresponding score from the original set:

                  {(0.5, 'http://www.foo.com'),(0.33, 'http://www.bar.com')}

                  我想出了以下解决方案:

                  I came up with the following solution:

                  from collections import defaultdict
                  
                  seen = defaultdict(lambda:1)
                  for score, url in s:
                      if score < seen[url]:
                          seen[url] = score
                  
                  filtered = {(v,k) for k,v in seen.items()}
                  

                  ...但我觉得可能有一些更简单、更有效的方法可以做到这一点,而无需使用中间 dict 来跟踪最大元素,然后从中重新生成集合.按第一个元素的最小值/最大值过滤一组元组的最佳方法是什么?

                  ... but I feel like there is probably some simpler and more efficient way to do this without using the intermediary dict to keep track of the max element, and then regenerate the set from that. What is the best way to filter a set of tuples by the min/max of the first element?

                  推荐答案

                  你已经实现了我能想到的最简单的方法.我要做的唯一改变是循环——一个稍微简洁一点的版本是使用 min.

                  You've already implemented the simplest approach I can think of. The only change I'd make would be to the loop—a slightly more concise version is using min.

                  seen = defaultdict(lambda: 1)  # `lambda: float('inf')` if scores can be > 1
                  for score, url in s:
                      seen[url] = min(seen[url], score)
                  
                  {(v,k) for k,v in seen.items()}
                  # {(0.33, 'http://www.bar.com'), (0.5, 'http://www.foo.com')}
                  

                  <小时>

                  如果您真的想要一个更短的解决方案,就像我说的那样,这不是最简单的方法,但它是一种单一的方法.大多数挑战是交换 URL 和分数,因此您可以在删除重复项时使用 URL 作为键.不用说,排序是这里的先决条件(这就是为什么我不像上面那样喜欢这个解决方案).


                  If you really want a shorter solution, like I said, it isn't the simplest approach, but it is a one liner. Most of the challenge is interchanging the URL and the score so you can use the URL as a key when dropping duplicates. It goes without saying that sorting is a pre-condition here (that's why I don't like this solution as much as the one above).

                  {(v, k) for k, v in dict(sorted(((v, k) for k, v in s), reverse=True)).items()}
                  # {(0.33, 'http://www.bar.com'), (0.5, 'http://www.foo.com')}
                  

                  如果 s 看起来像这样,这个解决方案就会变得更短:

                  This solution becomes so much shorter if s looks like this:

                  s2 = {(v,k) for k, v in s}
                  s2 
                  # {('http://www.bar.com', 0.33), ('http://www.bar.com', 0.66), ...}
                  

                  你只需要这样做

                  list(dict(sorted(s2, reverse=True)).items())
                  # [('http://www.foo.com', 0.5), ('http://www.bar.com', 0.33)]
                  

                  这篇关于如何过滤一组(int,str)元组,仅返回第一个元素中具有最小值的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从元组中删除元素时额外的空元素 下一篇:在 Python 元组列表中查找重复项

                  相关文章

                  • <bdo id='XyyTb'></bdo><ul id='XyyTb'></ul>
                  <i id='XyyTb'><tr id='XyyTb'><dt id='XyyTb'><q id='XyyTb'><span id='XyyTb'><b id='XyyTb'><form id='XyyTb'><ins id='XyyTb'></ins><ul id='XyyTb'></ul><sub id='XyyTb'></sub></form><legend id='XyyTb'></legend><bdo id='XyyTb'><pre id='XyyTb'><center id='XyyTb'></center></pre></bdo></b><th id='XyyTb'></th></span></q></dt></tr></i><div id='XyyTb'><tfoot id='XyyTb'></tfoot><dl id='XyyTb'><fieldset id='XyyTb'></fieldset></dl></div>

                  <small id='XyyTb'></small><noframes id='XyyTb'>

                  <tfoot id='XyyTb'></tfoot>

                    <legend id='XyyTb'><style id='XyyTb'><dir id='XyyTb'><q id='XyyTb'></q></dir></style></legend>