我有一个变量x(列表),看起来像是
['JACKIE:34', 'MATT:444', 'CEN:12', 'PETE:12', 'RANDY:92', 'MITCH:2', 'JAN:2'],然后我想转换为元组(成对)所以它看起来像< br />
[('JACKIE',34), ('MATT',444), ('CEN',12), ('PETE',12), ('RANDY',92), ('MITCH',2), ('JAN',2)]
答案 0 :(得分:7)
按要求编号的数字
before = ['JACKIE:34', 'MATT:444', 'CEN:12', 'PETE:12', 'RANDY:92', 'MITCH:2', 'JAN:2']
after = [(name, int(value)) for name, value in (x.split(':') for x in before)]
print after
答案 1 :(得分:2)
a = ['JACKIE:34', 'MATT:444', 'CEN:12', 'PETE:12', 'RANDY:92', 'MITCH:2', 'JAN:2']
b = [x.split(':') for x in a]
b = [(x[0], int(x[1])) for x in b]