关于tuple([1])的疑问? - 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
brikhoff

关于tuple([1])的疑问?

  •  
  •   brikhoff Oct 8, 2013 4130 views
    This topic created in 4609 days ago, the information mentioned may be changed or developed.
    tuple([1]) = (1,)
    这个深入python里面也解释过:为创建单元素元组,需要在值之后加上一个逗号。没有逗号,Python 会假定这只是一对额外的圆括号,虽然没有害处,但并不创建元组。
    其实我想问的是,如果在进行SQL IN 查询的时候,我最开始出来的时候会str(tuple([1, 2, 5, 9]))来生成'(1, 2 , 5, 9)'的字符串来连接查询,但是这样在1 == len(pid_list)的时候就会出现上面的问题,最后str([1, 2, 5, 9]).replace('[', '(').replace(']', ')')这样来实现。
    虽然说有时候写代码,只要能实现这个功能就可以了,但是我想想也知道这个方法很笨,我想问问有没有其他更好的方法。写python不多,感觉肯定有我不知道的方法。
    6 replies    1970-01-1 08:00:00 +08:00
    ericls
        1
    ericls  
       Oct 8, 2013 via Android
    1 == len(pid_list)
    是什么问题?
    vmebeh
        2
    vmebeh  
       Oct 8, 2013
    最好不要直接拼接,文档有例子

    http://docs.python.org/2/library/sqlite3.html#cursor-objects
    farseerfc
        3
    farseerfc  
       Oct 8, 2013
    理解清楚你的。你是,你有一list是pid_list,然後想最快得到它的'(1,2,3,4)'的形式所以str(tuple(pid_list)),且不想要len(pid_list)==1最後的那逗是?
    你的方法不笨。Python面做事情一般只有一做法,越清晰可越好,你的方法就很可。
    如果是我的,: '(%s)'%str(pid_list)[1:-1]
    est
        4
    est  
       Oct 9, 2013
    ','.join()
    likexian
        5
    likexian  
       Oct 9, 2013
    有join不用,用什么str,str是这样用的吗
    brikhoff
        6
    brikhoff  
    OP
       Oct 9, 2013
    @likexian join + map?
    About     Help     Advertise     Blog     API     FAQ     Solana     3081 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 116ms UTC 06:48 PVG 14:48 LAX 23:48 JFK 02:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86