Blender 2.8: How to select multiple object with the same diffuse color in principled shader?
import bpy

for obj in bpy.data.objects.keys():
    bpy.context.view_layer.objects.active = bpy.data.objects[obj]
    activeobj = bpy.context.active_object 
    try:
        if "." in activeobj.active_material.name:            
            mat = bpy.context.object.active_material.name[:-4]
            activeobj.data.materials[0] = bpy.data.materials[mat]
    except:
        pass

for mat in bpy.data.materials.keys():
    if bpy.data.materials[mat].users == 0:
        bpy.data.materials.remove(bpy.data.materials[mat])


Try this out and let me know