获取数组中列表的出现-python

时间:2019-05-06 02:49:47

标签: python arrays numpy

a = np.array([1,2,4,2,3,4,1])
s = [1,2]

如何获取一个数组,该数组告诉我s中的元素是否存在于a中? 这就是我希望得到的:

[True, True, False, True, False, False, True]

3 个答案:

答案 0 :(得分:5)

使用np.isin

set

答案 1 :(得分:0)

O(2**n)

答案 2 :(得分:0)

没有麻木,你可以这样做

import numpy as np

a = np.array([1, 2, 4, 2, 3, 4, 1])
s = [1, 2]
r = [n in s for n in a]
print(r)  # [True, True, False, True, False, False, True]

如果stopMapActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_stop_map); nextBtn = findViewById(R.id.addRouteButton); FragmentManager fragmentManager = getFragmentManager(); MapFragment mapFragment = (MapFragment) fragmentManager .findFragmentById(R.id.map); mapFragment.getMapAsync(this); ssm = new ServerSideMethods(this,false); stop_list = new ArrayList<stop_list_item>(); new Thread(new Runnable(){ @Override public void run() { Message msg = new Message(); msg.what = 0; try{ stop_list = ssm.getCarStopList(17900); msg.what = 1; for (int i = 0; i < stop_list.size(); i++) { MarkerOptions makerOptions = new MarkerOptions(); makerOptions .position(new LatLng(stopListArray.get(i).get("x"), stopListArray.get(i).get("y"))) .title(stopListArray.get(i).get("stop_name")); mGoogleMap.addMarker(makerOptions); } }catch (Exception e){ Log.e("[HB]"+this.getClass().getName(),"Error:"+e.toString()); msg.what = 0; } } }).start(); } 较大,则a = [1,2,4,2,3,4,1] s = [1,2] t = list(map(lambda a: a in s, a)) 更为有效

s