将成对字符串转换为元组

时间:2014-03-13 01:55:04

标签: python

我有以下字符串:

my_str = "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)"

我想要做的是将它们转换为这个元组

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

如何在Python中方便地完成这项工作

3 个答案:

答案 0 :(得分:2)

my_str = "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)"

import re

stuff = re.findall(r'(\w+)\(([0-9.]+)\)',my_str)

stuff
Out[4]: 
[('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')]

这会让你大部分都在那里,然后它只是一点类型转换

[(s,float(f)) for s,f in stuff]
Out[7]: 
[('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 :(得分:2)

最简单的解决方案,不使用正则表达式:

my_str = "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)"

that_str = map(lambda s: s.rstrip(')').split('(') ,my_str.split(','))
that_str = map(lambda s: (s[0], float(s[1])), that_str)
>>> that_str
[('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)]

你可以在一次通过中使用外部函数而不是lambda来完成这项工作:

def build_tuple(s):
    t = s.rstrip(')').split('(')
    return (t[0], float(t[1]))

that_str = map(build_tuple, my_str.split(','))

答案 2 :(得分:1)

怎么样:

>>> zip(re.findall(r'([a-zA-Z]+)',my_str), map(float, re.findall(r'([0-9.]+)',my_str)))