Home > Enterprise >  Compare list of lists with a list
Compare list of lists with a list

Time:04-20

Good morning,

I want to compare each list in the list of lists with the list based on the fact that each element is an interval and if that interval from "Listoflists" is included inside the interval from "List" we would get a True statement or else it would be a False one. And if possible, get in output a dataframe with True/False statement.

Here are the generated data :

List = [(4.960796557803424, 7.248503442196575), (5.038014752877868, 7.346085247122132), (5.103186780925054, 7.421613219074945), (5.105574436906216, 7.411525563093783), (5.09793832836004, 7.40086167163996), (5.117123344669599, 7.425176655330402), (5.140331981802099, 7.4544680181979), (5.153676999469211, 7.474623000530788), (5.169652452805959, 7.492747547194041), (5.176548183589987, 7.504451816410014), (5.179694590627988, 7.503005409372013), (5.186390607692675, 7.511009392307325), (5.187339125799644, 7.508960874200357), (5.189423235837134, 7.505376764162866), (5.199445384769695, 7.522654615230306), (5.1955279814463555, 7.512372018553645), (5.197146040939456, 7.511053959060543), (5.197860761030804, 7.516639238969197), (5.210950348383933, 7.529249651616069), (5.209023219591814, 7.5227767804081855), (5.197399815676738, 7.501300184323261), (5.195492268796732, 7.500807731203269), (5.210861199605485, 7.519838800394513), (5.204998445996691, 7.508901554003308), (5.20852003421463, 7.5121799657853705), (5.212252490189632, 7.524247509810367), (5.201234058771638, 7.500165941228362), (5.203652197491083, 7.503647802508917), (5.203165974373181, 7.504134025626819), (5.193959133355697, 7.490940866644302), (5.1988794553742315, 7.50032054462577), (5.197989848964307, 7.496210151035692), (5.1764459042422235, 7.466454095757777), (5.17883661246545, 7.472363387534549), (5.179113854294984, 7.475186145705015), (5.165933121850285, 7.456266878149714), (5.161950017421835, 7.454949982578165), (5.158510435609787, 7.456189564390212), (5.136973595450633, 7.425026404549368), (5.12549341260486, 7.416606587395141), (5.112957061397275, 7.397442938602726), (5.088003293233508, 7.365796706766493), (5.0680012406177966, 7.344398759382205), (5.027766228062783, 7.289433771937217), (4.98758418928086, 7.237615810719141), (4.941884579815628, 7.177315420184371), (4.8843399842923585, 7.101860015707642), (4.809069789199319, 7.0000302108006816), (4.712868404520053, 6.862731595479948), (4.594140983877125, 6.695059016122874)]
Listoflists = [[(5.624485901006036, 7.314114098993965), (5.411991077827618, 7.054208922172383), (5.236414671318958, 6.784685328681043)], [(5.624485901006036, 7.314114098993965), (5.411991077827618, 7.054208922172383), (5.236414671318958, 6.784685328681043)], [(5.624485901006036, 7.314114098993965), (5.411991077827618, 7.054208922172383), (5.236414671318958, 6.784685328681043)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.537573221519728, 7.167826778480273), (5.5812641275580726, 7.228535872441927), (5.538628919468101, 7.1545710805319), (5.452498368454096, 7.082601631545904), (5.614454290015469, 7.294645709984532), (5.6133091333567045, 7.294990866643296), (5.654112609506859, 7.3263873904931405), (5.5870048201935125, 7.264595179806489), (5.630825712428621, 7.322874287571381), (5.633694432612272, 7.320705567387727), (5.666478901946156, 7.370621098053843), (4.988901956502318, 6.487398043497683)], [(5.486756512505686, 7.108943487494315), (5.502462012633316, 7.109137987366683)], [(5.486756512505686, 7.108943487494315), (5.502462012633316, 7.109137987366683)], [(5.6211930530698115, 7.284606946930188), (5.538370684448803, 7.184029315551197), (5.400211474921432, 6.992988525078569)], [(5.6211930530698115, 7.284606946930188), (5.538370684448803, 7.184029315551197), (5.400211474921432, 6.992988525078569)], [(5.6211930530698115, 7.284606946930188), (5.538370684448803, 7.184029315551197), (5.400211474921432, 6.992988525078569)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.625321394155952, 7.2834786058440475), (5.543237464503713, 7.167062535496287), (5.609005028975223, 7.2505949710247775), (5.66266675738864, 7.3353332426113615), (5.593157996296727, 7.243842003703273), (5.6916504629293465, 7.368249537070654), (5.082570543334951, 6.597429456665049)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.53613079846842, 7.159969201531581), (5.6371294512269285, 7.300970548773072), (5.568992317062811, 7.190907682937189), (5.774366988264341, 7.490033011735659), (5.689328310328547, 7.371471689671454), (5.548454703339994, 7.200745296660006), (5.607135343983764, 7.237664656016236)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)], [(5.366898199538092, 6.952401800461908), (5.39976840137702, 6.999131598622981), (5.347062317281391, 6.973037682718609), (5.500327401989365, 7.163472598010635), (5.348496716709206, 6.954703283290795), (5.459572139235642, 7.0954278607643575), (5.270226488204126, 6.846073511795873), (5.2754687250413435, 6.893431274958657), (5.477930742790426, 7.105069257209572), (4.983069161432187, 6.521030838567814), (4.963886091621905, 6.476613908378095), (4.983402688518933, 6.498897311481065), (5.171912572830255, 6.7390874271697445), (5.011399738614732, 6.552000261385268), (5.111506124019241, 6.658993875980759), (5.112209948356012, 6.633190051643988)]]

Example to describe it clearly (it's not a code it's just to have a clear view):

We take the first list of lists from the "Listoflists" : [(5.624485901006036, 7.314114098993965), (5.411991077827618, 7.054208922172383), (5.236414671318958, 6.784685328681043)]
We compare each interval with all the intervals from the "List".

If (5.624485901006036, 7.314114098993965) (which is the first element from "Listoflists") is in the intervals from "List" we will get a True statement, Else we will have a False one.

If you have a starting point or any idea, I would be very grateful.

Thank you very much and have a great day !

CodePudding user response:

For checking if each element in ListofLists is a subset of any tuple in List

y = []
for i in Listoflists:
    for j in i:
        for ii in List:
            # Sort each tuple. Remove this if they are already sorted
            sorta = sorted(j)
            sortb = sorted(ii)
            y.append(sorta[0] >= sortb[0] and sorta[1] <= sortb[1])
>>> y[0:5]
[False, True, True, True, True]

This goes with the rationale that a subset of [0,1] can be [0,1] (the same set). If you want it to not include the same set, remove the = from the inequalities.


And try not to name your variables after classes (List) as it can cause unexpected errors

CodePudding user response:

You need to try and make your problems statement simpler and clearer. Also its kind of taboo to start variable names with a capital letter (like List and ListOfLists). You can better write: lst (becasue list is taken) and list_of_lists.

From what i understand, you are looking for this:

lst  = [(1, 1), (2, 5), (3, 4)]
data = [[(1, 2), (8, 5), (7, 3)], [(1, 1), (2, 5), (3, 4)]]

# check if lst is present in data
if lst in data:
    # do something if the lst was found in data (ListOfLists)
    print("yay")
else:
    # do something if the lst was not found in data
    print("nay")

If you are trying to find if one of these: (1, 5) tuples in your List is present in one if the lists in your ListOfLists. You can do the following:

lst  = [(1, 1), (2, 5), (3, 4)]
data = [[(1, 2), (8, 5), (7, 3)], [(1, 1), (2, 5), (3, 4)]]

for el in data:
    for tpl in lst:
        if tpl in el:
            # you found that (2, 5) is in data[1]
            print("yay")

EDIT: for interval checking

lst  = [(1, 1), (2, 5), (3, 4)]
data = [[(1, 2), (8, 5), (7, 3)], [(1, 1), (2, 5), (3, 4)]]

# for intervals in lst
for tpl in lst:

    # go over list of intervals
    for i, interval_list in enumerate(data):
        for interval in interval_list:
            # check if between interval (inclusive)
            if tpl[0] >= interval[0] and tpl[1] <= interval[1]:
                print(tpl, 'fits in interval located in list', i)
                print('the interval:', interval)
  • Related