Python 板


LINE

hello 各位好, 最近在练习实作max,min的时候,遇到一个型态的问题. 如何把[1, 2, 0, 3, 4]<-list 或 "hello" <-string 处理成个别的tuple元素呢?? 查了型态转换,string to tuple or list to tuple.一直都无法达到自己想要的. 各位有甚麽建议嘛?? ===================================修改後================================================== import types def min(*args, **kwargs): key = kwargs.get("key", None) print(key) if len(args) == 1: args = args[0] if type(args) == type('a'): MinNo='z' else: MinNo=999 for ii in args: if type(args) == type('a'): if ord(ii) < ord(MinNo): MinNo = ii else: if key == int: if int(ii) < int(MinNo): MinNo = ii else: if ii < MinNo: MinNo = ii return MinNo def max(*args, **kwargs): key = kwargs.get("key", None) if len(args) == 1: args = args[0] if type(args) == type('a'): MaxNo='a' else: MaxNo=-1 for ii in args: if type(args) == type('a'): if ord(ii) > ord(MaxNo): MaxNo = ii else: if key == int: if int(ii) > int(MaxNo): MaxNo = ii else: if ii > MaxNo: MaxNo = ii return MaxNo if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing #assert max(3, 2) == 3, "Simple case max" #assert min(3, 2) == 2, "Simple case min" #assert max([1, 2, 0, 3, 4]) == 4, "From a list" #assert min("hello") == "e", "From string" #assert max(2.2, 5.6, 5.9, key=int) == 5.6, "Two maximal items" assert min([[1, 2], [3, 4], [9, 0]], key=lambda x: x[1]) == [9, 0], "lambda key" <----这个要怎麽实作阿?? --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.124.107.139
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1501567505.A.5D7.html
1F:→ bibo9901: 判断一下 len(args) 就好了 08/01 14:09
2F:→ angleevil: 但是[1, 2, 0, 3, 4] 或 "hello"要怎麽parser各别元素 08/01 15:02
3F:→ angleevil: list_args = [] 08/01 15:03
4F:→ angleevil: list_args = [list(item) for item in args] 并没有预 08/01 15:03
5F:→ angleevil: 期的功能 08/01 15:04
6F:→ djshen: Unpacking Argument Lists 08/01 15:39
7F:→ bibo9901: 1. parser 是名词, parse 才是动词, 08/01 16:31
8F:→ bibo9901: 2. if len(args) == 1: args = args[0] 底下不用改 08/01 16:32
※ 编辑: angleevil (59.124.107.139), 08/01/2017 17:06:38 ※ 编辑: angleevil (59.124.107.139), 08/01/2017 17:15:52 ※ 编辑: angleevil (59.124.107.139), 08/01/2017 17:44:35 ※ 编辑: angleevil (59.124.107.139), 08/01/2017 17:53:46
9F:→ djshen: max(*[1, 2, 0, 3, 4]), min(*"hello")不就可以了? 08/01 18:31
10F:→ djshen: 抱歉 没看到是要实作 08/01 18:38
11F:→ pmove: tuple([1,2,0,3,4])和tuple("hello") ? 08/01 21:56
12F:推 rexyeah: 原po是想自己实做那个function 08/07 14:21







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP