TypeError:'<' ' int'的实例之间不受支持和'列出'在numpy intersect1d

时间:2018-01-19 20:12:16

标签: python python-3.x pandas numpy

我想在两个列表,手机和电话之间应用相交phone_office。所以我写了以下代码

phone=df_final_naFill.iloc[0,4]
type(phone) # List
s=pd.Series(phone)
type(s) #pandas.core.series.Series
a=pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list))
phone_office=df_final_naFill.iloc[0,6]
# type(phone_office) #List
h=pd.Series(phone_office)
phone_comb=np.intersect1d(a,h)

但是在运行代码之后,我收到了以下错误消息

  File "<ipython-input-206-3512341621de>", line 1, in <module>
    phone_comb=np.intersect1d(a,h)

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 337, in intersect1d
    aux.sort()

TypeError: '<' not supported between instances of 'int' and 'list'

前5行手机看起来像

0    [735015372, 72151508105, 7217511580, 721150431...
1                                                   []
2    [735152771, 7351515043, 7115380870, 7115427...
3    [7111332015, 73140214, 737443075, 7110815115...
4    [718218718, 718221342, 73551401, 71811507...

我看起来像

      1541291
      1011248
      1015925
      1013535
      1093131
      1125310
      1154955
      1158590
      1103552
      1121881

和看起来像

[92972897]
[92020181]
[92038083]
[92083771]
[92611383]
[93290963]
[93262309]
[92966917]
[93181110]
[93396990]
[93186911]
[92011329]

虽然类型&amp; h显示为pandas.core.series.Series。我猜测这种类型的格式是在一些迭代中出现的。大部分时间我的代码运行没有任何错误。

你能否告诉我在python 3.x中需要做些什么改变?

1 个答案:

答案 0 :(得分:2)

如果两个public class BlankFragment2 extends Fragment { private ArrayList<songInfo> _songs = new ArrayList<songInfo>(); RecyclerView recyclerView; SeekBar seekBar; Button play; public songAdapter songAdapter1; protected ImageView album_art; MediaPlayer mediaPlayer; private Handler myHandler = new Handler(); public Cursor cursor; View rootView; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private BlankFragment.OnFragmentInteractionListener mListener; public BlankFragment2() { // Required empty public constructor/ } // TODO: Rename and change types and number of parameters public static BlankFragment newInstance(String param1, String param2) { BlankFragment fragment = new BlankFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView= inflater.inflate(R.layout.fragment_blank_fragment2, container, false); recyclerView = rootView. findViewById(R.id.recyclerView); seekBar = rootView.findViewById(R.id.seekBar); album_art = rootView.findViewById(R.id.albumArt); songAdapter1 = new songAdapter(getActivity(),_songs); recyclerView.setAdapter(songAdapter1); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.addItemDecoration(dividerItemDecoration); songAdapter1.setOnItemClickListener(new songAdapter.OnItemClickListener() { @Override public void onItemClick(RecyclerView.ViewHolder holder, View view, final songInfo obj, int position) { if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; }else { Runnable runnable = new Runnable() { @Override public void run() { try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(obj.getSongUrl()); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); seekBar.setProgress(0); seekBar.setMax(mediaPlayer.getDuration()); Log.d("Prog", "run: " + mediaPlayer.getDuration()); } }); }catch (Exception e){} } }; myHandler.postDelayed(runnable,100); } } }); checkUserPermission(); Thread t = new runThread(); t.start(); return rootView; } public class runThread extends Thread { @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.d("Runwa", "run: " + 1); if (mediaPlayer != null) { seekBar.post(new Runnable() { @Override public void run() { seekBar.setProgress(mediaPlayer.getCurrentPosition()); } }); Log.d("Runwa", "run: " + mediaPlayer.getCurrentPosition()); } } } } private void checkUserPermission(){ if(Build.VERSION.SDK_INT>=23){ if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},123); return; } } loadSongs(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 123: if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ loadSongs(); }else{ Toast.makeText(getContext(), "Permission Denied", Toast.LENGTH_SHORT).show(); checkUserPermission(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } private void loadSongs() { Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"; cursor = getContext().getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); String id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); songInfo s = new songInfo(name, artist, url,id); _songs.add(s); } while (cursor.moveToNext()); cursor.close(); recyclerView.setAdapter(songAdapter1); } } } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (BlankFragment.OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { void onFragmentInteraction(Uri uri); } } songAdapter public class songAdapter extends RecyclerView.Adapter<songAdapter.SongHolder>{ private ArrayList<songInfo> _songs = new ArrayList<>(); private Context context; MediaMetadataRetriever metaRetriver; byte[] art; private OnItemClickListener mOnItemClickListener; public songAdapter(Context context, ArrayList<songInfo> songs) { this.context = context; this._songs = songs; } public interface OnItemClickListener { void onItemClick(RecyclerView.ViewHolder holder, View view, songInfo obj, int position); // void onItemClick(Button b, View view, songInfo obj, int position); } public void setOnItemClickListener(final OnItemClickListener mItemClickListener) { this.mOnItemClickListener = mItemClickListener; } @Override public SongHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View myView = LayoutInflater.from(context).inflate(R.layout.card,viewGroup,false); return new SongHolder(myView); } @Override public void onBindViewHolder(final SongHolder songHolder, final int i) { final songInfo s = _songs.get(i); metaRetriver = new MediaMetadataRetriever(); metaRetriver.setDataSource(_songs.get(i).getId()); try { art = metaRetriver.getEmbeddedPicture(); Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length); songHolder.album_art.setImageBitmap(songImage); } catch (Exception e) { songHolder.album_art.setBackgroundColor(Color.GRAY); } songHolder.tvSongName.setText(_songs.get(i).getSongName()); songHolder.tvSongArtist.setText(_songs.get(i).getArtistName()); setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(RecyclerView.ViewHolder holder, View view, songInfo obj, int position) { if (mOnItemClickListener != null) { mOnItemClickListener.onItemClick(songHolder, view, s, i); } } }); } @Override public int getItemCount() { return _songs.size(); } public class SongHolder extends RecyclerView.ViewHolder { TextView tvSongName,tvSongArtist; ImageView album_art; Button play; public SongHolder(View itemView) { super(itemView); play=(Button)itemView.findViewById(R.id.play); tvSongName = (TextView) itemView.findViewById(R.id.songName); tvSongArtist = (TextView) itemView.findViewById(R.id.artistName); album_art = (ImageView) itemView.findViewById(R.id.albumArt); } } } 没有嵌套,您可以大量简化代码:

list

如果需要比较原始phone_comb=np.intersect1d(phone, phone_office) 中的每一行:

DataFrame