相关疑难解决方法(0)

在Python中评估布尔表达式

对象在Python中评估的真值是什么?

相关问题

python boolean object

11
推荐指数
2
解决办法
1万
查看次数

当在一个陈述中合并时,"和"和"或"如何工作?

出于某种原因,这个功能使我困惑:

def protocol(port):
    return port == "443" and "https://" or "http://"
Run Code Online (Sandbox Code Playgroud)

有人可以解释幕后发生的事情的顺序,使其按照它的方式工作.

直到我尝试它才明白这一点:

要么A)

def protocol(port):
    if port == "443":
        if bool("https://"):
            return True
    elif bool("http://"):
        return True
    return False
Run Code Online (Sandbox Code Playgroud)

或者B)

def protocol(port):
    if port == "443":
        return True + "https://"
    else:
        return True + "http://"
Run Code Online (Sandbox Code Playgroud)

这是Python中的某种特殊情况,还是我完全误解了语句的工作原理?

python boolean-logic if-statement

7
推荐指数
3
解决办法
1万
查看次数

如果L中的'a'或'b',其中L是一个列表(Python)

我遇到以下逻辑问题:

让我们说我有一个清单 L = ['a', 'b', 'c']


这两个项目都在列表中......

if ('a' or 'b') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 It's there!


只有第一个项目在列表中...

if ('a' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 It's there!


列表中没有任何项目......

if ('e' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 No sorry


这是令人困惑的一个只有列表中的第二项......

if ('e' or 'a') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
Run Code Online (Sandbox Code Playgroud)

版画 No sorry


我不明白为什么这不是一个真实的陈述.这如何概括为具有n个 …

python

7
推荐指数
2
解决办法
2万
查看次数

Convert elements of list in pandas series using a dict

I have the following Pandas dataframe:

1    ["Apple", "Banana"]
2    ["Kiwi"]
3    None
4    ["Apple"]
5    ["Banana", "Kiwi"]
Run Code Online (Sandbox Code Playgroud)

and the following dict:

{1: ["Apple", "Banana"],
2: ["Kiwi"]}
Run Code Online (Sandbox Code Playgroud)

I would now like to map all the entries in the lists in my dataframe using the dictionary. The result should be the following:

1    [1]
2    [2]
3    None
4    [1]
5    [1, 2]
Run Code Online (Sandbox Code Playgroud)

How can this be done most efficiently?

python pandas

7
推荐指数
1
解决办法
112
查看次数

在`x = xy()`之后,为什么`x`变成了`None`而不是被修改(可能导致“AttributeError:'NoneType'对象没有属性”)?

如果您的问题作为此问题的重复项而被关闭,那是因为您有一些通用形式的代码

x = X()
# later...
x = x.y()
# or:
x.y().z()
Run Code Online (Sandbox Code Playgroud)

其中X是某种类型,它提供了y旨在z变异修改)对象(X类型的实例)的方法。这可以适用于:

  • 可变的内置类型,例如listdictsetbytearray
  • 标准库(尤其是 Tkinter 小部件)或第三方库提供的类。

这种形式的代码很常见,但并不总是错误的。问题的明显迹象是:

  • x.y().z()一样,会引发异常AttributeError: 'NoneType' object has no attribute 'z'

  • 有了x = x.y(),x就变成None, 而不是被修改的对象。这可能会被后来的错误结果发现,或者被像上面这样的异常(x.z()稍后尝试时)发现。

Stack Overflow 上有大量关于这个问题的现有问题,所有这些问题实际上都是同一个问题。之前甚至有多次尝试在特定上下文中涵盖同一问题的规范。然而,理解问题并不需要上下文,因此这里尝试一般性地回答:

代码有什么问题吗?为什么这些方法会这样,我们如何解决这个问题?


另请注意,当尝试使用 alambda或列表理解)来产生副作用时,会出现类似的问题。

同样明显的问题可能是由因其他原因返回的方法引起的None …

python attributeerror command-query-separation nonetype

7
推荐指数
1
解决办法
577
查看次数

使用布尔表达式分配字符串

我试图从别人的项目中理解这段代码.如果您想要上下文,请访问:https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2只是一个布尔变量,True如果Python主要版本是2.我知道非空字符串是True,但由于某种原因我不理解openmode被指定'w'或者'wt'而不是TrueFalse.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}
Run Code Online (Sandbox Code Playgroud)

有人能解释一下结果吗?

python python-2.7

6
推荐指数
2
解决办法
942
查看次数

在python lambda函数中使用OR运算符

O Reilly Programming Python书中有一个代码示例,它在lambda函数中使用OR运算符.该文本指出"[代码]使用或运算符强制运行两个表达式".

这是如何以及为什么有效?

widget = Button(None, # but contains just an expression
text='Hello event world',
command=(lambda: print('Hello lambda world') or sys.exit()) )
widget.pack()
widget.mainloop()
Run Code Online (Sandbox Code Playgroud)

python lambda

6
推荐指数
1
解决办法
2292
查看次数

逻辑"和"运算符如何在Python中使用整数?

所以,我正在玩翻译,并输入以下内容:

In [95]: 1 and 2
Out[95]: 2

In [96]: 1 and 5
Out[96]: 5

In [97]: 234324 and 2
Out[97]: 2

In [98]: 234324 and 22343243242
Out[98]: 22343243242L

In [99]: 1 or 2 and 9
Out[99]: 1
Run Code Online (Sandbox Code Playgroud)

最初我认为它与False和True值有关,因为:

In [101]: True + True
Out[101]: 2

In [102]: True * 5
Out[102]: 5
Run Code Online (Sandbox Code Playgroud)

但这似乎并不相关,因为False总是为0,而且从上面的试验来看,它似乎不是输出的最大值.

我不能在这里诚实地看到这种模式,并且在文档中找不到任何东西(老实说,我真的不知道如何有效地寻找它).

那么,怎么做

int(x) [logical operation] int(y)
Run Code Online (Sandbox Code Playgroud)

在Python工作?

python

5
推荐指数
2
解决办法
3935
查看次数

Python中的逻辑运算符

在阅读python中的逻辑运算符时,我遇到了表达式:

  5 and 1 
Run Code Online (Sandbox Code Playgroud)

输出:1

  5 or 1 
Run Code Online (Sandbox Code Playgroud)

输出:5

任何人都可以解释这是如何工作的?
我知道,操作数的的逻辑运算符是布尔

python

4
推荐指数
1
解决办法
678
查看次数

将变量与多个值进行比较的简洁方法

我一直试图了解是否可以使用类似于我在下面演示的语句的 if 语句。我的理解是不是?

for i in range(10):
  if i == (3 or 5) or math.sqrt(i) == (3 or 5):
    numbers.append(i)
Run Code Online (Sandbox Code Playgroud)

使用这段代码,我只能得到数字3& 9,而我应该得到3, 5, 9. 是否有另一种方法可以不列出下面的代码?

for i in range(10):
  if i == 3 or i == 5 or math.sqrt(i) == 3 or math.sqrt(i) == 5:
    numbers.append(i)
Run Code Online (Sandbox Code Playgroud)

python if-statement

2
推荐指数
1
解决办法
80
查看次数