For example, the first line above contains 3:19 and 3:6. How to write code so that the final file only takes the first one encountered? In this example, select 3:19
Because I don’t know whether your string is a string or something, so I will implement it in the form of a string first
l = '0:13 1:9 2:14 3:19 4:12 3:19'
d = {}
result = []
for _ in l.split():
key = _.split(':')[0]
if key not in d:
d[key] = _
result.append(d[key])
print(result)
print(result)
# 输出
['0:13', '1:9', '2:14', '3:19', '4:12']
Because I don’t know whether your string is a string or something, so I will implement it in the form of a string first