你可以将count()用于整个列表而不是一个变量吗?

时间:2017-03-09 23:16:27

标签: python-3.x

有没有办法对整个列表使用count()而不必分别在每个变量上使用它?如果这是可能的话,它将为我节省很多打字。

var1 = random.randint(u,v)
var2 = random.randint(w,x)
var3 = random.randint(y,z)

listName = [var1,var2,var3]

listName.count(x)
listName.count(y) #can you get the count for an entire list instead of having to do them
listName.count(z) #all seperately? It would be much more efficient.

2 个答案:

答案 0 :(得分:0)

这是一个创建包含随机内容的列表然后显示长度和总和的示例。

03-10 08:12:38.665 13660-13660/com.example.mypc.inventory W/Bundle: Key id expected String but value was a java.util.HashMap.  The default value <null> was returned.
03-10 08:12:38.666 13660-13660/com.example.mypc.inventory W/Bundle: Attempt to cast generated internal exception:
                                                                    java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
                                                                        at android.os.BaseBundle.getString(BaseBundle.java:995)
                                                                        at android.content.Intent.getStringExtra(Intent.java:6243)
                                                                        at com.example.mypc.inventory.Inventory_View.onCreate(Inventory_View.java:59)
                                                                        at android.app.Activity.performCreate(Activity.java:6679)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
03-10 08:12:38.671 13660-13660/com.example.mypc.inventory D/SQLiteHandler: Fetching user from Sqlite: {}
03-10 08:12:38.675 13660-13660/com.example.mypc.inventory D/AndroidRuntime: Shutting down VM
03-10 08:12:38.676 13660-13660/com.example.mypc.inventory E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.mypc.inventory, PID: 13660
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypc.inventory/com.example.mypc.inventory.Inventory_View}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                at com.example.mypc.inventory.Inventory_View.onCreate(Inventory_View.java:61)
                                                                                at android.app.Activity.performCreate(Activity.java:6679)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

示例输出:

import random

my_list = [
  random.randint(1, 10),
  random.randint(1, 10),
  random.randint(1, 10)
]

print("The value of my_list is {0}".format(my_list))

print("The length of my_list is {0}".format(len(my_list)))

print("The sum of my_list is {0}".format(sum(my_list)))

这是你在找什么?

答案 1 :(得分:0)

list.count(item)返回item在列表中显示的次数。

如果您想知道每个项目在列表中显示的次数,如果它出现在列表中,您可以这样做:

original_list = [1, 1, 2, 3, 4, 4, 4]
uniques = list(set(original_list))

counts = {}
for unique in uniques:
    counts[unique] = original_list.count(unique)

print(counts)

应打印类似

的内容
{
    1: 2,
    2: 1,
    3: 1,
    4: 3
}

以下是set数据类型的更多信息:

https://docs.python.org/3/tutorial/datastructures.html#sets

虽然我们正在使用它,但您也可以使用collections.Counter

from collections import Counter
counts = Counter([1, 1, 2, 3, 4, 4, 4])
print(dict(counts))

这应该打印与上面相同的字典。