목록에서 요소의 빈도를 찾아야 합니다.
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
출력->
b = [4,4,2,1,2]
또한 중복된 항목을 제거하고 싶습니다.
a = [1,2,3,4,5]
파이썬 2.7 (이상) 에서 사용할 수 있습니다. ['콜레스티론스카운터'] [2]:
import collections
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)
print(counter)
# Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})
print(counter.values())
# [4, 4, 2, 1, 2]
print(counter.keys())
# [1, 2, 3, 4, 5]
print(counter.most_common(3))
# [(1, 4), (2, 4), (3, 2)]
파이썬 2.6 이상 사용하는 경우, 다운로드할 수 있습니다. here.
[2] https://docs.python.org/2/library/collections.html # 콜레스티론스카운터.
파이썬 2.7+ 수 있는 [콜레스티론스카운터] [1] 항목을 사용할 수 있습니다
>>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>>
>>> from collections import Counter
>>> c=Counter(a)
>>>
>>> c.values()
[4, 4, 2, 1, 2]
>>>
>>> c.keys()
[1, 2, 3, 4, 5]
[1]: http://docs.python.org/3.1/whatsnew/2.7.html # 사용되지 않는 모듈에서는 향상 및 새로운
from itertools import groupby
items = [5, 1, 1, 2, 2, 1, 1, 2, 2, 3, 4, 3, 5]
results = {value: len(list(freq)) for value, freq in groupby(sorted(items))}
{1: 4, 2: 4, 3: 2, 4: 1, 5: 2}
이 작업을 수행할 수 있습니다.
import numpy as np
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
np.unique(a, return_counts=True)
출력:
(array([1, 2, 3, 4, 5]), array([4, 4, 2, 1, 2], dtype=int64))
첫 번째 어레이입니다 는 이 값들을 사용하여, 두 번째 어레이입니다 값뿐만 요소의 숫자입니다.
그래서 그냥 어레이에는 afaq 스케쳐내 콩지름에 숫자는 이:
np.unique(a, return_counts=True)[1]
from collections import Counter
a=["E","D","C","G","B","A","B","F","D","D","C","A","G","A","C","B","F","C","B"]
counter=Counter(a)
kk=[list(counter.keys()),list(counter.values())]
pd.DataFrame(np.array(kk).T, columns=['Letter','Count'])
꼭 이래야겠어요 사용하여 간단히 스키피오스테스트리트럼프레크 다음과 같은 방법으로:
from scipy.stats import itemfreq
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
freq = itemfreq(a)
a = freq[:,0]
b = freq[:,1]
여기에 문서를 확인할 수 있습니다. 0/reference/generated/scipy.stats.itemfreq.html http://docs.scipy.org/doc/scipy-0.16
하지만 이 일이 있고 또 다른 말 나는 상당히 도움이 될 것입니다.
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
freq_list = []
a_l = list(set(a))
for x in a_l:
freq_list.append(a.count(x))
print 'Freq',freq_list
print 'number',a_l
이 생산할 예정이다.
Freq [4, 4, 2, 1, 2]
number[1, 2, 3, 4, 5]
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
# 1. Get counts and store in another list
output = []
for i in set(a):
output.append(a.count(i))
print(output)
# 2. Remove duplicates using set constructor
a = list(set(a))
print(a)
출력입니다
D:\MLrec\venv\Scripts\python.exe D:/MLrec/listgroup.py
[4, 4, 2, 1, 2]
[1, 2, 3, 4, 5]
def _fileIndex(fh):
''' create a dict using Counter of a
flat list of words (re.findall(re.compile(r"[a-zA-Z]+"), lines)) in (lines in file->for lines in fh)
'''
return Counter(
[wrd.lower() for wrdList in
[words for words in
[re.findall(re.compile(r'[a-zA-Z]+'), lines) for lines in fh]]
for wrd in wrdList])
from collections import OrderedDict
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
def get_count(lists):
dictionary = OrderedDict()
for val in lists:
dictionary.setdefault(val,[]).append(1)
return [sum(val) for val in dictionary.values()]
print(get_count(a))
>>>[4, 4, 2, 1, 2]
중복 제거 및 유지를 위해 주문.
list(dict.fromkeys(get_count(a)))
>>>[4, 2, 1]