簡體   English   中英

列出數組名稱

[英]Listing names to arrays

o = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5]

我希望能夠顯示多少個1、2等。

例如,如果有6 1 ,它將打印BendingF = 6 我的12345是不同的位置。 1 = BendingF2 = BendingM3 = Twisting4 = Walking5 = Squat

我試過了

##1 = print('Bending Forward')
##2 = print('Bending Midway')
##3 = print('Twisting')
##4 = print('Walking')
##5 = print('Squating')

但這會給我錯誤:

SyntaxError:無法分配給文字

正如@Amadan提到的那樣,您可以使用Counter來計數數組中每次出現的唯一數字。 然后創建一個字典( labels ),將您的整數映射到它們將代表的字符串值:

from collections import Counter

o = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5]

labels = {
  1: 'Bending Forward',
  2: 'Bending Midway',
  3: 'Twisting',
  4: 'Walking',
  5: 'Squating'
}

count = Counter(o)

for val in count.keys():
  print(labels[val] + " - " + str(count[val]))

產出

 Bending Forward - 9
 Bending Midway - 8
 Twisting - 16
 Walking - 11
 Squating - 8

復制鏈接

o = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5]
print("Bending forward = " + str(o.count(1)))
print("Bending Midway = " + str(o.count(2)))
print("Twisting = " + str(o.count(3)))
print("Walking = " + str(o.count(4)))
print("Squatting = " + str(o.count(5)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM