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

关于 for 循环 print 出数据问题请教

  •  
  •   krisbai · 2020-06-30 17:25:52 +08:00 · 1778 次点击
    这是一个创建于 1394 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ···python3
    ------------------
    for span in spans:
    cc = span.text.strip('\')
    print(cc,end="/")`
    ------------------
    这个 for 循环是在函数内执行的,请教下如何把 print 的数据作为变量值再利用呢?
    kiracyan
        1
    kiracyan  
       2020-06-30 17:28:24 +08:00   ❤️ 1
    循环外加个变量 然后把你要保存的值赋值进去
    imn1
        2
    imn1  
       2020-06-30 17:34:03 +08:00   ❤️ 1
    ccs=[]
    ...
    ccs.append(cc)
    ...
    return ccs
    或者
    print(...)
    yield cc
    krisbai
        3
    krisbai  
    OP
       2020-06-30 17:38:03 +08:00
    @imn1 这样的话我保存的字符串还能再利用吗?这个方式我开始有试过,但是 print 的是中文字符。。。
    imn1
        4
    imn1  
       2020-06-30 20:01:02 +08:00   ❤️ 1
    @krisbai #3
    你这样问我很难回答,这是基础知识,区别是怎样用,建议看手册
    linvaux
        5
    linvaux  
       2020-06-30 22:54:48 +08:00   ❤️ 1
    放到一个 list 里面
    Cursor1st
        6
    Cursor1st  
       2020-07-01 08:39:57 +08:00   ❤️ 1
    @krisbai
    @imn1 的方法应该就符合你的要求,定义空列表,每循环 append(),最后得到的就是包含所有结果的列表,再按需调用即可。
    krisbai
        7
    krisbai  
    OP
       2020-07-01 09:50:22 +08:00
    @Cursor1st 感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3225 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:05 · PVG 20:05 · LAX 05:05 · JFK 08:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.