如何查找列表列表的元素的频率?

时间:2018-02-22 11:58:44

标签: python

我试图获取列表中每个列表的频率。 以下是我的代码

my_list=[1,2,3]

此代码适用于数字列表,即ctr[[1,2,3]]。但是对于列表列表它不起作用。 2应打印 TypeError: unhashable type: 'list' ,但错误如下

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=""
tools:layout_editor_absoluteY="81dp">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="opening game"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <NumberPicker
        android:id="@+id/numberPicker2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="14dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/GameTitleET"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <EditText
        android:id="@+id/GameTitleET"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="153dp"
        android:ems="10"
        android:hint="Game's name"
        android:inputType="textPersonName"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/GameContentET"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="מהלך המשחק"
        android:inputType="textMultiLine"
        android:maxLines="8"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeleteAndMakeYOurOwnButton" />

    <Button
        android:id="@+id/DeleteAndMakeYOurOwnButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:text="Write your own game"
        app:layout_constraintStart_toStartOf="@+id/GameContentET"
        app:layout_constraintTop_toBottomOf="@+id/numberPicker2" />

    <Switch
        android:id="@+id/SendToReviewSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="7dp"
        android:layout_marginStart="16dp"
        android:text="הגש משחק להגשה"
        app:layout_constraintBottom_toTopOf="@+id/DeleteAndMakeYOurOwnButton"
        app:layout_constraintStart_toStartOf="parent" />

    <include
        android:id="@+id/include5"
        layout="@layout/activity_create_post_methods"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/GameContentET" />
</android.support.constraint.ConstraintLayout></ScrollView>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

一个解决方案:

public Observable<Bitmap> fetchBitmap(String url) {
    Observable<Bitmap> bitmapObservable = mGenericApiService.getBitmap(url)
        .map(responseBody -> {
          Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = Bitmap.createBitmap(50, 50, conf);

          // Get a Canvas for the Bitmap
          Canvas  canvas = new Canvas(bitmap);

          // Read the SVG file
          SVG svg = SVGParser.getSVGFromInputStream(inputstream);
          // There are other ways to read an SVG file. See the SVGParser class for the others.

          // Get a Picture from the SVG and render it to the Canvas
          canvas.drawPicture(SVG.getPicture());

          return bitmap;
        }).doOnError(getUniversalErrorHandler(mContext, mEventBus));
    return bitmapObservable;
}

<强>解释

  • from collections import Counter lst = [[1,2,3],[1,2,3]] ctr = Counter(map(tuple, lst)) ctr[(1,2,3)] # 2 个实例是字典,因此需要不可变 键。元组是不可变的,而列表不是。
  • 此外,Counter个实例接受任何可迭代的,而不仅仅是列表。
  • 懒惰地执行元组转换意味着您不必创建 一个新的清单。

答案 1 :(得分:1)

问题是Counter创建了一个字典,你不能拥有一个类型为list的字典。因此,当您提供列表列表时,它会失败。要解决此问题,请将内部列表转换为元组,如下所示:

from collections import Counter

my_list  = [[1, 2, 3], [1, 2, 3]]
new_list = [tuple(x) for x in my_list]  # or overwrite with my_list = ..

ctr = Counter(new_list)    
print(ctr[(1, 2, 3)])  # -> 2
相关问题