推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
daiqiangbudainiu
V2EX  ›  Python

求教怎么能比较优雅的实现这个功能

  •  
  •   daiqiangbudainiu · Apr 29, 2019 · 6878 views
    This topic created in 2745 days ago, the information mentioned may be changed or developed.

    有一段 json,格式大概是这样的

    "data":[
        {
            "file": "https://fvs.io/re.........",
            "label": "480p",
            "type": "mp4"
        },
        {
            "file": "https://fvs.io/redirector.......",
            "label": "720p",
            "type": "mp4"
        }
    ]
    

    我想取出来 label 中清晰度最高的

    37 replies  •  2019-04-30 12:23:17 +08:00
    ErnieSauce
    ErnieSauce
    Apr 29, 2019
    1
    我一般使用 json 转数组,然后提取
    ErnieSauce
    ErnieSauce
    Apr 29, 2019
    2
    @ErnieSauce 刚看到是在 python 区,这个不太清楚,应该也有类似的吧
    hmxxmh
    hmxxmh
    Apr 29, 2019 via Android
    3
    遍历取出字段 label 的值和对应的索引,比较大小留下大的索引,然后通过索引去取字典😂
    entertainyou
    entertainyou
    Apr 29, 2019
    4
    max(list, key=lambda x: x['label']) // 需要根据需求修改 key 函数.
    Leigg
    Leigg
    Apr 29, 2019 via iPhone❤️1
    5
    @hmxxmh 遍历的时候就已经得到结果了,兄弟
    daiqiangbudainiu
    daiqiangbudainiu
    OP
    Apr 29, 2019
    6
    @Leigg 遍历的时候得不到吧,毕竟不知道后边是不是还有更大的值
    zzxCNCZ
    zzxCNCZ
    Apr 29, 2019
    7
    遍历一遍就能去取到最大的啦
    awah
    awah
    Apr 29, 2019
    8
    如果是 python sorted 可以自定义排序吧
    Northxw
    Northxw
    Apr 29, 2019
    9
    首先,这种 Json 格式一般来说是固定且有序的(不排除某些奇葩站点,不过我没遇见过),获取 -1 索引位置的就好;
    然后,如果接口每次返回随机个数的 label,但是固定顺序的,len(data) 后获取 [-1] 个就好;
    最后,如果随机个数且无序,建议用正则;第一次匹配数字,确定最大的,第二次在第一期确定基础之上,非贪婪正则匹配最近的 label。
    zzz686970
    zzz686970
    Apr 29, 2019
    10
    @entertainyou 如果还有 1080p 那岂不是拿不到么?
    linweibin
    linweibin
    Apr 29, 2019
    11
    不知道楼主要什么语言,思路:冒泡排序取
    zqx
    zqx
    Apr 29, 2019 via Android
    12
    js:
    arr.sort((a,b)=>{
    return Number(a.label)-Number(b.label)
    })[0]
    hundan
    hundan
    Apr 29, 2019 via Android
    13
    一边遍历一边按标签重新分组 然后最后操作不同分组
    LancerComet
    LancerComet
    Apr 29, 2019
    14
    [
    {
    "file": "https://fvs.io/re.........",
    "label": "480p",
    "type": "mp4"
    },
    {
    "file": "https://fvs.io/redirector.......",
    "label": "720p",
    "type": "mp4"
    },
    {
    "file": "https://fvs.io/redirector.......",
    "label": "240p",
    "type": "mp4"
    },
    {
    "file": "https://fvs.io/redirector.......",
    "label": "1080p",
    "type": "mp4"
    },
    ].reduce((prev, value) => parseInt(value.label) > parseInt(prev.label) ? value : prev)
    icy37785
    icy37785
    Apr 30, 2019
    15
    遍历一遍呀。
    prolic
    prolic
    Apr 30, 2019
    16
    print(reduce(lambda x,y: x if x['label'] > y['label'] else y,json.loads(str).get('data')))
    懒得格式化了,凑合看吧
    vHypnos
    vHypnos
    Apr 30, 2019
    17
    sort 排序 ( by lable ) 然后返回最后一个 ( or 第一个)
    Trim21
    Trim21
    Apr 30, 2019 via Android
    18
    max(o['data'], key=lambda x:int(x['label'][:-1]))
    v4dc
    v4dc
    Apr 30, 2019 via Android
    19
    遍历啊,为什么要去排序
    hmxxmh
    hmxxmh
    Apr 30, 2019 via Android
    20
    @linweibin 我也觉得有点类似冒泡排序😂
    Foreverdxa
    Foreverdxa
    Apr 30, 2019
    21
    js 里的 reduce 可以实现,前面老哥都是很详细了。。。
    zqx
    zqx
    Apr 30, 2019 via Android
    22
    js:
    Math.max(arr.map((item)=>{return item.label}))
    WuwuGin
    WuwuGin
    Apr 30, 2019
    23
    啥排序啊,就取个最大值而已
    nacosboy
    nacosboy
    Apr 30, 2019
    24
    maxResolution := 0

    for _, d := range data {
    if maxResolution < parseXXX(d.Label) {
    maxResolution = parseXXX(d.Label)
    }
    }

    return maxResolution
    nacosboy
    nacosboy
    Apr 30, 2019
    25
    ```go
    func main() {
    maxResolution := 0

    for _, d := range data {
    if maxResolution < parseXXX(d.Label) {
    maxResolution = parseXXX(d.Label)
    }
    }

    print(maxResolution)
    }

    ```
    klongfei
    klongfei
    Apr 30, 2019 via iPhone
    26
    遍历一边就能拿到手非常简单
    daguaochengtang
    daguaochengtang
    Apr 30, 2019
    27
    @zqx 你这个表达式返回的是 NaN。 第一,Math.max 不能接收数组。第二,label 是字符串,要转成数字
    Math.max(...arr.map(item => parseInt(item.label)))
    Chenamy2017
    Chenamy2017
    Apr 30, 2019
    28
    遍历
    取每个 label,转数字,比较大小;如果大的这个对象就暂存
    一遍下来就能拿到暂存的最大的对象了。
    45HXlKzal6W56zUJ
    45HXlKzal6W56zUJ
    Apr 30, 2019
    29
    你们都忽略了 在比较的时候 1080P < 480P 的问题..
    fzzff
    fzzff
    Apr 30, 2019
    30
    data = [
    {
    "file": "https://fvs.io/re.........",
    "label": "480p",
    "type": "mp4"
    },
    {
    "file": "https://fvs.io/redirector.......",
    "label": "1020p",
    "type": "mp4"
    },
    {
    "file": "https://fvs.io/redirector.......",
    "label": "720p",
    "type": "mp4"
    }
    ]

    data.sort(key=lambda d:int(d['label'][:-1]), reverse=True)
    print(data[0])
    45HXlKzal6W56zUJ
    45HXlKzal6W56zUJ
    Apr 30, 2019
    31
    sorted(a, key=lambda x:int(x['label'][:-1]), reverse=True)

    第一个就是
    inhzus
    inhzus
    Apr 30, 2019
    32
    max(json.dumps(d)['data'], key=lambda x: int(x['label'][:-1]))
    zqx
    zqx
    Apr 30, 2019
    33
    @nikolausliu 手机上打的,疏忽了。parseInt 可以将字符串中数字部分提取出来,而 Number 只能构造数值,最后应该用...展开数组给 max 函数
    Akichu
    Akichu
    Apr 30, 2019
    34
    用 js 的 ramda 库:
    R.last(R.sortBy(R.prop('label'), data))
    ccdrea
    ccdrea
    Apr 30, 2019
    35
    1.
    max(data, key=lambda x: int(x['label'].replace('p','')))

    2.
    data.sort(key=lambda x:int(x['label'].replace('p','')))
    data[-1]
    ccdrea
    ccdrea
    Apr 30, 2019
    36
    补充:
    from collections import OrderedDict

    c = list(map(lambda x:OrderedDict(x),a))[-1]

    # OrderedDict 实现一个 FIFO 得 dict
    TommyLemon
    TommyLemon
    Apr 30, 2019
    37
    能用 SQL 过滤最好,不能用就遍历再对比
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5175 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 93ms · UTC 06:30 · PVG 14:30 · LAX 22:30 · JFK 01:30
    ♥ Do have faith in what you're doing.