Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning tuples into a pairwise string

Tags:

python

I have the following tuples

[   ('StemCells', 16.530000000000001),
    ('Bcells', 13.59),
    ('Monocytes', 11.58),
    ('abTcells', 10.050000000000001),
    ('Macrophages', 9.6899999999999995),
    ('gdTCells', 9.4900000000000002),
    ('StromalCells', 9.3599999999999994),
    ('DendriticCells', 9.1999999999999993),
    ('NKCells', 7.8099999999999996),
    ('Neutrophils', 2.71)]

what I want to create is to create a single string that looks like this:

StemCells(16.53), Bcells(13.59), Monocytes(11.58) .... Neutrophils(2.71)

How can I do that conveniently in Python?

like image 690
neversaint Avatar asked Nov 29 '22 00:11

neversaint


2 Answers

', '.join('%s(%.02f)' % (x, y) for x, y in tuplelist)
like image 67
acushner Avatar answered Dec 10 '22 16:12

acushner


I don't really see any difficulty:

tuples = [   ('StemCells', 16.530000000000001),
    ('Bcells', 13.59),
    ('Monocytes', 11.58),
    ('abTcells', 10.050000000000001),
    ('Macrophages', 9.6899999999999995),
    ('gdTCells', 9.4900000000000002),
    ('StromalCells', 9.3599999999999994),
    ('DendriticCells', 9.1999999999999993),
    ('NKCells', 7.8099999999999996),
    ('Neutrophils', 2.71)]
print ', '. join('%s(%.02f)' % (name, value) for name, value in tuples)
like image 33
njzk2 Avatar answered Dec 10 '22 15:12

njzk2