我想从数组中找到一个特定元素,并从该特定数组中删除所有下一个元素。
下面是我的代码:
import pandas as pd
import numpy as np
sample_col = ["col1","col2","col3","col4", "col5"]
sample_data = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]
sample = pd.DataFrame(data = sample_data, columns = sample_col)
m = [0,1,2,1]
marr = np.array(m)
for i in sorted(set(m)):
if i==0:
continue
sample.loc[marr==i] = sample.loc[marr==i].shift(i, axis=1)
我想输出如下:var res = [a,b,c];
谢谢,
答案 0 :(得分:1)
使用splice()
。您需要在data2
中获取data1
值的索引,然后使用该索引删除data1
之后的所有元素。
var data1 = ['a','b','c','d','e','f','g','h'];
var data2 = 'c';
data1.splice(data1.indexOf(data2)+1, data1.length);
console.log(data1);