Turn tuples into a pair string

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 one line that looks like this:

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

How can I do this conveniently in Python?

+4
source share
6 answers
', '.join('%s(%.02f)' % (x, y) for x, y in tuplelist)
+11
source

I do not see any difficulties:

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)
+4
source
", ".join(["{0} ({1})".format(x[0], x[1]) for x in a])

a - .

. for x in a , x , , :

a = [1, 2, 3]
b = [x for x in a]
print(b)

. . [0] [1], . format :

a = Thimble
b = 1.234
print("{0} ({1})".format(a, b)

Thimble (1.234), {0} {1} get , , :

a = [   ('StemCells', 16.530000000000001),
  ('Bcells', 13.59),
  ('Monocytes', 11.58),
  ('abTcells', 10.050000000000001) ]
b = ["{0} ({1})".format(x[0], x[1]) for x in a]
print(b)

:

['StemCells (16.53)', 'Bcells (13.59)', 'Monocytes (11.58)', 'abTcells (10.05)']

, , join, :

", ".join(b)

, , , ',' :

StemCells (16.53), Bcells (13.59), Monocytes (11.58), abTcells (10.05)

, Python 3.3, , Python 2.7 , , .

+2
>>> l = [   ('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)]


>>> ['%s(%.2f)' % (f, d) for f,d in l]
['StemCells(16.53)', 'Bcells(13.59)', 'Monocytes(11.58)', 'abTcells(10.05)',
 'Macrophages(9.69)', 'gdTCells(9.49)', 'StromalCells(9.36)',
 'DendriticCells(9.20)', 'NKCells(7.81)', 'Neutrophils(2.71)']

>>> ', '.join(['%s(%.2f)' % (f, d) for f,d in l])
'StemCells(16.53), Bcells(13.59), Monocytes(11.58), abTcells(10.05), Macrophages(9.69), gdTCells(9.49), StromalCells(9.36), DendriticCells(9.20), NKCells(7.81), Neutrophils(2.71)'
+2

python.

>>> s = [('StemCells', 16.530000000000001), ('Bcells', 13.59), ...]
>>> ", ".join(["{name}({num})".format(name=name, num=num) for name, num in s])
'StemCells(16.53), Bcells(13.59), Monocytes(11.58), abTcells(10.05), Macrophages(9.69), gdTCells(9.49), StromalCells(9.36), DendriticCells(9.2), NKCells(7.81), Neutrophils(2.71)'
+2
def make_str(lst):
    result = ""
    for i in lst:
        result += " {0}({1:.2f}), ".format(i[0], i[1])
    return result[:-2] if len(result) > 1 else result

a = [('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 make_str(a)

:

 StemCells(16.53),  Bcells(13.59),  Monocytes(11.58),  abTcells(10.05),  Macrophages(9.69),  gdTCells(9.49),  StromalCells(9.36),  DendriticCells(9.20),  NKCells(7.81),  Neutrophils(2.71)
+1

Source: https://habr.com/ru/post/1531335/


All Articles