python字典列表查找特定键的重复值,并为它们重新生成DIC列表

jobtbby3  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(227)

我有一个驱动器列表( drive = [] )而电台在里面听写( stations ={} ). ['type'],['date and time'],['temperature'],['latitude'],['longitude']['value'] 是字典里的一些键。
尝试从运算符获取itemgetter,但我的所有数据都是字符串,它无法检查日期、时间、纬度和经度。

[{"type": "1840", "value": "Low", "date and time": " 30/06/21 10:11:50", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "-0.00", "acceleration y": "0.00", "acceleration z": "-0.01", "latitude": "38.0173", "longitude": "23.793925"},
{"type": "1840", "value": "Low", "date and time": " 30/06/21 10:11:50", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "0.00", "acceleration z": "0.00", "latitude": "38.026402222222224", "longitude": "23.793925"},
{"type": "1840", "value": "Low", "date and time": " 30/06/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "-0.00", "acceleration y": "0.01", "acceleration z": "-0.00", "latitude": "38.03550444444444", "longitude": "23.793925"},
{"type": "1840", "value": "Low", "date and time": " 01/07/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "-0.00", "acceleration z": "0.00", "latitude": "38.04460666666667", "longitude": "23.793925"},
{"type": "1840", "value": "Low", "date and time": " 30/06/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "-0.00", "acceleration y": "-0.00", "acceleration z": "-0.00", "latitude": "38.05370888888889", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "-0.00", "acceleration y": "0.01", "acceleration z": "-0.00", "latitude": "38.03550444444444", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 01/07/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "-0.00", "acceleration z": "0.00", "latitude": "38.04460666666667", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:51", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "-0.00", "acceleration y": "-0.00", "acceleration z": "-0.00", "latitude": "38.05370888888889", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:52", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "0.01", "acceleration z": "0.01", "latitude": "38.117424444444445", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:52", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "0.01", "acceleration z": "0.01", "latitude": "38.071913333333335", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:52", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "-0.01", "acceleration z": "0.00", "latitude": "37.98999333333333", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:52", "temperature": "18", "battery": "3.57", "speed": "0", "acceleration x": "0.00", "acceleration y": "-0.01", "acceleration z": "0.00", "latitude": "38.0173", "longitude": "23.793925"},
{"type": "940", "value": "Low", "date and time": " 30/06/21 10:11:57", "temperature": "18", "battery": "3.57", "speed": "1", "acceleration x": "0.00", "acceleration y": "0.00", "acceleration z": "-0.00", "latitude": "38.0171875", "longitude": "23.793525"}]

最后,我希望有一个新的字典列表,每个列表都有所有的键,字典的值,但是“日期和时间”,“纬度”,“经度”在这个列表中是相同的。

7hiiyaii

7hiiyaii1#

最有效的方法是使用pandas,因为您拥有的是一个结构化的数据框架(表格数据)。转换完成后,您所看到的是一个很小的问题,即逐列查找dataframe中的重复行。


# Use your list here.

data = [{},{},{},{}]

# Note that the sample you provided is missing commas after each dictionary.

# convert to Pandas dataframe

df = pd.DataFrame(data)

# display as a dataframe

print(df.head())

# find duplicates in specific columns

df = df[df.duplicated(subset=['date and time','latitude', 'longitude'], keep=False)]

# display as a dataframe

print(df.head())

# convert back to list of dicts

x = df.T.to_dict().values()

# display the result

print(x)

使用pandas,您还可以避免不必要的循环,如果您的数据是海量的,这些循环将花费很长时间。

相关问题