python array[dict] Reduce a certain key according to a certain key
data = [ {"Quantumtunneling_193d9d14381f11efbbeb000d3a096647": "Quantumtunneling_193d9d14381f11efbbeb000d3a096647.docx"}, {"Quantumtunneling_193d9d14381f11efbbeb000d3a096647": "Quantumtunneling_193d9d14381f11efbbeb000d3a096647.docx"}, {"Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647": "Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647.docx"}, {"Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647": "Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647.docx"} ]
I now have an array key that needs to be deduplicated according to the key. At the beginning, I used list(set()) to deduplicate it, but found that set cannot be passed into dict.
This way, you can use a key to reload it.
# Raw datadata = [ {"Quantumtunneling_193d9d14381f11efbbeb000d3a096647": "Quantumtunneling_193d9d14381f11efbbeb000d3a096647.docx"}, {"Quantumtunneling_193d9d14381f11efbbeb000d3a096647": "Quantumtunneling_193d9d14381f11efbbeb000d3a096647.docx"}, {"Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647": "Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647.docx"}, {"Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647": "Asymptoticfreedom_193cbc00381f11efbbeb000d3a096647.docx"} ] # Convert dictionary to tuple for deduplicationunique_tuples = list(set(tuple(())[0] for d in data)) # Convert tuples back to dictionaryunique_list = [{k: v} for k, v in unique_tuples] # Print the resultsimport json print((unique_list, indent=2, ensure_ascii=False))
This is the end of this article about python array[dict]. This is the end of this article about deduplication according to a certain key. For more related python array[dict] content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!