简体   繁体   English

python:用条件替换列表中的元素

[英]python: replace elements in list with conditional

I am trying to do the following with python and am having a strange behavior. 我试图用python做以下事情并且有一个奇怪的行为。 Say I have the following list: 说我有以下列表:

x = [5, 4, 3, 2, 1]

Now, I am doing something like: 现在,我正在做类似的事情:

x[x >= 3] = 3

This gives: 这给出了:

x = [5, 3, 3, 2, 1]

Why does only the second element get changed? 为什么只改变第二个元素? I was expecting: 我在期待:

[3, 3, 3, 2, 1]

Because Python will evaluated the x>=3 as True and since True is equal to 1 so the second element of x will be converted to 3. 因为Python会将x>=3评估为True ,因为True等于1所以x的第二个元素将转换为3。

For such purpose you need to use a list comprehension : 为此,您需要使用列表理解:

>>> [3 if i >=3 else i for i in x]
[3, 3, 3, 2, 1]

And if you want to know that why x >= 3 evaluates as True, see the following documentation : 如果您想知道为什么x >= 3计算结果为True,请参阅以下文档

CPython implementation detail: Objects of different types except numbers are ordered by their type names; CPython实现细节:除了数字之外的不同类型的对象按其类型名称排序; objects of the same types that don't support proper comparison are ordered by their address. 不支持正确比较的相同类型的对象按其地址排序。

In python-2.x and CPython implementation of course, a list is always greater than an integer type.As a string is greater than a list : 当然,在python-2.x和CPython实现中,列表总是大于整数类型。由于字符串大于列表:

>>> ''>[]
True

In Python-3.X, however, you can't compare unorderable types together and you'll get a TypeError in result. 但是,在Python-3.X中,您无法将不可共享的类型进行比较,并且您将在结果中获得TypeError

In [17]: '' > []
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-052e7eb2f6e9> in <module>()
----> 1 '' > []

TypeError: unorderable types: str() > list()

You're using python lists. 你正在使用python列表。 In python(2.x), comparison of a list with an int will compare the types , not the values. 在python(2.x)中, listint比较将比较类型 ,而不是值。 So, your comparison results in True which is equivalent to 1 . 因此,您的比较结果为True ,相当于1 In other words, your expression is equivalent to: 换句话说,你的表达式相当于:

x[1] = 3  # x[1] == x[True] == x[x > 3]

Note, python3.x disallows this type of comparison (because it's almost certainly not what you meant) -- And if you want to be doing this sort of operation, you almost certainly thought of it by looking at numpy documentation as the numpy API has been designed specifically to support this sort of thing: 注意,python3.x不允许这种类型的比较(因为它几乎肯定不是你的意思) - 如果你想做这种操作,你几乎肯定会通过查看numpy文档来考虑它,因为numpy API有专为支持此类事情而设计:

import numpy as np
array = np.arange(5)
array[array > 3] = 3

You can use this syntax with Numpy : 您可以将此语法与Numpy一起使用

>>> import numpy as np
>>> x = np.array([5, 4, 3, 2, 1])
>>> x[x>3]=3
>>> x
array([3, 3, 3, 2, 1])

You can also do this with Pandas : 你也可以用Pandas做到这一点:

>>> import pandas as pd
>>> x = pd.Series([5, 4, 3, 2, 1])
>>> x
0    5
1    4
2    3
3    2
4    1
dtype: int64
>>> x[x>3]=3
>>> x
0    3
1    3
2    3
3    2
4    1
dtype: int64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM