将元组转换成成对的字符串

时间:2014-03-12 13:24:28

标签: python

我有以下元组

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

我想要创建的是创建一个如下所示的字符串:

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

我怎样才能在Python中方便地做到这一点?

6 个答案:

答案 0 :(得分:11)

', '.join('%s(%.02f)' % (x, y) for x, y in tuplelist)

答案 1 :(得分:4)

我真的没有看到任何困难:

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)

答案 2 :(得分:2)

", ".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}被格式方法的参数替换,将这两部分放在一起就可以得到:

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中使用,也可能更早。

答案 3 :(得分: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)'

答案 4 :(得分:2)

你可以使用list comprehension和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)'

答案 5 :(得分:1)

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)