python - Efficiently count list entries that are smaller than a given value -
i have 2 lists, 1 contains employee salaries , 1 contains queries. each query want print how many employee salaries less that.
below can find code use, exceeds time limit large inputs. therefore, i'm looking more efficient solutions.
sal = [int(raw_input()) x in range(num_employees)] q = [int(raw_input()) x in range(num_queries)] in q: count = 0 ep in sal: if ep < i: count +=1 print count
i don't know whether faster (or slower) solution. can use timeit
module determine large inputs.
sal = [int(raw_input()) x in range(num_employees)] q = [int(raw_input()) x in range(num_queries)] in q: print sum(1 s in sal if s < i)
depending on data sizes, sort salaries , queries beforehand , need make 1 pass on lists:
idx = 0 sal = sorted(sal) count = 0 q in sorted(queries): while sal[idx] < q: count += 1 idx += 1 print count
this second method faster long inputs, perhaps slower shorter inputs - you'd have profile representative data. if data sorted method fast.
Comments
Post a Comment