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

使用 Join 为啥会莫名奇妙的输出空格?

  •  
  •   ladypxy · 2018-11-05 20:28:04 +08:00 · 2362 次点击
    这是一个创建于 2240 天前的主题,其中的信息可能已经有所发展或是发生改变。

    正在做 pythonchallenge,Q2,我自己用的是字典的方法

    words ="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. "
    
    secret = {"a":"c","b":"d","c":"e","d":"f","e":"g","f":"h ","g":"i","h ":"j","i":"k","j":"l","k":"m","l":"n","m":"o","n":"p","o":"q","p":"r","q":"s","r":"t","s":"u","t":"v","u":"w","v":"x","w":"y","x":"z","y":"a","z":"b"}
    
    answer=[]
    i=0
    while i < len(words):
        j = words[i]
        if j in secret.keys():
            answer.append(secret[j])
        else:
            answer.append(j)
        i = i +1
    print(answer)
    print("".join(answer))
    

    测试发现 最后的 answer 这个 list 是没有问题的, 内容是

    ['i', ' ', 'h ', 'o', 'p', 'e', ' ', 'y', 'o', 'u', ' ', 'd', 'i', 'd', 'n', 't', ' ', 't', 'r', 'a', 'n', 's', 'l', 'a', 't', 'e', ' ', 'i', 't', ' ', 'b', 'y', ' ', 'h ', 'a', 'n', 'd', '.', ' ', 't', 'h ', 'a', 't', 's', ' ', 'w', 'h ', 'a', 't', ' ', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', ' ', 'a', 'r', 'e', ' ', 'f', 'o', 'r', '.', ' ', 'd', 'o', 'i', 'n', 'g', ' ', 'i', 't', ' ', 'i', 'n', ' ', 'b', 'y', ' ', 'h ', 'a', 'n', 'd', ' ', 'i', 's', ' ', 'i', 'n', 'e', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't', ' ', 'a', 'n', 'd', ' ', 't', 'h ', 'a', 't', "'", 's', ' ', 'w', 'h ', 'y', ' ', 't', 'h ', 'i', 's', ' ', 't', 'e', 'x', 't', ' ', 'i', 's', ' ', 's', 'o', ' ', 'l', 'o', 'n', 'g', '.', ' ', 'u', 's', 'i', 'n', 'g', ' ', 's', 't', 'r', 'i', 'n', 'g', '.', 'm', 'a', 'k', 'e', 't', 'r', 'a', 'n', 's', '(', ')', ' ', 'i', 's', ' ', 'r', 'e', 'c', 'o', 'm', 'm', 'e', 'n', 'd', 'e', 'd', '.', ' ', 'n', 'o', 'w', ' ', 'a', 'p', 'p', 'l', 'y', ' ', 'o', 'n', ' ', 't', 'h ', 'e', ' ', 'u', 'r', 'l', '.', ' ']
    

    但是最后使用 join 将 list 转变成 str 时,结果就变成了

    i h ope you didnt translate it by h and. th ats wh at computers are for. doing it in by h and is inefficient and th at's wh y th is text is so long. using string.maketrans() is recommended. now apply on th e url. 
    

    注意其中莫名奇妙的多了几个空格,正常应该是

    i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url. 
    

    请教下大家这莫名奇妙的空格是怎么被输出的?

    9 条回复    2018-11-07 10:33:52 +08:00
    jasonyang9
        1
    jasonyang9  
       2018-11-05 20:33:55 +08:00
    亲,你的

    ```
    secret = {"a":"c","b":"d","c":"e","d":"f","e":"g","f":"h ","g":"i","h ":"j","i":"k","j":"l","k":"m","l":"n","m":"o","n":"p","o":"q","p":"r","q":"s","r":"t","s":"u","t":"v","u":"w","v":"x","w":"y","x":"z","y":"a","z":"b"}
    ```

    中,"f":"h "后面就是有空格的啊
    sunhk25
        2
    sunhk25  
       2018-11-05 20:35:27 +08:00 via Android
    answer 的 list 里面不也是有空格吗
    watzds
        3
    watzds  
       2018-11-05 20:36:22 +08:00 via Android
    这也太明显了,左右还有两个引号
    ladypxy
        4
    ladypxy  
    OP
       2018-11-05 20:44:03 +08:00
    @jasonyang9 ...果然是低级错误。用 emeditor 批量替换成字典的时候多打了空格。。
    PythonAnswer
        5
    PythonAnswer  
       2018-11-06 00:16:59 +08:00 via iPhone
    Strip 一下就好
    trait
        6
    trait  
       2018-11-06 02:11:26 +08:00 via iPhone
    遇到问题还是再多想一下吧,这么硬敲的时候不想偷下懒么?这密码对是有规律的啊
    MrGba2z
        7
    MrGba2z  
       2018-11-06 04:57:34 +08:00
    扔个 one-liner(逃):

    print(*[secret.get(s, s) for s in words], sep='')
    opengps
        8
    opengps  
       2018-11-06 07:45:26 +08:00 via Android
    习惯性所有输入处理都带上 trim 就看不到这问题了
    princelai
        9
    princelai  
       2018-11-07 10:33:52 +08:00
    如果遇到 words.upper()这样的字符你的程序就失效了啊,字典再扩大一倍?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3888 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:58 · PVG 08:58 · LAX 16:58 · JFK 19:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.