鱼C论坛

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

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

[复制链接]
发表于 2018-8-22 21:02:32 | 显示全部楼层
本帖最后由 archlzy 于 2018-8-22 21:53 编辑
  1. def newzip(*arg):
  2.     max_len = max([len(i) for i in arg])
  3.     result = []
  4.     for n in range(max_len):
  5.         temp = []
  6.         for m in arg:
  7.             try:
  8.                 temp.append(m[n])
  9.             except:
  10.                 temp.append(None)
  11.         temp = tuple(temp)
  12.         result.append(temp)
  13.     return result


  14. def newzip_1(*arg):
  15.     arg = [list(i) for i in arg]
  16.     max_len = max([len(i) for i in arg])
  17.     for n in range(max_len):
  18.         for m in arg:
  19.             try:
  20.                 m[n] = m[n]
  21.             except:
  22.                 m.append(None)
  23.         yield tuple([m[n] for m in arg])  # 请教版主大腿这个位置 为什么不可以 写成
  24.                                                       # yield (m[n] for m in arg)
复制代码

点评

括号的不是元组生成式,而是一个生成器  发表于 2018-8-24 20:44

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-23 02:47:32 | 显示全部楼层
  1. str1 = 'ABC'
  2. list1 = [1, 2, 3,[4, 5]]
  3. tuple1 = (1.1, 2.2)

  4. def fun201(*argv)->'sequence or iterables':
  5.     max_length = 0
  6.     for arg in argv:                # elements in # of args
  7.         if len(arg) > max_length:
  8.             max_length = len(arg)   # find max # of elements
  9.   
  10.     zips = []
  11.     for i in range(max_length):     # initialization
  12.         zips.append([])             # extends # of max elements
  13.         
  14.     for arg in range(max_length):    # main col
  15.         for i in range(len(argv)):      # number of args
  16.             try:
  17.                 zips[arg].append(argv[i][arg])
  18.             except:
  19.                 zips[arg].append(None)
  20.         zips[arg]=tuple(zips[arg])
  21.     return zips
  22.             
  23. z = fun201(str1,list1,tuple1)
  24. print(z)
复制代码
  1. [('A', 1, 1.1), ('B', 2, 2.2), ('C', 3, None), (None, [4, 5], None)]
复制代码

生成器还不熟

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-23 08:53:40 | 显示全部楼层
vip  通道 怎么找不到了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-23 10:55:53 | 显示全部楼层
  1. def fun201(*x):
  2.     length = max([len(i) for i in x])
  3.     result = []
  4.     for i in range(length):
  5.         list_a = []
  6.         for j in x:
  7.             try:
  8.                 list_a.append(j[i])
  9.             except IndexError:
  10.                 list_a.append(None)
  11.         result.append(tuple(list_a))
  12.     return result
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2018-8-23 11:46:59 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-23 19:36:59 | 显示全部楼层
  1. def f(*args):
  2.     list0, Max = [x for x in args], max([len(x) for x in args])
  3.     list1,res = [], []
  4.     for each in list0:
  5.         if len(each) < Max:
  6.             add = Max - len(each)
  7.             each = list(each)
  8.             while add:
  9.                 each.append('None')
  10.                 add -= 1
  11.             list1.append(each)
  12.         else:
  13.             list1.append(each)
  14.     for i in range(Max):
  15.         list2 = []
  16.         for j in list1:
  17.             list2.append(j[i])
  18.         res.append(tuple(list2))
  19.     return res
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-24 10:15:10 | 显示全部楼层
  1. def newzip(a,b,c):
  2.         x = list(a)
  3.         y = list(b)
  4.         z = list(c)
  5.         m = max([len(x),len(y),len(z)])
  6.         for i in [x,y,z]:
  7.                 if len(i) < m:
  8.                         ro = m - len(i)
  9.                 for j in range(0,ro):
  10.                         i.append(None)
  11.         print(list(zip(x,y,z)))


  12. str1 = 'ABC'
  13. list1 = [1, 2, 3,[4, 5]]
  14. tuple1 = (1.1, 2.2)
  15. newzip(str1,list1,tuple1)
复制代码

点评

题目没有限定只有3个参数  发表于 2018-8-24 20:47

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-24 14:40:27 | 显示全部楼层
def newzip(*arg):
    leng=0
   
    for i in arg:
        i=len(i)
        if i>leng:
            leng=i
    result=[]
   
    for i in range(leng):
        result2=[]
        for x in arg:
            try:
                result2.append(x[i])
            except IndexError:
                result2.append(None)
        result.append(tuple(result2))
    return result

str1 = 'ABC'
list1 = [1, 2, 3,[4, 5]]
tuple1 = (1.1, 2.2)
print(newzip(str1, list1, tuple1))

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-24 18:34:42 | 显示全部楼层
def newzip(str1, list1, tuple1):
    max1 = max(len(str1), len(list1), len(tuple1))
    s_str1 = len(str1)
    s_list1 = len(list1)
    s_tuple1 = len(tuple1)

    list_s = list(str1)
    for str1_i in range(max1 - s_str1):
        list_s.append(None)

    for list1_i in range(max1 - s_tuple1):
        list1.append(None)

    list_t = list(tuple1)
    for tuple1_i in range(max1 - s_tuple1):
        list_t.append(None)  #添加两次



# 补齐四个之后
    list_r = []
    for i in range(max1):
        list_k = []
        list_k.append(list_s[i])
        list_k.append(list1[i])
        list_k.append(list_t[i])

        list_r.append(tuple(list_k))
    print(list_r)

str1 = 'ABC'
list1 = [1, 2, 3,[4, 5]]
tuple1 = (1.1, 2.2)
newzip(str1,list1,tuple1)

没读懂题。。。
感觉弄复杂了。。。
不知道所给的类型是不是一定的。。。。还是任意。。。

点评

题目没有限定3个参数  发表于 2018-8-24 20:49

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-28 14:28:17 | 显示全部楼层
def newzip(*args):       
        m=max(len(x) for x in args)
        for i in range(m):
                tmp=[]
                for x in args:                       
                        tmp.append(x[i] if i<len(x) else None)                                       
                yield tuple(tmp)

str1 = 'ABC'
list1 = [1, 2, 3,[4, 5]]
tuple1 = (1.1, 2.2)
print(list(newzip(str1, list1, tuple1)))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-23 18:55:57 | 显示全部楼层
  1. def newzip(*itera):
  2.     list1=[*itera]
  3.     for i in range(len(list1)):
  4.         if not isinstance(list1[i],list):
  5.             list1[i]=list(list1[i])
  6.     list2=list(map(lambda x:len(x),list1))
  7.     for each in range(len(list1)):
  8.         list1[each]+=[None]*(max(list2)-list2[each])
  9.     return(list(zip(*list1)))

  10. str1 = 'ABC'
  11. list1 = [1, 2, 3,[4, 5]]
  12. tuple1 = (1.1, 2.2)
  13. print(newzip(str1, list1, tuple1))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-6 16:18:28 | 显示全部楼层
  1. def test201(*args):
  2.     return [tuple([j[i] if i < len(j) else None for j in args]) for i in range(max([len(i) for i in args]))]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 22:03:00 | 显示全部楼层
  1. def zipped(*iters):
  2.         for i in range(max((len(i)for i in iters))):
  3.                 l=[]
  4.                 for j in iters:
  5.                         try:l.append(j[i])
  6.                         except:l.append(None)
  7.                 yield tuple(l)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 16:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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