V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
LeIYc
V2EX  ›  Python

列表操作的遇到问题。

  •  
  •   LeIYc · 2018-04-12 14:23:21 +08:00 · 2253 次点击
    这是一个创建于 2199 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想写一个函数,给函数传入一个列表。进行一些判断后产生一个新列表。。。。。。。。。。。。。。。。。。。。。 def get_file(file_index1): file_index2 = [] for f2 in file_index1: f_kind = f2.split("-")[0] f_num = f2.split("-")[2] f_type = f2.split("-")[3].split(".")[0]
    f_all = f_kind+'-'+f_type if f_all == 'receiver-in': F2 = 'sender'+'-8100-'+f_num+'-out.wav' print(F2) return file_index2

    但是最后结果获取不到新列表。

    14 条回复    2018-04-14 06:20:42 +08:00
    LeIYc
        1
    LeIYc  
    OP
       2018-04-12 14:23:50 +08:00
    def get_file(file_index1):
    file_index2 = []
    for f2 in file_index1:
    f_kind = f2.split("-")[0]
    #print(f_kind)
    f_num = f2.split("-")[2]
    #print(f_num)
    f_type = f2.split("-")[3].split(".")[0]
    #print(f_type)

    f_all = f_kind+'-'+f_type
    #print(f_all)
    # file_index2.append((f_all,f_num))
    if f_all is 'receiver-in':
    F2 = 'sender'+'-8100-'+f_num+'-out.wav'
    #print(F2)
    file_index2.append(F2)
    return file_index2
    sikariba
        2
    sikariba  
       2018-04-12 14:31:17 +08:00
    v 站不能贴 Markdown 吗。。。
    prolic
        3
    prolic  
       2018-04-12 14:33:13 +08:00 via Android
    字符串比较别用 is
    rocksolid
        4
    rocksolid  
       2018-04-12 14:35:02 +08:00
    获取不到新列表,那你获取的是什么?
    LeIYc
        5
    LeIYc  
    OP
       2018-04-12 14:53:04 +08:00
    获取的是空的。不过现在莫名其妙的获取成功了。真是醉了。。。。。。
    判断的时候用多个 if 好呢。还是 if。。。elif。。。。else 这种的好啊。
    fromxt
        6
    fromxt  
       2018-04-12 14:56:34 +08:00
    is 比较引用是否相同

    == 比较内容是否相同

    if f_all is 'receiver-in': --> if f_all == 'receiver-in':就可以了
    LeIYc
        7
    LeIYc  
    OP
       2018-04-12 15:11:43 +08:00
    @fromxt 谢谢你的帮助。现在又遇到新问题了,怎么让列表自由组合 生成新的列表
    L1=[receiver,sender]
    L2 = [-in,-out]
    生成:
    L3 = ['receiver-in','receiver-out','sender-in','sender-out']
    虽然可以直接写出来。但是想学下怎么自动生成
    ballshapesdsd
        8
    ballshapesdsd  
       2018-04-12 15:17:46 +08:00   ❤️ 1
    @LeIYc #7
    L3=list(map(lambda x:''.join(x),itertools.product(L1,L2)))
    fromxt
        9
    fromxt  
       2018-04-12 15:44:43 +08:00
    @LeIYc 不客气,我也是菜鸟,用列表推导就可以解决
    L1=[‘ receiver ’,‘ sender ’]
    L2 = [‘-in ’,‘-out ’]
    L3 =list(x+y for x in L1 for y in L2))
    另外,其实《 Beginning Python 》这本书没事多翻翻就能找到答案了
    LeIYc
        10
    LeIYc  
    OP
       2018-04-12 15:52:38 +08:00
    L3 = [(l1+l2)for l1 in L1 for l2 in L2 ]
    这样也可以
    LeIYc
        11
    LeIYc  
    OP
       2018-04-12 15:57:57 +08:00
    有一个列表 里面大概 20 个元素。每个元素中有一段数字类似这样 receiver-8100-20180316102434-out.wav
    相同数字的文件有四个['receiver-in', 'receiver-out', 'sender-in', 'sender-out']
    目前只取出了 3 个,还差一个 。最后那个应该怎么取?
    vipppppp
        12
    vipppppp  
       2018-04-12 16:08:37 +08:00
    看了几遍,都不知道你的“怎么取”是什么意思。。。
    LeIYc
        13
    LeIYc  
    OP
       2018-04-13 14:31:57 +08:00
    @fromxt 再请教个问题:
    有一个列表,列表元素大概是这样的
    ['sender-8100-20180316101841-in.wav',
    'sender-8100-20180316101841-out.wav',
    'receiver-8100-20180316101841-in.wav',
    'receiver-8100-20180316101841-out.wav']
    元素都是成组出现的,一个组数字有四个想同的文件。
    怎么才能根据数字将元素重新组合生成新的列表。每四个组成一个新的列表
    有没有什么关键字的方法可以实现?
    fromxt
        14
    fromxt  
       2018-04-14 06:20:42 +08:00
    @LeIYc 这个用 re 匹配一下数字,再用 set 去重就可以了
    eg:
    import re
    data0 = ['sender-8100-20180316101841-in.wav',
    'sender-8100-20180316101841-out.wav',
    'receiver-8100-20180316101841-in.wav',
    'receiver-8100-20180316101841-out.wav']

    data1 = []
    for item in data:
    data1.append(re.sub(r'[^0-9\-]', '', item))

    list(set(data1)

    再用一个列表推导式处理
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3066 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:02 · PVG 19:02 · LAX 04:02 · JFK 07:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.