我使用的是Python 2.7,并有一个列表列表:
my_list_0 = [
['X00001', 1123477, 1.25, 'Yes', 'No', 11.53, 3.23, 0.25, 3.0025],
['X00002', 1125251, 0.22, 'Yes', 'No', 22.83, 3.6, 0.5, 0.0012],
['X00003', 1125433, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 12.0014536],
['X00003', 1125435, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 12.0014536],
['X00003', 1125436, 966.33, 'Yes', 'No', 1.8, 3.8, 0.36, 30.12121],
['X00004', 1125437, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 12.0014536],
['X00007', 1125438, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 12.0014536],
['X00001', 1125439, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 12.0014536],
['X00004', 1231254, 1.3, 'Yes', 'No', 21.83, 3.6, 0.5, 1005.000336669],
['X00008', 2520250, 54.02, 'Yes', 'No', 1.83, 3.8, 0.01, 82.0000001],
['X00005', 3202020, 966.33, 'Yes', 'No', 1.8, 3.8, 0.36, 0.000001200012],
['X00006', 3215204, 1.36, 'Yes', 'No', 1.86, 3.65, 0.15, 0.1234565],
['X00007', 4561230, 6.33, 'Yes', 'No', 1.83, 66.22, 0.1, 1.1111111],
['X00009', 5721365, 1.25, 'No', 'No', 11.53, 3.23, 0.25, 11.1110001],
['X00010', 8520525, 6.33, 'Yes', 'No', 1.83, 66.22, 0.1, 20.000156],
['X00110', 8520725, 6.33, 'Yes', 'No', 1.83, 66.22, 0.1, 20.000156],
['X00010', 8520825, 6.33, 'Yes', 'No', 1.83, 66.22, 0.1, 20.000156],
['X00012', 8520123, 966.33, 'Yes', 'No', 1.8, 3.8, 0.36, 30.12121],
['X00011', 9536122, 54.01, 'Yes', 'No', 1.83, 3.8, 0.01, 15.2312456],
['X00015', 1212021, 0.333, 'No', 'No', 1.83, 3.8, 0.01, 0],
['X00015', 1222221, 0.333, 'No', 'No', 1.83, 3.8, 0.01, 0],
['X00013', 9654123, 4.1, 'No', 'No', 1.83, 3.8, 0.01, 90.01],
['X00014', 2021230, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 0.00001],
['X00017', 3322123, 1.23, 'Yes', 'Yes', 1.88, 1.25, 1.12, 0.0002],
['X00016', 9531450, 23.15, 'Yes', 'Yes', 1.83, 3.25, 0.12, 30.123],
['X00020', 3625252, 1.23, 'Yes', 'No', 1.86, 3.65, 0.15, 0.02123589],
['X00009', 5721777, 1.25, 'No', 'No', 11.53, 3.23, 0.25, 11.1110001],
['X00018', 1205203, 1.87, 'Yes', 'No', 1.88, 1.25, 1.12, 89.21233456987],
['X00019', 1124521, 0.25, 'No', 'No', 2.1, 0.6, 0.03, 12.564897],
['X00021', 8952631, 0.25, 'No', 'No', 2.4, 0.6, 0.03, 23.20033],
['X00022', 1123458, 0.33, 'Yes', 'Yes', 10.38, 41.11, 0.5, 0.2314582],
['X00023', 3236254, 3, 'No', 'Yes', 10.38, 41.11, 0.5, 78.024568],
['X00002', 1205323, 1.87, 'Yes', 'No', 1.88, 1.25, 1.12, 32.021348246],
['X00024', 1305324, 1.87, 'Yes', 'No', 1.88, 1.25, 1.12, 32.021348246],
['X00025', 1245325, 1.87, 'Yes', 'No', 1.88, 1.25, 1.12, 32.021348246],
['X00015', 1212444, 0.333, 'No', 'No', 1.83, 3.8, 0.01, 0],
['X00015', 1244421, 0.333, 'No', 'No', 1.83, 3.8, 0.01, 0],
['X00005', 3332020, 966.33, 'Yes', 'No', 1.8, 3.8, 0.36, 0.000001200012],
['X00006', 3555204, 1.36, 'Yes', 'No', 1.86, 3.65, 0.15, 0.1234565],
['X00007', 9871230, 6.33, 'Yes', 'No', 1.83, 66.22, 0.1, 1.1111111]
]
我根据索引2到8的值进行分组:
from itertools import groupby
from operator import itemgetter
my_list_0_grouped_by = [
list(z) for _, z in groupby(sorted(my_list_0), itemgetter(2,3,4,5,6,7,8))
]
现在我想根据群组重新标记列表,这意味着' X00000'标签应替换为新标签,以便每组列表使用相同的标签。
新标签创建为新列表(但这并不重要)
NEW_LABELS = ['A' + str(i) for i in range(len(my_list_0_grouped_by))]
现在我想迭代my_list_0_grouped_by并替换旧的' X00000'标签上有NEW_LABELS的新标签,所以我这样做:
for i in my_list_0_grouped_by:
for j in i:
for k in NEW_LABELS:
j[0] = k
显然这不起作用,因为我只用NEW_LABELS中的最后一个替换所有标签。
答案 0 :(得分:1)
只需使用zip
进行组和按照标签的成对并行迭代:
for i, k in zip(my_list_0_grouped_by, NEW_LABELS):
for j in i:
j[0] = k
答案 1 :(得分:1)
或者您可以使用枚举来获得正确的标签:
for index, i in enumerate(my_list_0_grouped_by):
for j in i:
j[0] = NEW_LABELS[index]