将成对字符串转换为元组

2024-06-22 10:37:51 发布

您现在位置: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中方便地实现这一点


Tags: 字符串my元组strneutrophilsmonocytesmacrophagesnkcells
3条回答
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)]

怎么样:

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

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

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

相关问题 更多 >