鱼C论坛

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

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

[复制链接]
发表于 2017-11-8 01:29:06 | 显示全部楼层
  1. def number(i):
  2.     num=0
  3.     for j in str(i):
  4.         num+=int(j)
  5.     i=num
  6.     if len(str(i))!=1:
  7.         num=number(i)
  8.         

  9.     return num
  10. list1 = [684,6299800,6041028,2458660284,937,7857867,384147026736,888843160862,270]

  11. for i in list1:
  12.    
  13.     print(number(i))
复制代码


list2不知道怎么牌,只能这样了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-10 10:50:13 | 显示全部楼层
本帖最后由 776667 于 2017-11-10 10:51 编辑
  1. def test(list1,list2):
  2.    
  3.     def digits_sum(num):
  4.         result = eval('+'.join(list(str(num))))
  5.         while result >= 10:
  6.             result = eval('+'.join(list(str(result))))
  7.         return result

  8.     list_sum = [digits_sum(num) for num in list1]
  9.     result_list = []
  10.     for i in list2:
  11.         for j in list1:
  12.             if list_sum[list1.index(j)] == i:
  13.                 result_list.append(j)
  14.     return result_list      

  15. if __name__ == '__main__':
  16.     print(test([684, 6299800, 6041028, 2458660284, 937, 7857867, 384147026736, 888843160862, 270],[1, 6, 7, 4, 9, 3, 2, 5, 8]))
复制代码

评分

参与人数 1鱼币 +3 收起 理由
冬雪雪冬 + 3

查看全部评分

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

使用道具 举报

发表于 2017-11-15 17:16:04 | 显示全部楼层
list1 = [684, 6299800, 6041028, 2458660284, 937, 7857867,
         384147026736, 888843160862, 270]
list2 = [1, 6, 7, 4, 9, 3, 2, 5, 8]
def mySum(x):
    sum = 0
    while x > 0:
        sum += x%10
        print(sum)
        x = int(x/10)   
        print(x)     
    if  sum < 10:
        return int(sum)
    return mySum(sum)

list3 = list(map(mySum,list1))

tar = sorted(list3, key = lambda x: list2.index(x))
print(tar)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-19 16:08:27 | 显示全部楼层
  1. def summ(n):
  2.     while n // 10:
  3.         ls = list(str(n))
  4.         temp = 0
  5.         for each in ls:
  6.             temp += int(each)
  7.         n = temp
  8.     return int(n)

  9. list1 = [684, 6299800, 6041028, 2458660284, 937, 7857867, 384147026736, 888843160862, 270]
  10. result = []
  11. for each in list1:
  12.     result.append(summ(each))
  13. print(result)

  14. list2 = [1, 6, 7, 4, 9, 3, 2, 5, 8]
  15. order= {n:i for i,n in enumerate(list2)}
  16. def x(n):
  17.     return order[n]
  18. result.sort(key = x)
  19. print(result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-30 11:32:55 | 显示全部楼层
本帖最后由 shigure_takimi 于 2017-11-30 11:45 编辑
  1. def getNumber(number):
  2.     while number > 9:
  3.         number = sum([int(i) for i in str(number)])
  4.     return number

  5. def getOrderedList(list1, list2):
  6.     newList1 = [getNumber(i) for i in list1]
  7.     newList2 = list(zip(newList1, list1))
  8.     newList3 = [list(i) for i in newList2]
  9.     numberDict = {list2[i]:i for i in range(len(list2))}
  10.     for i in newList3:
  11.         i[0] = numberDict[i[0]]
  12.     newList3= sorted(newList3)
  13.     resultList = [i[1] for i in newList3]
  14.     return resultList

  15. list1 = [684, 6299800, 6041028, 2458660284, 937, 7857867, 384147026736, 888843160862, 270]
  16. list2 = [1, 6, 7, 4, 9, 3, 2, 5, 8]

  17. print(getOrderedList(list1, list2))

  18.                     
  19. #  result:  [937, 384147026736, 6299800, 270, 684, 2458660284, 6041028, 7857867, 888843160862]
  20. #  方法比较笨,但算是做出来了。
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-30 16:00:50 | 显示全部楼层
def Sum(list1):
    list2=[]
    for i in range(len(list1)):
        count=0
        number=list(str(list1[i]))
        for j in range(len(number)):
            count+=int(number[j])
        list2.append(count)
    return list2

list1 = [684, 6299800, 6041028, 2458660284, 937,
         7857867, 384147026736, 888843160862, 270]

list2=[1,6,7,4,9,3,2,5,8]

list3=Sum(list1)
list4=[]
while True:
    for i in range(len(list3)):
        if list3[i]>10:
            list3=Sum(list3)
        continue
    break

for i in list2:
    for j in list3:
        if i==j:
            list4.append(j)

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

使用道具 举报

发表于 2018-2-4 09:10:54 | 显示全部楼层
本帖最后由 yjsx86 于 2018-2-4 09:13 编辑
  1. list1 = [684, 6299800, 6041028, 2458660284, 937, 7857867, 384147026736, 888843160862, 270]
  2. list2 = [1, 6, 7, 4, 9, 3, 2, 5, 8]
  3. def digits_sum(num): # 求位数和
  4.     temp = sum(map(int,str(num)))
  5.     return temp if temp < 10 else digits_sum(temp)

  6. print(sorted(list1,key=lambda x: list2.index(digits_sum(x)))) # 找到位数和在list2中的索引排序

  7. # result
  8. [937, 384147026736, 6299800, 684, 2458660284, 270, 6041028, 7857867, 888843160862]
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-14 14:50:28 | 显示全部楼层
def switch(n):
    temp = 0
    for each in str(n):
        temp += int(each)
    if temp < 10:
        return temp
    else:
        return switch(temp)
   
   
def cus_sort(list_o):
    for i in range(len(list_o)-1):
        for j in range(len(list_o)-i-1):
            if list2.index(list_o[j]) > list2.index(list_o[j+1]):
                list_o[j], list_o[j+1] = list_o[j+1], list_o[j]
    return list_o


list3 = []
for each in list1:
    list3.append(switch(each))
print(cus_sort(list3))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-1 18:35:31 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-3-22 16:43 编辑

结果:[937, 384147026736, 6299800, 684, 2458660284, 270, 6041028, 7857867, 888843160862]
  1. def _func(num):
  2.     while num>9:
  3.         temp=0
  4.         while num:
  5.             temp+=num%10
  6.             num//=10
  7.         num=temp
  8.     return num

  9. def func(List):
  10.     return sorted(List,key=_func)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-5 08:52:48 | 显示全部楼层
list1 = [684, 6299800, 6041028, 2458660284, 937, 7857867, 384147026736, 888843160862, 270]
list2 = [1, 6, 7, 4, 9, 3, 2, 5, 8]

f = dict()
for num in list1:
  pre = num
  while pre > 9:
    k = 0
    while pre > 0:
      k += pre % 10
      pre //= 10
    pre = k
  if pre not in f:
    f[pre] = 1
  else:
    f[pre] += 1

ans = []   
for num in list2:
  if num in f:
    ans += [num] * f[num]

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 07:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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