I've got a function called frequency
, which stores the occurrence of each element in a dictionary such that element:occurrence
. I use get_output
to format it properly by finding the length
of the individual elements of the list and grouping them together (by using +
operator).
lst = [-1,1,-6,4,5,-6,1,4,1]
def f(r): #to check if elements in the list are same
if r.count(r[0])==len(r):
return True
d={}
megalist=[]
def frequency(x):
for i in x:
d[i]=x.count(i)
for j in d:
n=d[j]
if n in d.values():
megalist.append([j for k in range(d[j])])
frequency(lst)
def get_output(y):
for a in y:
for b in y:
if a!=b:
if len(a)==len(b)and f(a) ==True and f(b)==True:
y[y.index(a)]=a+b
y.remove(b)
print(y)
get_output(megalist)
output:
[[-1, 5], [1, 1, 1], [-6, -6, 4, 4]]
UPDATE:
For this,
the order of each frequency group must be ordered from greatest number to least input
You could run a for
loop and sort the individual elements of the list (using "list".sort(reverse=True)
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…