从列表中删除值
Using filter
nan
不是正确的 Python 语法,仅在Pandas
图书馆。所以我将用这个值替换None
.
我们可以使用filter
函数删除列表中包含的任何值nan
value.
test = list(filter(lambda x: None not in x, test))
使用列表理解
编辑:应该是x
代替test
。谢谢@fountainhead
test = [x 表示测试中的 x,如果 None 不在测试中]
test = [x for x in test if None not in x]
两者都会返回一个列表,其中的值不包含None
in them
Plotting
我们可以使用一个名为的库matplotlib
绘制图表。例如,要创建散点图:
import matplotlib.pyplot as plt
plt.scatter(x, y)
其中 plt.scatter 分别接受行和列的 x 值和 y 值。完整代码如下:
import matplotlib.pyplot as plt
test = [[1,2],[1,None],[3,4]]
test = list(filter(lambda x: None not in x, test))
x = [x for x,y in test]
y = [y for x,y in test]
# Assume all first elements are x values second elements are y values
plt.scatter(x, y)
plt.show()
示例图片如下