Re: How to you convert list of tuples to string

2016-11-23 Thread Larry Hudson via Python-list

On 11/23/2016 03:09 AM, Ned Batchelder wrote:
[snip...]

Or using the new string formatting syntax:

msg = '{},{},{}:{}'.format(*item)

The *item in the format() unpacks the tuple.



"new" == "Introduced in 2.6, available since 2008" :)

--Ned.



Of course.  I probably should have said "newer" or "other".:-)

--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to you convert list of tuples to string

2016-11-23 Thread Ned Batchelder
On Wednesday, November 23, 2016 at 3:43:05 AM UTC-5, Larry Hudson wrote:
> 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.


"new" == "Introduced in 2.6, available since 2008" :)

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to you convert list of tuples to string

2016-11-23 Thread Larry Hudson via Python-list

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 -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to you convert list of tuples to string

2016-11-22 Thread Michiel Overtoom
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,

-- 
https://mail.python.org/mailman/listinfo/python-list


How to you convert list of tuples to string

2016-11-22 Thread Ganesh Pal
Dear friends ,


I am using fedora 18 and on  Python 2.7 version


I have a list of tuples as shown below

>> list

[(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)]

(a) I need to select any element randomly the list say (x, y, xL,
8192)

  >>> list
 [(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)]


  >>> import random
  >>> i = random.randrange(len(list))
  >>> sel_item = list[i]
  >>> sel_item
  (3, 5, 340058112L, 8192)



(b) Then convert the selected item in the below format i.e
 1,1,373891072:8192 ( strip L and add :)

 >>> sel_item
  (3, 5, 340058112L, 8192)
   >> c1 = ','.join(map(str,sel_item))

# what happened to 'L' it got stripped automatically ? will these be a
problem
   >>> c1
   '3,5,340058112,8192'
#last four are always 8912 and
   >>> c1 = c1[0:-5] + ':8912'
>>> c1
 '3,5,340058112:8912'
   >>>


Any better suggestion to improve this piece of code and make it look more /
pythonic


Regards,
Ganesh Pal
-- 
https://mail.python.org/mailman/listinfo/python-list