How to you convert list of tuples to string
Larry Hudson
orgnut at yahoo.com
Wed Nov 23 03:42:47 EST 2016
On 11/22/2016 08:51 AM, Michiel Overtoom wrote:
> Hi Ganesh,
>
>> Any better suggestion to improve this piece of code and make it look more pythonic?
>
>
> import random
>
> # A list of tuples. Note that the L behind a number means that the number is a 'long'.
>
> data = [(1, 1, 373891072L, 8192), (1, 3, 390348800L, 8192), (1, 4, 372719616L,
> 8192), (2, 3, 382140416L, 8192), (2, 5, 398721024L, 8192), (3, 1,
> 374030336L, 8192), (3, 3, 374079488L, 8192), (3, 5, 340058112L, 8192)]
>
> item = random.choice(data) # Select a random item from the 'data' list.
>
> msg = "%d,%d,%d:%d" % item # Format it in the way you like.
>
> print msg
>
>
> Greetings,
>
Or using the new string formatting syntax:
msg = '{},{},{}:{}'.format(*item)
The *item in the format() unpacks the tuple.
--
-=- Larry -=-
More information about the Python-list
mailing list