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
meteor2013
V2EX  ›  Python

如果有两个格式不一样的 list, 怎么样判断是否含有“相同的单词”呢?

  •  
  •   meteor2013 · 2015-01-15 07:34:16 +08:00 · 2766 次点击
    这是一个创建于 3391 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第一个: [['Vodka'], ['Whisky'], ['Scotch'], ['Brandy']]
    第二个: (['Vodka', 'today', 'nice', 'drink', 'Scotch'], 'Group 2')
    11 条回复    2015-01-16 02:51:17 +08:00
    aheadlead
        1
    aheadlead  
       2015-01-15 07:44:43 +08:00
    列表解析为set...交给set处理吧...
    icedx
        2
    icedx  
       2015-01-15 07:46:52 +08:00 via Android
    for I in l1:
    ~for j in l2:
    Fox4y
        3
    Fox4y  
       2015-01-15 08:29:44 +08:00
    a=[['Vodka'], ['Whisky'], ['Scotch'], ['Brandy']]
    >>> for line in a:
    ... "Vodka" in line
    ztcontrol
        4
    ztcontrol  
       2015-01-15 09:09:36 +08:00
    外行, 写的不对请指正
    l1 = [['Vodka'], ['Whisky'], ['Scotch'], ['Brandy']]
    l2 = (['Vodka', 'today', 'nice', 'drink', 'Scotch'], 'Group 2')

    s1 = set()
    s2 = set()

    def listToSet(setx, listx):
    for listobj in listx:
    if type(listobj) == list:
    listToSet(setx, listobj)
    else:
    setx.add(listobj)

    listToSet(s1, l1)
    listToSet(s2, l2)
    print s1
    print s2
    print '/n'
    print s1 & s2
    thinkmore
        5
    thinkmore  
       2015-01-15 09:39:20 +08:00
    遍历
    scenix
        6
    scenix  
       2015-01-15 12:08:50 +08:00
    递归试试?

    #!/bin/env python
    #encoding=utf-8
    # Author: Aaron Shao - [email protected]
    # Last modified: 2015-01-15 12:07
    # Filename: test.py
    # Description:

    a= [['Vodka'], ['Whisky'], ['Scotch'], ['Brandy']]
    b = (['Vodka', 'today', 'nice', 'drink', 'Scotch'], 'Group 2')

    def parse(x):
    if isinstance(x, (tuple,list)):
    result = set([])
    for i in x:
    pass
    result = result | parse(i)
    return result
    elif isinstance(x, str):
    return set([x])
    else:
    return set([])

    x = parse(a)
    y = parse(b)

    print x & y
    scenix
        7
    scenix  
       2015-01-15 12:09:15 +08:00
    妈蛋 忘了把email删了。。。
    iannil
        8
    iannil  
       2015-01-15 12:14:05 +08:00
    1、看list1到list2的难度有多大。
    2、如果去掉无效元素难度不大,就直接先把list1都处理成list2,或把list1和list2都处理成list3的格式。
    3、如果去掉无效元素难度过大,就反过来处理,把list1和list2中你需要的信息拿出来,而不是去掉没用的信息。相同的单词这种特征简直太好取了。
    hahastudio
        9
    hahastudio  
       2015-01-15 12:54:13 +08:00   ❤️ 1
    这时候就要祭出一个经典的 recipe 啦:
    flatten arbitrarily nested lists
    https://stackoverflow.com/questions/10823877/what-is-the-fastest-way-to-flatten-arbitrarily-nested-lists-in-python

    交集就是
    set(flatten(l1)).intersection(set(flatten(l2)))
    imn1
        10
    imn1  
       2015-01-15 13:07:27 +08:00
    二维表一向都交由 pandas/numpy 处理,相关函数多得是

    虽然处理这个问题用 pandas 有点托大,但估计这种数据的产生和后续处理也有不少地方可以用到 pandas
    meteor2013
        11
    meteor2013  
    OP
       2015-01-16 02:51:17 +08:00
    @hahastudio 谢谢啊,你这个方法超赞!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3015 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:12 · PVG 22:12 · LAX 07:12 · JFK 10:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.