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

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

    2. “添加到集合"在java中返回一个布尔值-python呢?

      时间:2023-10-14
        <tbody id='MKcsn'></tbody>
      <legend id='MKcsn'><style id='MKcsn'><dir id='MKcsn'><q id='MKcsn'></q></dir></style></legend>
        <i id='MKcsn'><tr id='MKcsn'><dt id='MKcsn'><q id='MKcsn'><span id='MKcsn'><b id='MKcsn'><form id='MKcsn'><ins id='MKcsn'></ins><ul id='MKcsn'></ul><sub id='MKcsn'></sub></form><legend id='MKcsn'></legend><bdo id='MKcsn'><pre id='MKcsn'><center id='MKcsn'></center></pre></bdo></b><th id='MKcsn'></th></span></q></dt></tr></i><div id='MKcsn'><tfoot id='MKcsn'></tfoot><dl id='MKcsn'><fieldset id='MKcsn'></fieldset></dl></div>
      • <tfoot id='MKcsn'></tfoot>

                <bdo id='MKcsn'></bdo><ul id='MKcsn'></ul>

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

              • 本文介绍了“添加到集合"在java中返回一个布尔值-python呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Java 中,我喜欢使用添加到集合"操作返回的布尔值来测试该元素是否已经存在于集合中:

                In Java I like to use the Boolean value returned by an "add to the set" operation to test whether the element was already present in the set:

                if (set.add("x")) {
                   print "x was not yet in the set";
                }
                

                我的问题是,在 Python 中有没有这样方便的东西?我试过了

                My question is, is there something as convenient in Python? I tried

                 z = set()
                 if (z.add(y)):
                     print something
                

                但它不打印任何东西.我错过了什么吗?谢谢!

                But it does not print anything. Am I missing something? Thx!

                推荐答案

                在 Python 中,set.add() 方法不返回任何内容.您必须使用 not in 运算符:

                In Python, the set.add() method does not return anything. You have to use the not in operator:

                z = set()
                if y not in z: # If the object is not in the list yet...
                    print something
                z.add(y)
                

                如果您真的需要在添加之前知道对象是否在集合中,只需存储布尔值即可:

                If you really need to know whether the object was in the set before you added it, just store the boolean value:

                z = set()
                was_here = y not in z
                z.add(y)
                if was_here: # If the object was not in the list yet...
                    print something
                

                但是,我认为您不太可能需要它.

                However, I think it is unlikely you need it.

                这是一个 Python 约定:当一个方法更新某个对象时,它返回 None.你可以忽略这个约定;此外,还有一些在野外"违反它的方法.然而,这是一个普遍的、公认的惯例:我建议您坚持并牢记这一点.

                This is a Python convention: when a method updates some object, it returns None. You can ignore this convention; also, there are methods "in the wild" that violate it. However, it is a common, recognized convention: I'd recommend to stick to it and have it in mind.

                这篇关于“添加到集合"在java中返回一个布尔值-python呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:获取集合的所有可能分区 下一篇:Java 部分有序集合&lt;E&gt;

                相关文章

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

                  <tfoot id='vXhsU'></tfoot>
                1. <legend id='vXhsU'><style id='vXhsU'><dir id='vXhsU'><q id='vXhsU'></q></dir></style></legend>

                    <bdo id='vXhsU'></bdo><ul id='vXhsU'></ul>
                  1. <small id='vXhsU'></small><noframes id='vXhsU'>