numpy argsort可以处理领带吗?

时间:2015-07-11 01:16:14

标签: python arrays sorting numpy

我有一个numpy数组:

package menu.saryal.example.com.menu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;


/**
 * A placeholder fragment containing a simple view.
 */
public class ItemOrderedFragment extends Fragment {

    private ArrayAdapter<String> itemTitle;
    public final static String  EXTRA_TEXT = "menu.saryal.example.com.menu";

    public ItemOrderedFragment getThisItemOrderedFragment() {
        return thisItemOrderedFragment;
    }

    ItemOrderedFragment thisItemOrderedFragment = this;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_ordered, container, false);

        final MenuDbHelper dbHandler = new MenuDbHelper(this.getActivity());
        ArrayList<menuDescription> data = dbHandler.readData();

        // Get a reference to the ListView, and attach this adapter to it.
        ListView listView = (ListView) rootView.findViewById(R.id.fragment_item_ordered_list_view);


      ArrayList<HashMap<String, String>> albumsList;
        albumsList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
        for(menuDescription x:data){
            map.put("Title", x.getTitle());
            map.put("Description", x.getDescription());
        }
        // adding HashList to ArrayList
        albumsList.add(map);

        ListAdapter adapter = new SimpleAdapter(
                this.getActivity(),
                albumsList,
                R.layout.list_item_with_image,
                new String[] { "Title", "Description"},
                new int[] {R.id.list_item_with_image_text_view, R.id.recepit_cost_text_view });
        if (data.size()>0){
            listView.setAdapter(adapter);
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String title = itemTitle.getItem(position);
                menuDescription item = dbHandler.findByTitle(title);
                String cost_string = "" + item.getCost();
                String totalCost_string = "" + item.totalCost;
                String totalOrder_string = "" + item.getTotalOrder();
                String[] transfer_data = {item.getTitle(),item.getDescription(),cost_string,totalCost_string,totalOrder_string,item.getImage()};
                Intent intent = new Intent(getActivity(), EditDetailPage.class);
                intent.putExtra(EXTRA_TEXT, transfer_data);
                startActivity(intent);
            }
        });
        dbHandler.close();
        return rootView;
    }
}

我想要前三项。调用

foo = array([3, 1, 4, 0, 1, 0])

返回

foo.argsort()[::-1][:3]

通知值array([2, 0, 4]) foo[1]相等,因此foo[4]通过返回数组中最后出现的项的索引来处理并列;即索引4。

对于我的应用程序,我不能让打破平局总是偏向数组的末尾,那么如何实现随机的平局?也就是说,我得到的时间有一半numpy.argsort(),而另一半我得array([2, 0, 4])

1 个答案:

答案 0 :(得分:4)

这是一种方法:

使用numpy.unique对数组进行排序并删除重复项。传递return_inverse参数以将索引放入已排序的数组中,该数组提供原始数组的值。然后,您可以通过查找反向数组的索引来获取绑定项的所有索引,该数组的值等于该项的唯一数组中的索引。

例如:

foo = array([3, 1, 4, 0, 1, 0])
foo_unique, foo_inverse = unique(foo, return_inverse=True)

# Put largest items first
foo_unique = foo_unique[::-1]
foo_inverse = -foo_inverse + len(foo_unique) - 1

foo_top3 = foo_unique[:3]

# Get the indices into foo of the top item
first_indices = (foo_inverse == 0).nonzero()

# Choose one at random
first_random_idx = random.choice(first_indices)

second_indices = (foo_inverse == 1).nonzero()
second_random_idx = random.choice(second_indices)

# And so on...

numpy.unique是使用argsort实现的,因此浏览一下它的实现可能会提出一种更简单的方法。