2014-03-12 36 views
4

我有以下的元組談到元組成成對串

[ ('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中?

回答

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

由於'STR。join'不得不將迭代器變成一個列表(因爲它將兩次傳遞給它),但它實際上稍微有點高性能,它將它傳遞給列表理解。這是我使用list comp而不是生成器表達式的狹義情況。 –

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
", ".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) 

將打印,因爲字符串中的{0}{1}Thimble (1.234)獲得由參數格式的方法所取代,把這兩部分結合在一起時得到這樣的:

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工作,甚至更早。

+0

你可以/應該使用生成器表達式傳遞來加入(即去掉括號) – acushner

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)' 
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)