Python:用条件替换列表中的元素 您所在的位置:网站首页 列表替换元素使用什么运算符 Python:用条件替换列表中的元素

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

2023-03-28 15:36| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

I am trying to do the following with python and am having a strange behavior. 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.

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 :

CPython implementation detail: Objects of different types except numbers are ordered by their type names; 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 :

>>> ''>[] True

In Python-3.X, however, you can't compare unorderable types together and you'll get a TypeError in result.

In [17]: '' > [] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 '' > [] TypeError: unorderable types: str() > list() 其他推荐答案

You can use this syntax with 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:

>>> 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 其他推荐答案

You're using python lists. In python(2.x), comparison of a list with an int will compare the types, not the values. So, your comparison results in True which is equivalent to 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:

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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有