<tfoot id='0oc2t'></tfoot>
        <bdo id='0oc2t'></bdo><ul id='0oc2t'></ul>

      <small id='0oc2t'></small><noframes id='0oc2t'>

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

        Python中集合的不区分大小写比较

        Case-insensitive comparison of sets in Python(Python中集合的不区分大小写比较)
        <tfoot id='BKXY9'></tfoot>

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

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

            • <bdo id='BKXY9'></bdo><ul id='BKXY9'></ul>
                  <legend id='BKXY9'><style id='BKXY9'><dir id='BKXY9'><q id='BKXY9'></q></dir></style></legend>
                  本文介绍了Python中集合的不区分大小写比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两套(虽然我可以做列表或其他什么):

                  I have two sets (although I can do lists, or whatever):

                  a = frozenset(('Today','I','am','fine'))
                  b = frozenset(('hello','how','are','you','today'))
                  

                  我想得到:

                  frozenset(['Today'])
                  

                  或至少:

                  frozenset(['today'])
                  

                  如果我将我假设的所有内容都小写,第二个选项是可行的,但我正在寻找一种更优雅的方式.有可能吗

                  The second option is doable if I lowercase everything I presume, but I'm looking for a more elegant way. Is it possible to do

                  a.intersection(b) 
                  

                  以不区分大小写的方式?

                  in a case-insensitive manner?

                  Django 中的快捷方式也很好,因为我正在使用该框架.

                  Shortcuts in Django are also fine since I'm using that framework.

                  下面的交集方法示例(我无法弄清楚如何在评论中格式化):

                  Example from intersection method below (I couldn't figure out how to get this formatted in a comment):

                  print intersection('Today I am fine tomorrow'.split(),
                                      'Hello How a re you TODAY and today and Today and Tomorrow'.split(),
                                      key=str.lower)
                  
                  [(['tomorrow'], ['Tomorrow']), (['Today'], ['TODAY', 'today', 'Today'])]
                  

                  推荐答案

                  以下版本适用于任何一对可迭代对象:

                  Here's version that works for any pair of iterables:

                  def intersection(iterableA, iterableB, key=lambda x: x):
                      """Return the intersection of two iterables with respect to `key` function.
                  
                      """
                      def unify(iterable):
                          d = {}
                          for item in iterable:
                              d.setdefault(key(item), []).append(item)
                          return d
                  
                      A, B = unify(iterableA), unify(iterableB)
                  
                      return [(A[k], B[k]) for k in A if k in B]
                  

                  例子:

                  print intersection('Today I am fine'.split(),
                                     'Hello How a re you TODAY'.split(),
                                     key=str.lower)
                  # -> [(['Today'], ['TODAY'])]
                  

                  这篇关于Python中集合的不区分大小写比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                    <legend id='gHAJZ'><style id='gHAJZ'><dir id='gHAJZ'><q id='gHAJZ'></q></dir></style></legend>
                      <bdo id='gHAJZ'></bdo><ul id='gHAJZ'></ul>

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

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

                      • <tfoot id='gHAJZ'></tfoot>