根据现有值更改列表中的值

时间:2017-10-23 17:45:04

标签: python list replace expression

我有一个包含数据框数据类型的列表。我想根据现有值更改列表中的值。

listnew = ["Double" if x == "dtype('float64')" else "Long" for x in list]

我想要一个新的列表,其值为" Double"对于值" dtype(' float64')"和#34; Long"其他任何事情。我试过这个表达但是没有用。

update table1 t1 set col1=(select coalesce((select t2.col2 from table1 t2 where t1.col3=t2.col4 limit 1), null));

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

import numpy as np

list_ = [np.dtype(np.int64) for n in range(18)] + [np.dtype(np.float64) for n in range(6)]

listnew = ["Double" if np.dtype(x) == np.dtype(np.float64) else "Long" for x in list_]

print(listnew)

哪里

list_ = [np.dtype(np.int64) for n in range(18)] + [np.dtype(np.float64) for n in range(6)]

等同于您的给定列表:

list_ = [np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.int64),   np.dtype(np.int64),   np.dtype(np.int64),   \
         np.dtype(np.float64), np.dtype(np.float64), np.dtype(np.float64), \
         np.dtype(np.float64), np.dtype(np.float64), np.dtype(np.float64)]

打印listnew时的输出如下:

['Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Long', 'Double', 'Double', 'Double', 'Double', 'Double', 'Double']