【发布时间】:2019-03-07 03:04:07
【问题描述】:
这里是 Python 初学者。我有一个像这样的元组列表:
[(100, 1), (50, 2), (25, 4), (20, 5), (10, 10)]
我想把它转换成
[0, 2, 4, 5, 1]
必须有一种比不断替换每个字符更快的方法:
strfp1 = re.sub('\(','',str(factor_pairs)); strfp2 = re.sub('\)','',strfp1); strfp3 = re.sub('\[','',strfp2); strfp4 = re.sub(']','',strfp3); strfp5 = re.sub(',','',strfp4); strfp6 = re.sub(' ','',strfp5)
factor_numbers = [int(i) for i in set(strfp6)]
然而,我什至找不到一次替换多个不相邻字符的方法。我错过了什么明显的东西吗?
【问题讨论】:
标签: python regex string list tuples