[英]Count different numbers in list python
大家好,這是我第一年編程,我從python開始。 我對編程非常了解,但在此作業問題上需要幫助。
我必須使用列表作為我的參數,然后返回列表中不同值的數量。 問題中的示例列表為[1, 4, 1, 7, 6, 1, 4, 3]
,因此返回值應為5。
現在,我知道我的解決方法可能並不簡潔或優雅,但是如果有人可以幫助我並告訴我要進行哪些更改以使其可行,我將不勝感激。
def count(mylist):
newlist = []
newlist.append(mylist[0])
stor = False
for i in mylist:
stor = False
for j in newlist:
if j == i:
stor == True
if not stor:
newlist.append(i)
return newlist
使用set()
代替:
def count(myList):
return len(set(myList))
一組將僅保存每個值的一個副本,因此將列表轉換為一組具有刪除所有重復項的便捷副作用。 結果集的長度就是您要尋找的答案。
使用集合是最有效的方法。 或者,您也可以使用dict() :
def count(myList):
return len(dict.fromkeys(myList))
效率稍差一些,因為它將為與鍵關聯的值保留空間。
如果你想用一個列表,(效率最低),使用not in
負成員資格測試:
def count(myList):
unique = []
for item in myList:
if item not in unique:
unique.append(item)
return len(unique)
您可以在這里使用集:
In [1]: lis=[1, 4, 1, 7, 6, 1, 4, 3]
In [2]: len(set(lis))
Out[2]: 5
幫助上set
:
set(iterable) -> new set object
Build an unordered collection of unique elements.
使用for循環:
In [6]: def count(lis):
...: mylis=[]
...: for elem in lis:
...: if elem not in mylis: # append the element to
# mylis only if it is not already present
...: mylis.append(x)
...: return len(mylis)
...:
In [7]: count(lis)
Out[7]: 5
還可以看看collections.Counter()
,它返回dict
的子類,其中包含重復元素的次數:
In [10]: from collections import Counter
In [11]: c=Counter(lis)
In [12]: c
Out[12]: Counter({1: 3, 4: 2, 3: 1, 6: 1, 7: 1})
In [13]: len(c)
Out[13]: 5
stor == True
您實際上不是在這里將stor
設置為True
。
如果無法使用set
,請嘗試
def count(mylist):
mylist.sort()
total = 0
for k in range(1, len(mylist) -1 ):
if mylist[k] != mylist[k + 1]:
total += 1
return total
這將對列表進行排序,然后在每次元素與下一個元素不相等時遞增計數器。
如果不能使用排序,通常會跟蹤計數值。 但是,這太明顯了,因此這是一種有趣的方法,無需保留您已經計算在內的值列表:
def count(mylist):
total = 0
for k, value in enumerate(mylist):
total += 1 / mylist.count(value)
return total
因此,對於[1, 4, 1, 7, 6, 1, 4, 3]
1、4、1、7、6、1、4、3 [1, 4, 1, 7, 6, 1, 4, 3]
,權重為[1/3, 1/2, 1/3, 1, 1, 1/3, 1/2, 1]
總計應為5
。
這是比您的老師正在尋找的方法更加出色的方法(盡管這種方法效率低下)。
如果您應該使用循環,那么這就是方法(或其中之一)。 :)
the_list = [1, 4, 1, 7, 6, 1, 4, 3]
def count(the_list):
unique_list = []
for item in the_list:
if item not in unique_list:
unique_list.append(item)
return len(unique_list)
首先,是程序的固定版本
def count(mylist):
newlist = []
newlist.append(mylist[0])
stor = False
for i in mylist:
stor = False
for j in newlist:
if j == i:
stor = True # stor == True test for equality
if not stor:
newlist.append(i)
return len(newlist) # you do not want the list itself but its length
這里有一些建議:
stor
。 稍后再兩行完成此操作。 break
內循環的break
-這樣可以加快速度(無需進行不必要的比較) newlist
可以初始化為空列表,而無需附加第一項。 該算法保持有效(內部循環首次具有零次迭代) 這里作為代碼示例:
def count(mylist):
newlist = []
for i in mylist:
stor = False
for j in newlist:
if j == i:
stor = True
break
if not stor:
newlist.append(i)
return len(newlist)
更優雅(和pythonic):使用in
syntax;)
def count(mylist):
newlist = []
for i in mylist:
if i not in newlist:
newlist.append(i)
return len(newlist)
如果可以在something_iterable
找到item
,則基本上item in something_iterable
返回item
。 大多數項目集合都是可迭代的(例如列表,集合,字符串... 'a' in 'abc'
返回true)
和最pythonic的方式,但沒有for / while循環:
def count(mylist):
return len(set(mylist))
請查看其他答案以獲得解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.