Python:每日一题 387
今天的题目:给定一个字符串,将字符串里的字符按照出现的次数降序排列,并返回排列后的字符串。
示例 1:
输入:"tree"
输出:"eert"
解释:'e' 出现两次,'r' 和 't' 都只出现一次。因此 'e' 必须出现在 'r' 和 't' 之前。
说明:此外,"eetr" 也是正确的答案。
示例 2:
输入:"cccaaa"
输出:"cccaaa"
解释:'c' 和 'a' 都出现三次。
说明:此外,"aaaccc" 也是有效的答案。
注意:"cacaca"是不正确的,因为相同的字母必须放在一起。
示例 3:
输入:"Aabb"
输出:"bbAa"
说明:此外,"bbaA" 也是正确的答案,但 "Aabb" 是不正确的。
{:10_298:}欢迎大家一起答题!{:10_298:} 占楼 class Solution:
def frequencySort(self, s: str) -> str:
s=sorted(s)
s.sort(key=collections.Counter(s).get,reverse=True)
return ''.join(s) 本帖最后由 sunrise085 于 2020-4-30 14:07 编辑
def fun387(string):
result=''
myset=set(string)
mylist=[]
for ch in myset:
mylist.append(())
mylist.sort(key=lambda ele:ele,reverse=True)
for list1 in mylist:
result+=list1*list1
return result from collections import Counter
def f387(s):
res,l = '',sorted(list(Counter(s).items()),reverse=True,key=lambda i: i)
for i in l:
res += i*i
return res 本帖最后由 Twilight6 于 2020-4-30 14:56 编辑
string = input('请输入一个字符串~:')
set_str = set(string)
count_dict = {}
for i in set_str:
count_dict = string.count(i)
new_count_list = sorted(list(zip(count_dict.keys(),count_dict.values())),key=lambda x:x)
for i in new_count_list[::-1]:
print(i*i,end='')
唉,我怎么感觉只会写暴力的{:10_284:} from collections import Counter
def f387(s):
return ''.join(*i for i in sorted(list(Counter(s).items()),reverse=True,key=lambda i: i)]) def func(string):
list1 =
list1.sort(key = lambda x: (string.count(x), x), reverse = True)
return ''.join(list1) 本帖最后由 March2615 于 2020-4-30 15:07 编辑
def daily387(s):
return ''.join(k * v for k, v in Counter(s).most_common()) 后排 本帖最后由 悠悠2264 于 2020-4-30 20:19 编辑
def fun387(s):
result = ''
d = {}
for i in s:
try:
d = d + 1
except:
d = 1
for c in range(len(s)):
t = None
for i in d:
t = i if t == None or d < d else t
for p in range(d):
result = result + t
d = 0
return result
def reverse_sort(string):
return_str = ""
length = len(set(string))
zero =
str_dict = dict(zip(set(string), zero))
for each in string:
str_dict += 1
sorted_str = sorted(str_dict.items(), key=lambda item:item, reverse=True)
for each in sorted_str:
for i in range(each):
return_str += each
return return_str 本帖最后由 qiuyouzhi 于 2020-4-30 16:33 编辑
随便写写
def func(str1):
return ''.join(sorted(str1, key = lambda c: str1.count(c), reverse = True))
改一下:
func = lambda str1: ''.join(sorted(str1, key = lambda c: str1.count(c), reverse = True)) March2615 发表于 2020-4-30 14:19
咱俩的好像{:10_257:} def timu387(s):
if s.strip() == '':
return ''
sa = set(s)
ra=''
da = {}
for i in sa:
da = s.count(i)
la = sorted(da.items(), key=lambda x: x, reverse=True)
for ik,iv in la:
ra = ra+ik*iv
return ra
print(timu387('tree'))
print(timu387('cccaaa'))
print(timu387('Aabb')) def f387(s):
sc = sorted(Counter(s).items(), key = lambda x: x, reverse=True)
return ''.join(val * c for val, c in sc) def f387(s):
tmp = set(s)
letter = list(tmp)
num = []
for i in letter:
num.append(s.count(i))
res = ""
for i in range(len(letter)) :
max_index = num.index(max(num))
res += num.pop(max_index) * letter.pop(max_index)
return res 凑热闹.png
代码如下:
def solve(s:str)->str:
d = {x:s.count(x) for x in set(s)}
return ''.join( for x in sorted(d.keys(),key=lambda a:d,reverse=1)])
if __name__ == '__main__':
print('示例1 输出:',solve("tree"))
print('示例2 输出:',solve("cccaaa"))
print('示例3 输出:',solve("Aabb"))
qiuyouzhi 发表于 2020-4-30 15:09
咱俩的好像
我给忘了在哪看到的了
里面讲的就是找最多最少字符,出现次数前几的字符,就用那个most_common()
本来想用sorted,但是我的reverse参数不知道为什么不能设置{:10_285:}切片又不好看,甚至能找到错误答案 def f387(s:str):
from collections import Counter
r =
return ''.join(i*j for i,j in r)