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

Python 列出两个列表中一个不含另一个的值

  •  
  •   Bill1 · 2017-01-21 00:58:54 +08:00 · 3382 次点击
    这是一个创建于 2650 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Google foo.bar游戏,本地测试正常,在游戏上提交了几次都测试失败了,代码如下,请大家帮我看看。

    def answer(x, y):
    	# your code here
    	a=0
    	while True:
    		if a==len(x):
    			a=0
    			while True:
    				if y[a] in x:
    					a=a+1
    				else:
    					print(y[a])
    					return
    		elif x[a] in y:
    			a=a+1
    		else:
    			print(x[a])
    			return
    

    #只有一个两个列表中一个不含另一个的值,输出的是一个值

    ryd994
        1
    ryd994  
       2017-01-21 06:05:38 +08:00 via Android
    这种东西在线问被查到会很惨的
    ryd994
        2
    ryd994  
       2017-01-21 07:05:54 +08:00 via Android
    @livid 隐藏本帖比较好
    一方面这种彩蛋招聘还要作弊就没意思了
    其实被 Google 索引到的话对楼主不利
    Victor215
        3
    Victor215  
       2017-01-21 10:10:10 +08:00
    可以用 set
    AlisaDestiny
        4
    AlisaDestiny  
       2017-01-21 13:57:56 +08:00
    ```python
    def answer(x,y):
    a = [e for e in x if e not in y]
    b = [e for e in y if e not in x]
    print(a+b)

    ```
    Bill1
        5
    Bill1  
    OP
       2017-01-21 16:06:23 +08:00
    @ryd994 我没打算用这种方式进 Google ,我只是纳闷本地测试没问题而提交就不行了

    @AlisaDestiny 仍然本地测试没问题而提交就不行了

    纳闷。。。。
    jmc891205
        6
    jmc891205  
       2017-01-21 16:30:46 +08:00
    一般就是一些边界条件没搞对 有些 testcase 过不了 你自己再查查吧
    herozhang
        7
    herozhang  
       2017-01-21 17:24:28 +08:00
    可能的点:
    1. 缺少对输入值的检查,例如传入的参数是乱七八糟的其他东西
    2. 性能(速度、内存等)不达标
    cyrbuzz
        8
    cyrbuzz  
       2017-01-22 12:56:55 +08:00
    或许是 return 问题,这样写 return 是 None 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1054 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:30 · PVG 06:30 · LAX 15:30 · JFK 18:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.