python中if语句的实例_在Python中映射if语句(Map an if statement in Python)

该博客讨论了如何在Python 2.7的pandas DataFrame中使用lambda函数实现带有if语句,如`lambdax: 1 if x=='C' else 0`,并提到了可能的错误和替代解决方案。作者分享了示例和常见问题解答,帮助读者避免语法错误。
摘要由CSDN通过智能技术生成

在Python中映射if语句(Map an if statement in Python)

我试图将以下函数映射到python 2.7中的一个pandas数据框(基本上是一个列表):

df["Cherbourg"] = df["Embarked"].map(lambda x: if (x == "C") 1 else 0)

但是,使用像这样的lambda函数的python错误是一个语法错误。 有没有办法在python中映射这样的if语句?

I'm trying to map the following function over a pandas dataframe (basically a list) in python 2.7:

df["Cherbourg"] = df["Embarked"].map(lambda x: if (x == "C") 1 else 0)

But python errors saying using a lambda function like this is a syntax error. Is there some way to map an if statement like this in python?

原文:https://stackoverflow.com/questions/29247718

更新时间:2020-01-13 06:31

最满意答案

尝试

lambda x: 1 if x == "C" else 0

示例:

map(lambda x: True if x % 2 == 0 else False, range(1, 11))

结果将是 - [假,真,假,真,假,真,假,真,假,真]

Try

lambda x: 1 if x == "C" else 0

Example :

map(lambda x: True if x % 2 == 0 else False, range(1, 11))

result will be - [False, True, False, True, False, True, False, True, False, True]

2017-05-23

相关问答

有几种方法可以做到这一点。 最明确的是: if width < 0 or width > 10000:

但我最喜欢的是: if not 0 <= width <= 10000:

There are a few ways to do it. The most explicit is this: if width < 0 or width > 10000:

but my favourite is this: if not 0 <= width <= 10000:

比较这个,不使用nonlocal : x = 0

def outer():

x = 1

def inner():

x = 2

print("inner:", x)

inner()

print("outer:", x)

outer()

print("global:", x)

# inner: 2

# outer: 1

# global: 0

对于这个,使用nonlocal ,其中inner()的x现在也是outer()的x :

...

你可以使用一个字典: def f(x):

return {

'a': 1,

'b': 2,

}[x]

You could use a dictionary: def f(x):

return {

'a': 1,

'b': 2,

}[x]

你的问题是:如果你的搜索找不到任何东西,它将返回None 。 你不能做None.group(1) ,这是你的代码所代表的。 相反,检查搜索结果是否为“ None - 不是搜索结果的第一个组。 import re

import string

chars = re.escape(string.punctuation)

sub='FW: Re: 29699'

search_result = re.search(r"^FW: (\w{10})", sub)

if search_result is n

...

是的,它在for语句中定义。 它只是列表中元素的占位符,可以被称为任何东西,例如 grocery_list = ['apples', 'bananas', 'oranges', 'milk']

for grocery in grocery_list:

print(grocery, len(grocery))

Yes it's defined within the for statement. It's just a placeholder for an element in the list

...

尝试 lambda x: 1 if x == "C" else 0

可能的重复有没有办法在Python的lambda中执行“if” 示例: map(lambda x: True if x % 2 == 0 else False, range(1, 11))

结果将是 - [假,真,假,真,假,真,假,真,假,真] Try lambda x: 1 if x == "C" else 0

possible duplicate of Is there a way to perform "if" in

...

这里有一个非常好的解释。 基本上,with语句在关联对象上调用两个特殊方法。 __enter__和__exit__方法。 enter方法返回与“with”语句关联的变量。 虽然__exit__方法在执行语句以处理任何清理(例如关闭文件指针)后被调用。 There's a very nice explanation here. Basically, the with statement calls two special methods on the associated object. The _

...

这是不可能的。 唯一的两个选项是(a)通过返回false-y值来传播异常,或者(b)通过返回True来吞下异常。 无法从抛出异常的位置恢复代码块。 无论哪种方式,你with阻止都结束了。 It's not possible. The only two options are (a) let the exception propagate by returning a false-y value or (b) swallow the exception by returning True. Ther

...

它实际上没有做任何事情,正如@Daniel在评论中所说的那样,它可以做任何事情的唯一方法是,如果self.current指的是属性方法 。 类似于以下内容: class X():

@property

def current(self):

mail_admins()

return whatever

def next(self):

...

这样,调用self.current ,实际上会做一些事情。 但是无论如何它绝对不被认为是

...

正如其他人所说, if False是一种阻止执行后代码的方法,那就是不好的做法。 那么为什么这是不好的做法呢? if False在运行时执行if False的条件, if False与注释相比会有轻微的开销。 使用它的一个原因可能是块内已经有三个引用的注释,因此注释可能不会很容易。 如今大多数编辑都使用颜色编码来表示注释,但是他们不够聪明,不能意识到代码没有被执行。 因此,在阅读代码时,除非您发现条件,否则代码被绕过并不明显。 事实上你必须来到这个网站询问这是什么意思说明了这一点! 这种做法可能是

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值