第1章 1.5 操作字符串

简介一下"if ... else ... for" 的用法:

>>> word = 'AFTER'

>>> print('x' if w.isdigit() else w for w in word)

<generator object <genexpr> at 0x105439750>

打印的结果是一个生成器对象,没看到具体内容,原因是"if ... else ... for"生成了一个生成器( python generator object)对象,类似C++中的指针和iterator对象.

 

>>> print(''.join('x' if w.isdigit() else w for w in word))

AFTER

通过join可以将生成器连成字符串

 

>>> word = 'AFTER7'

>>> print(''.join('x' if w.isdigit() else w for w in word))

AFTERx

>>> print(['x' if w.isdigit() else w for w in word])

['A', 'F', 'T', 'E', 'R', 'x']

也可以将成生器转换成一个列表

 

>>> word = 'AFTER7.2'

>>> print(['x' if w.isdigit() else w for w in word])

['A', 'F', 'T', 'E', 'R', 'x', '.', 'x']

 

>>> INPUT_TEXT = '''AFTER THE CLOSE OF THE SECOND QUARTER, OUR COMPANY, THE REVENUE OF 7.47%. THIS IS '''

>>> 

>>> print([('X' if w.isdigit() else w for w in word) for word in words])

[<generator object <listcomp>.<genexpr> at 0x10546e050>, <generator object <listcomp>.<genexpr> at 0x10546e0d0>, <generator object <listcomp>.<genexpr> at 0x10546e150>, <generator object <listcomp>.<genexpr> at 0x10546e1d0>, <generator object <listcomp>.<genexpr> at 0x10546e250>, <generator object <listcomp>.<genexpr> at 0x10546e2d0>, <generator object <listcomp>.<genexpr> at 0x10546e350>, <generator object <listcomp>.<genexpr> at 0x10546e3d0>, <generator object <listcomp>.<genexpr> at 0x10546e450>, <generator object <listcomp>.<genexpr> at 0x10546e550>, <generator object <listcomp>.<genexpr> at 0x10546e5d0>, <generator object <listcomp>.<genexpr> at 0x10546e650>, <generator object <listcomp>.<genexpr> at 0x10546e6d0>, <generator object <listcomp>.<genexpr> at 0x10546e750>, <generator object <listcomp>.<genexpr> at 0x10546e7d0>]

嵌套的生成器直接转列表看到的还是生成器对象

 

>>> print([''.join('X' if w.isdigit() else w for w in word) for word in words])

['AFTER', 'THE', 'CLOSE', 'OF', 'THE', 'SECOND', 'QUARTER,', 'OUR', 'COMPANY,', 'THE', 'REVENUE', 'OF', 'X.XX%.', 'THIS', 'IS']

通过join可以将嵌套的生成器转成字符

>>> 

>>> print([[('X' if w.isdigit() else w for w in word)] for word in words])

[[<generator object <listcomp>.<genexpr> at 0x105476050>], [<generator object <listcomp>.<genexpr> at 0x1054760d0>], [<generator object <listcomp>.<genexpr> at 0x105476150>], [<generator object <listcomp>.<genexpr> at 0x1054761d0>], [<generator object <listcomp>.<genexpr> at 0x105476250>], [<generator object <listcomp>.<genexpr> at 0x1054762d0>], [<generator object <listcomp>.<genexpr> at 0x105476350>], [<generator object <listcomp>.<genexpr> at 0x1054763d0>], [<generator object <listcomp>.<genexpr> at 0x105476450>], [<generator object <listcomp>.<genexpr> at 0x105476550>], [<generator object <listcomp>.<genexpr> at 0x1054765d0>], [<generator object <listcomp>.<genexpr> at 0x105476650>], [<generator object <listcomp>.<genexpr> at 0x1054766d0>], [<generator object <listcomp>.<genexpr> at 0x105476750>], [<generator object <listcomp>.<genexpr> at 0x1054767d0>]]

嵌套的生成器通过两层列表转变,看到的还是生成器对象

>>> print(''.join('X' if w.isdigit() else w for w in word) for word in words)

<generator object <genexpr> at 0x105475050>

结果为生成器对象

>>> word = 'AFTER'

>>> genA = 'x' if w.isdigit() else w for w in word

  File "<stdin>", line 1

    genA = 'x' if w.isdigit() else w for w in word

                                       ^

SyntaxError: invalid syntax

不能将"if ... else .. for ..."的结果赋给一个变量

>>> print('x' if w.isdigit() else w for w in word)

<generator object <genexpr> at 0x105439750>

>>> print(['x' if w.isdigit() else w for w in word])

['A', 'F', 'T', 'E', 'R']

posted @ 2022-04-08 17:50  轻舞飞洋  阅读(28)  评论(0编辑  收藏  举报