我有一个 8000 元素的一维数组。
我想获得以下两个数组:
test
contains the element with the index from[1995:1999]
,[3995:3999]
,[5999:5999]
,[7995:7999]
.train
应该包含其他所有内容。
我该怎么做?
idx = [1995,1996,1997,1998, 1999, 3995, 3996, 3997,3998, 3999, 5995, 5996, 5997, 5998, 5999, 7995, 7996, 7997, 7998, 7999]
test = [X[i] for i in idx]
train = [X[i] for i **not** in idx]
原文由 wrek 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据您的示例,一个简单的解决方法是: