将python列表转换为numpy数组boolean

时间:2022-09-27 21:22:04

I have a big list in python like small example and want to make a numpy array which is boolean.

我在python中有一个很大的列表,就像小例子一样,想要制作一个boolean的numpy数组。

small example:

小例子:

li = ['FALSE', 'FALSE', 'TRUE', 'FALSE', 'FALSE', 'FALSE']

I tried to change it using the following line:

out = array(li, dtype = bool)

and then I got this output:

然后我得到了这个输出:

out = array([ True, True, True, True, True, True], dtype=bool)

but the problem is that they are all True. how can I make the same array but elements should remain the same meaning False should be False and True should be True in the new array.

但问题是他们都是真的。如何创建相同的数组,但元素应该保持相同的含义False应为False,True应在新数组中为True。

2 个解决方案

#1


4  

You can convert the strings to boolean literals using str.title and ast.literal_eval:

您可以使用str.title和ast.literal_eval将字符串转换为布尔文字:

import ast
import numpy as np

li = ['FALSE', 'FALSE', 'TRUE', 'FALSE', 'FALSE', 'FALSE']
arr = np.array([ast.literal_eval(x.title()) for x in li])
# array([False, False,  True, False, False, False], dtype=bool)

You could otherwise use a simple list comprehension if you have only those two in your list:

如果您的列表中只有这两个,那么您可以使用简单的列表理解:

arr = np.array([x=='TRUE' for x in li])
# array([False, False,  True, False, False, False], dtype=bool)

Keep in mind that non-empty strings are truthy, so coercing them to bool like you did will produce an array with all elements as True.

请记住,非空字符串是真实的,所以像你一样强制它们bool会生成一个所有元素都为True的数组。

#2


3  

bool('True') and bool('False') always return True because strings 'True'and 'False' are not empty

bool('True')和bool('False')总是返回True,因为字符串'True'和'False'不为空

You can create afunction to convert string to bool

您可以创建功能以将字符串转换为bool

def string_to_bool(string):
    if string == 'True':
        return True
    elif string == 'False':
        return False
>>> string_to_bool('True')
True

#1


4  

You can convert the strings to boolean literals using str.title and ast.literal_eval:

您可以使用str.title和ast.literal_eval将字符串转换为布尔文字:

import ast
import numpy as np

li = ['FALSE', 'FALSE', 'TRUE', 'FALSE', 'FALSE', 'FALSE']
arr = np.array([ast.literal_eval(x.title()) for x in li])
# array([False, False,  True, False, False, False], dtype=bool)

You could otherwise use a simple list comprehension if you have only those two in your list:

如果您的列表中只有这两个,那么您可以使用简单的列表理解:

arr = np.array([x=='TRUE' for x in li])
# array([False, False,  True, False, False, False], dtype=bool)

Keep in mind that non-empty strings are truthy, so coercing them to bool like you did will produce an array with all elements as True.

请记住,非空字符串是真实的,所以像你一样强制它们bool会生成一个所有元素都为True的数组。

#2


3  

bool('True') and bool('False') always return True because strings 'True'and 'False' are not empty

bool('True')和bool('False')总是返回True,因为字符串'True'和'False'不为空

You can create afunction to convert string to bool

您可以创建功能以将字符串转换为bool

def string_to_bool(string):
    if string == 'True':
        return True
    elif string == 'False':
        return False
>>> string_to_bool('True')
True