Python:每日一题 133
本帖最后由 冬雪雪冬 于 2018-1-10 19:46 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
已有一个列表如下
list1 =
由此生成新的列表,要求为一个2维列表,包括list1的每个不重复元素和在list1中出现的次数。例如12在list1中出现11次,就记为,最后按元素由小到大排序,生成如下的列表
[, , , , , , , , , , , , , , , , , , , , ] print(sorted([ for i in set(list1)])) list1 = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18, 17, 6, 16, 19, 12, 18, 4, 4,
16, 12, 13, 11, 0, 0, 12, 7, 10, 2, 7, 12, 4, 18, 13, 18, 16, 17, 14, 12, 1, 12, 4, 14, 8,
18, 4, 12, 10, 1, 18, 7, 2, 9, 3, 11, 12, 2, 9, 13, 12, 18, 15, 1, 9, 5, 11, 6, 12, 9, 16,
14, 18, 0, 15, 4, 17, 5, 1, 13, 0, 10, 0, 6, 16, 15, 3, 16, 20, 18, 4, 18, 16, 3]
new_list = set(list1)
final_list = []
for i in new_list:
num_group =
final_list.append(num_group)
print(final_list) # 查找列表中每个元素出现的次数
# list_original原始列表
# list_target 目标输出
# list_temp 转换用列表,用于存放原始列表中不重复的元素
# list_cell 转换用列表,用于存放原始列表中出现的元素和次数
list_original = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18,
17, 6, 16, 19, 12, 18, 4, 4, 16, 12, 13, 11, 0, 0, 12, 7, 10,
2, 7, 12, 4, 18, 13, 18, 16, 17, 14, 12, 1, 12, 4, 14, 8, 18,
4, 12, 10, 1, 18, 7, 2, 9, 3, 11, 12, 2, 9, 13, 12, 18, 15, 1,
9, 5, 11, 6, 12, 9, 16, 14, 18, 0, 15, 4, 17,5, 1, 13, 0, 10,
0, 6, 16, 15, 3, 16, 20, 18, 4, 18, 16, 3]
list_target = []
list_temp = []
list_cell = []
#将原始列表从小到大排序
list_original.sort()
#将元素列表中不重复的元素找出来,存放起来
for i in list_original:
if i not in list_temp:
list_temp.append(i)
#print(list_temp)
#循环查找每个元素出现的次数
for i in list_temp:
list_cell =
#将查找结果存放到列表中
list_target.append(list_cell)
print(list_target)
list1 = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18, 17, 6,
16, 19, 12, 18, 4, 4, 16, 12, 13, 11, 0, 0, 12, 7, 10, 2, 7, 12, 4,
18, 13, 18, 16, 17, 14, 12, 1, 12, 4, 14, 8, 18, 4, 12, 10, 1, 18,
7, 2, 9, 3, 11, 12, 2, 9, 13, 12, 18, 15, 1, 9, 5, 11, 6, 12, 9,
16, 14, 18, 0, 15, 4, 17, 5, 1, 13, 0, 10, 0, 6, 16, 15, 3, 16,
20, 18, 4, 18, 16, 3]
set1 = set(list1)
TT = []
for i in set1:
d= list1.count(i)
TT.append()
print(TT)
list1 =
print([ for i in set(list1)])
# set好像是自动按从小到大排序的? def fun133(list_x):
result = []
for i in range(min(list_x),max(list_x)+1):
result.append()
return result
if __name__ == '__main__':
print(fun133())
set1 = set(list1)
list2 = []
for i inset1:
list2 .append()
print(list2) list1 =
list1.sort()
list2 = []
for i in range(max(list1)+1):
n = list1.count(i)
list2.append()
print(list2)
好像很久不见题目了,多写几个{:10_333:}
from collections import Counter
def fun(lst):
return sorted(Counter(lst).most_common())
def fun2(lst):
counter = Counter(lst)
return [] for i in counter]
def fun3(lst):
return [ for i in set(lst)] from collections import Counter
print(sorted([ for i,j in Counter(list1).most_common()])) list1 = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18, 17, 6, 16, 19, 12, 18, 4, 4, 16, 12, 13, 11, 0,
0, 12, 7, 10, 2, 7, 12, 4, 18, 13, 18, 16, 17, 14, 12, 1, 12, 4, 14, 8, 18, 4, 12, 10, 1, 18, 7, 2, 9, 3, 11,
12, 2, 9, 13, 12, 18, 15, 1, 9, 5, 11, 6, 12, 9, 16, 14, 18, 0, 15, 4, 17, 5, 1, 13, 0, 10, 0, 6, 16, 15, 3,
16, 20, 18, 4, 18, 16, 3]
list1.sort()
set1 = set(list1)
result = []
for each in set1:
result.append()
print(result)
结果:[, , , , , , , , , , , , , , , , , , , , ]
先排序列表 然后去重 在找出出现的位置 本帖最后由 万事屋 于 2018-1-6 13:57 编辑
用堆写的。。也不知道名称和写法正不正规
def dsort(ll):
d=[]
ld=1
for i in ll:
ss=0
while 1:
if d==i:
d+=1
break
elif d>i:
if d==-1:
d.append()
d=ld
ld+=1
break
else:
ss=d
elif d<i:
if d==-1:
d.append()
d=ld
ld+=1
break
else:
ss=d
return d
def showlist(d,ss):
if ss==-1:
return []
else:
return showlist(d,d)+]+showlist(d,d)
ll=
ff=dsort(ll)
ans=showlist(ff,0)
print(ans) list2=
#创建集合,存放不重复数字
j=set()
#遍历list2
for x in list2:
j.add(x)
#将集合转换成列表
list3=list(j)
#排序
list3.sort()
print (list2)
print(list3)
#创建列表5存放结果
list5=[]
#print (list4)
for y in list3:
#统计数字出现的次数
a=list2.count(y)
#print (a)
#创建新列表4存放数字和出现的次数
list4 = []
list4.append(y)
list4.append(a)
#将列表4添加到列表5中
list5.append(list4)
print (list5)
list1 = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18, 17, 6, 16, 19, 12, 18, 4, 4, 16, 12, 13,
11, 0, 0, 12, 7, 10, 2, 7, 12, 4, 18, 13, 18, 16, 17, 14, 12, 1, 12, 4, 14, 8, 18, 4, 12, 10, 1, 18, 7,
2, 9, 3, 11, 12, 2, 9, 13, 12, 18, 15, 1, 9, 5, 11, 6, 12, 9, 16, 14, 18, 0, 15, 4, 17, 5, 1, 13, 0, 10,
0, 6, 16, 15, 3, 16, 20, 18, 4, 18, 16, 3]
answer=[]
list2=list(set(list1))
list2.sort()
for i in list2:
answer.append()
print(answer) 本帖最后由 timeislife 于 2018-1-7 09:59 编辑
list1 = [12, 1, 3, 19, 0, 1, 16, 8, 19, 10, 6, 10, 0, 4, 3, 18, 2, 18, 17, 6,16,
19, 12, 18, 4, 4, 16, 12, 13, 11, 0, 0, 12, 7, 10,2, 7, 12, 4, 18, 13,
18, 16, 17, 14,12, 1, 12, 4, 14, 8, 18, 4, 12, 10, 1, 18, 7, 2, 9, 3,
11, 12, 2, 9, 13,12, 18, 15, 1, 9, 5, 11, 6, 12, 9, 16, 14,18, 0, 15,
4, 17, 5, 1, 13, 0, 10, 0, 6, 16, 15, 3, 16, 20, 18, 4, 18,16, 3]
temp = sorted(list(set(list1)))
returnedlist = [ for each in temp]
print(returnedlist) list1 =
>>> temp=[]
>>> for each in list1:
temp.append(list1.count(each))
>>> list2=sorted(zip(list1,temp))
>>> print(list2) 321123 发表于 2018-1-7 11:34
list1 =
但是我的输出是[(0, 7), (0, 7), (0, 7), (0, 7), (0, 7), (0, 7), (0, 7), (1, 6), (1, 6), (1, 6), (1, 6), (1, 6), (1, 6), (2, 4), (2, 4), (2, 4), (2, 4), (3, 5), (3, 5), (3, 5), (3, 5), (3, 5), (4, 8), (4, 8), (4, 8), (4, 8), (4, 8), (4, 8), (4, 8), (4, 8), (5, 2), (5, 2), (6, 4), (6, 4), (6, 4), (6, 4), (7, 3), (7, 3), (7, 3), (8, 2), (8, 2), (9, 4), (9, 4), (9, 4), (9, 4), (10, 5), (10, 5), (10, 5), (10, 5), (10, 5), (11, 3), (11, 3), (11, 3), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (12, 11), (13, 4), (13, 4), (13, 4), (13, 4), (14, 3), (14, 3), (14, 3), (15, 3), (15, 3), (15, 3), (16, 8), (16, 8), (16, 8), (16, 8), (16, 8), (16, 8), (16, 8), (16, 8), (17, 3), (17, 3), (17, 3), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (18, 11), (19, 3), (19, 3), (19, 3), (20, 1)] 怎么解决能让他只出现一次{:5_94:} temp=[]
>>> for each in list1:
temp.append(list1.count(each))
>>> list2=sorted(zip(list1,temp))
>>> list3=[]
>>> for each in list2:
if each not in list3:
list3.append(each)
>>> list3