Python | The total value after the list-dict ratio of continuous keys

Record the first algorithm that was a little difficult for me when I first started working last year.

The requirements are as follows. By merging the existing refueling machine equipment table and the planned increase refueling machine equipment table from the gas station statistics, the future total machine maintenance quantity of each gas station is obtained. The data of the two tables has been combined through the previous script operation. After reading, sort according to the value of city, and then read the data to obtain the demo data shown below.

[{'addr': '天津市', 'city': '南开区', 'name': '天津市南开区天拖加油站', 'jiusan': 2, 'jiuba': 2, 'chaiyou': 1,'shijianduan': '00:00~24:00', 'fuwuduan': '00:00~24:00',
'zhifu': '加油宝APP', 'detail_addr': '天津市市辖区南开区天拖101号天拖加油站'},{'addr': '天津市', 'city': '南开区', 'name': '天津市南开区天拖加油站', 'jiusan': 2, 'jiuba': 4, 'chaiyou': 1,'shijianduan': '00:00~24:00', 'fuwuduan': '00:00~24:00','zhifu': '加油宝APP', 'detail_addr': '天津市市辖区南开区天拖101号天拖加油站'},{'addr': '天津市', 'city': '河西区', 'name': '天津市河西区梅江会展加油站', 'jiusan': 5, 'jiuba': 5, 'chaiyou': 2,'shijianduan': '00:00~24:00', 'fuwuduan': '00:00~24:00','zhifu': '加油宝APP', 'detail_addr': '天津市河西区梅江1034号梅江会展加油站'},

。。。。。。后续几百条数据,数据已从业务数据脱敏,展示数据全部为虚假数据}]

The elements have been sorted, and the same elements will be next to each other. It is necessary to compare the city in each element of the list-dict data, add the number of tankers in the elements with the same city value, and finally keep only one total Record, save as a list-list structure for subsequent operations, as shown below

[['天津市', '南开区', '天津市南开区天拖加油站', 4, 6, 2, '00:00~24:00','00:00~24:00','加油宝APP', '天津市市辖区南开区天拖101号天拖加油站'],.....]

In order to meet this requirement, first take out the key_list and value_list of each element

k1 = []
v1 = []

for x in range(len(dic1)):
    k1.append(list(dic1[x].keys()))
    v1.append(list(dic1[x].values()))

Then use operations that add value to the key

q1 = []
r = 0
for i in range(len(v1)):
    if i == 0:
        q1.append(v1[i])
        continue
    if v1[i][1] == q1[r][1]:
        q1[r][3] += v1[i][3]
        q1[r][4] += v1[i][4]
        q1[r][5] += v1[i][5]
        continue
    else:
        q1.append(v1[i])
        r += 1

print(q1)
The image is called the taxi algorithm, the essence is actually the relative value of the key and value, to judge whether v1[n][1] is equal, when the condition is met, add the value of v1[n][3-5] 
First prepare the first "car", then let the first data on the "car", then specify an "address", and then come the second data, if the "destination" of the second data is the same as the previous address If the "destination" is the same, "get on the bus" and add the corresponding values. 
If the second data has a different "destination" from the previous address "destination", then the "car" will drive away, that is, save in q1

But this only applies to list-dicts that have already been sorted. For those that are not sorted, they need to be sorted first or use descending double traversal to achieve the effect.

Guess you like

Origin blog.csdn.net/weixin_45325204/article/details/127876739