How To Extract Tuple Data into Single Element Format

﹥>﹥吖頭↗ 提交于 2019-12-02 01:02:33
policy_id = ((2309L,), (118L,), (94L,))
for i in policy_id:
    print i[0]  
>>> from itertools import chain
>>> policy_id = ((2309L,), (118L,), (94L,))
>>> for i in chain.from_iterable(policy_id):
        print i


2309
118
94
print '\n'.join(str(x[0]) for x in policy_id)

A notion that I like, that might confuse to begin with, is the following:

Python2

policy_id = ((2309L,), (118L,), (94L,))
for i, in policy_id:
    print i

Python3

policy_id = ((2309,), (118,), (94,))
for i, in policy_id:
    print(i)

The , after the i unpacks the element of a single-element tuple (does not work for tuples with more elements than one).

Ashwini Chaudhary
>>> policy_id = ((2309L,), (118L,), (94L,))
>>> print("\n".join(str(x[0]) for x in policy_id))
2309
118
94

Other way using map

map(lambda x: str(x[0]), policy_id)

If you want new lines then

"\n".join(map(lambda x: str(x[0]), policy_id))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!