如何在Python中重新排序列表以避免重复元素?

时间:2015-10-15 06:06:51

标签: python list duplicates sequence

我正在尝试检查列表是否有任何连续的重复元素,然后重新排序,以避免重复。如果那是不可能的,那么返回False。例如:

 private class ImageLoader extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(NewsAndImages... params) {
            NewsAndImages container = params[0];
            News news = container.news;
            try {
                if (container.position > 0) {
                    InputStream in = (InputStream) new URL(news.getImage150()).getContent();
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                    return bitmap;
                } else {
                    InputStream in = (InputStream) new URL(news.getRealImage()).getContent();
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                    return bitmap;
                }

            } catch (Exception e) {

                Log.v("LOGTAG", e + " streaming pic"+news.getImage150());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            try {
                if (bitmap !=null) {
                ImageView imageView = (ImageView) newsAndImages.view.findViewById(R.id.newsListImage);
                imageView.setImageBitmap(bitmap);
                newsAndImages.news.setBitmap(bitmap);
                }else {
                    ImageView imageView = (ImageView) newsAndImages.view.findViewById(R.id.newsHeadLineImage);
                    imageView.setImageBitmap(bitmap);
                    newsAndImages.news.setBitmap(bitmap);
                }
                bitmap.recycle();
            } catch (Exception e) {

                Log.v("LOGTAG", e + " post exe");
            }
        }

    }

这就是我所拥有的。有更优雅的解决方案吗?

checkRepeat([1,2])
Out[61]: [1, 2]

checkRepeat([1,2,2])
Out[62]: [2, 1, 2]

checkRepeat([1,2,2,1,1])
Out[63]: [1, 2, 1, 2, 1]

checkRepeat([1,2,2,1,1,3,3,3,3])
Out[64]: [1, 3, 1, 3, 2, 1, 3, 2, 3]

checkRepeat([1,2,2,1,1,3,3,3,3,3])
Out[65]: [3, 1, 3, 2, 3, 1, 3, 1, 3, 2]

checkRepeat([1,2,2,1,1,3,3,3,3,3,3])
Out[66]: [3, 1, 3, 1, 3, 1, 3, 2, 3, 2, 3]

checkRepeat([1,2,2,1,1,3,3,3,3,3,3,3])
Out[67]: False

2 个答案:

答案 0 :(得分:0)

您可以尝试概率方程式

重复次数最多的数字必须始终少于其他数字的数量。

[1,2,2,1,1,3,3,3,3,3,3,3]
 7< (3+2) false

[1,2,2,1,1,3,3,3,3,3,3]
 6< (3+2)  true

 [1,2,2,1,1,3,3,3,3,3]
 5< (3+3)  true

代码

from itertools import groupby
>>> a =[1,2,2,1,1,3,3,3,3,3]
>>> s = [len(list(group)) for key, group in groupby(a)]
>>> s
[1, 2, 2, 5]
>>> max(s) < (sum(s)-max(s))
True

答案 1 :(得分:0)

以下是我在实施的评论中提到的算法,使用非常有用的collections.Counter类:

from collections import Counter

def check_repeat(sequence):
    if not sequence:
        return []
    element_counts = Counter(sequence)
    new_sequence = []
    elements_chosen = 0
    elements_needed = len(sequence)
    previous_element_chosen = None
    while elements_chosen < elements_needed:
        candidates_needed = 1 if previous_element_chosen is None else 2
        candidates = element_counts.most_common(candidates_needed)
        candidate = (candidates[0] if 
            (len(candidates) < 2 or candidates[0][0] != previous_element_chosen)
            else candidates[1])
        if candidate[1] <= 0:
            return False
        else:
            new_sequence.append(candidate[0])
            element_counts[candidate[0]] -= 1
            previous_element_chosen = candidate[0]
            elements_chosen += 1
    return new_sequence

如果None是序列中的有效值,或者您关心任何程度的稳定性,则需要进行一些改进。如果序列中的元素不可清除,则根本不起作用。

这个三元candidate = ...任务可能更清晰一些。