How To Extract Tuple Data into Single Element Format

前端 未结 6 1961
-上瘾入骨i
-上瘾入骨i 2021-01-20 15:48

I get back good results from the following, but how to I extract that data from the tuple? In other words, how do I clean up the data?

Here is the data from the data

相关标签:
6条回答
  • 2021-01-20 16:17

    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).

    0 讨论(0)
  • 2021-01-20 16:17

    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))

    0 讨论(0)
  • 2021-01-20 16:19
    print '\n'.join(str(x[0]) for x in policy_id)
    
    0 讨论(0)
  • 2021-01-20 16:22
    >>> policy_id = ((2309L,), (118L,), (94L,))
    >>> print("\n".join(str(x[0]) for x in policy_id))
    2309
    118
    94
    
    0 讨论(0)
  • 2021-01-20 16:26
    >>> from itertools import chain
    >>> policy_id = ((2309L,), (118L,), (94L,))
    >>> for i in chain.from_iterable(policy_id):
            print i
    
    
    2309
    118
    94
    
    0 讨论(0)
  • 2021-01-20 16:29
    policy_id = ((2309L,), (118L,), (94L,))
    for i in policy_id:
        print i[0]  
    
    0 讨论(0)
提交回复
热议问题