2015-09-15 36 views
0

我有多个数字的list在它:如何在Python中将数字分成多个不相等的部分?

num_list = ['A',3452,3487,6534,4521] 

list的具有第一元件作为string充当标识符。

现在我想要做的就是采取在list每个number将它分为四个部分 - 18%,22%,24%和36%,然后形成具有第1个要素作为一个新的列表先前列表中的字符串后面跟着前面列表中每个数字的4个分开的部分。所以输出应该是这样的:

new_list = ['A',part1 of num1,part2 of num1,part3 of num1,part4 of num1,part1 of num2,part2 of num2,part3 of num2,part4 of num2......] 

我怎么能在python中做到这一点?

+1

'new_list = [num_list [0]]。 new_list.append(num_list [1] * 0.18); new_list.append(num_list [1] * 0.22); ...' – Kevin

+0

到目前为止你做了什么?你能提供一个代码吗? – Cyrbil

+1

非常不赞同“太宽泛”的结束投票。这是一个非常具体的问题,并且有明确的说明。 –

回答

4

可以用列表理解解决它:

>>> num_list = ['A', 3452, 3487, 6534, 4521] 
>>> percents = [0.18, 0.22, 0.24, 0.36] 
>>> [num_list[0]] + [item * percent for item in num_list[1:] for percent in percents] 
['A', 621.36, 759.44, 828.48, 1242.72, 627.66, 767.14, 836.88, 1255.32, 1176.12, 1437.48, 1568.1599999999999, 2352.24, 813.78, 994.62, 1085.04, 1627.56] 
     # --------- num1 ------------ # # --------- num2 ------------ # # ------------------ num3 ----------------- # # --------- num4 ------------- # 
相关问题