鱼C论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: 冬雪雪冬

[技术交流] Python:每日一题 133

[复制链接]
发表于 2018-1-11 19:29:45 | 显示全部楼层
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()
list2 = []
while len(list1) > 0:
    num = list1.count(list1[0])
    list2.append([list1[0],num])
    temp = list1[0]
    while temp in list1:
        list1.remove(temp)
print(list2)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-11 21:34:37 | 显示全部楼层
本帖最后由 mewk 于 2018-1-11 21:38 编辑
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()       #顺序排列
list1.append('end')      #列表尾部插入标记,因为总是出现超出表格范围问题,所以用了这个笨办法
list3 = []      #产生新列表
i = 1      #因为不能list1[-1],所以从1开始
while i <= len(list1)-1:    #循环列表长度的次数
    l = 1       #重置统计次数,因为自己不和自己比较,开始比较就2次了,所以从1开始
    list2 = []    #生成和清空列表,用于记录元素和次数
    while list1[i] == list1[i-1]:   #比较相邻的元素,直到相邻的不相同
        i += 1   #比较后面两个
        l += 1   #统计相同的出现的次数
    list2.append(list1[i-1]) #当不相同时,前面相同的最后一个元素加入表格的[0]位置
    list2.append(l)#统计的次数加入表格的[1]位置
    list3.append(list2)#将上面的表格,加入最终表格作为一个元素
    i += 1 #向前移动一位,使相邻的继续相等,进入下一次循环
print(list3) #输出最终表格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-11 21:53:22 | 显示全部楼层
本帖最后由 8306最硬 于 2018-1-11 21:59 编辑
list2 = [[i,list1.count(i)] for i in set(list1)]
[[0, 7], [1, 6], [2, 4], [3, 5], [4, 8], [5, 2], [6, 4], [7, 3], [8, 2], [9, 4], [10, 5], [11, 3], [12, 11], [13, 4], [14, 3], [15, 3], [16, 8], [17, 3], [18, 11], [19, 3], [20, 1]]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-12 10:46:13 | 显示全部楼层
初学python,不知道用set,用了很笨的方法,大神们不要笑~
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]
list2=[]
n=0
for i in list1:
        for j in list1:
                if j == i:
                        n+=1
        list2.append([i, n])
        n=0
list3=[]
for i in list2:
        if i not in list3:
                list3.append(i)
sorted(list3)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-12 11:05:52 | 显示全部楼层
def genrate_2Dlist(_1Dlist):
        _1Dlist_max = max(_1Dlist)
        _2Dlist =[[0 for i in range(0,2)] for j in range(0,_1Dlist_max+1)]
        for x in _1Dlist:
                _2Dlist[x][0] = x
                _2Dlist[x][1] += 1
        return _2Dlist
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]
out = genrate_2Dlist(list1)
print(out)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-16 15:41:09 | 显示全部楼层
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]
list2 = set(list1)
list3 = []
list4 = []

for i in list2:
    list3.append(i)
    list4.append(list1.count(i))
   
C = map(list,list(zip(list3,list4)))
print(list(C))
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-16 16:11:22 | 显示全部楼层
list2 = list(set(list1))
list3=[]
for i in list2:
    num = list1.count(i)
    list3.append([i,num])
print(list3)

写完发现有人和我写的一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 10:11:27 | 显示全部楼层
print([[i, list1.count(i)] for i in set(list1)])
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-24 23:12:13 | 显示全部楼层
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]
a=max(list1)
c=[]
d=[]
for b in range(a+1):
    c.append(list1.count(b))
    d.append(b)
print(list(zip(d,c)))
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-30 23:19:58 | 显示全部楼层
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]

list2 = list(set(list1))
newlist = []

for i in list2:
    newlist.append([i,list1.count(i)])

print(newlist)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-3 22:26:20 | 显示全部楼层
# coding=utf-8

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)
list2 = []
print(set1)
for i in set1:
        flag = 0
        tempList = []
        for j in list1:
                if i == j:
                        flag += 1
        tempList.append(i)
        tempList.append(flag)
        list2.append(tempList)
print(list2)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-20 20:44:08 | 显示全部楼层
def fun133(list1):
    return [[each,list1.count(each)] for each in set(list1)]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-1 12:30:48 | 显示全部楼层
def func(l):return [i,l.count(i)]for i in set(l)]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-1 14:15:16 | 显示全部楼层
from collections import Counter
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]
print(sorted([[i,j] for i,j in dict(Counter(list1)).items()]))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-9 13:39:27 | 显示全部楼层
def fun133(nums):   
    f = Counter(nums)
    ans = []
    for val in sorted(f):
        ans.append([val, f[val]])
    return ans

nums = [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]            
print(fun133(nums))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 03:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表