Skip to content

Commit

Permalink
simplify: prefer sum over len + if
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Aug 22, 2024
1 parent 6ca78b3 commit 18cfe85
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/pymatgen/analysis/functional_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_special_carbon(self, elements=None):

neighbor_spec = [str(self.species[n]) for n in neighbors]

ons = len([n for n in neighbor_spec if n in ["O", "N", "S"]])
ons = sum(n in ["O", "N", "S"] for n in neighbor_spec)

if len(neighbors) == 4 and ons >= 2:
specials.add(node)
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/analysis/magnetism/heisenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ def _do_screen(structures, energies):
screened_structures (list): Sorted structures.
screened_energies (list): Sorted energies.
"""
magmoms = [s.site_properties["magmom"] for s in structures]
n_below_1ub = [len([m for m in ms if abs(m) < 1]) for ms in magmoms]
magmoms = [struct.site_properties["magmom"] for struct in structures]
n_below_1ub = [sum(abs(m) < 1 for m in ms) for ms in magmoms]

df = pd.DataFrame(
{
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/core/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ def get_integer_index(
mi += 0 # converts -0 to 0

def n_minus(index):
return len([h for h in index if h < 0])
return sum(h < 0 for h in index)

if n_minus(mi) > n_minus(mi * -1):
mi *= -1
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/core/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ def math_lcm(a: int, b: int) -> int:
transf_hkl = np.array([idx // divisor for idx in transf_hkl])

# Get positive Miller index
if len([i for i in transf_hkl if i < 0]) > 1:
if sum(idx < 0 for idx in transf_hkl) > 1:
transf_hkl *= -1

return tuple(transf_hkl)
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/electronic_structure/cohp.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def has_antibnd_states_below_efermi(
limit (float): Only COHP higher than this value will be considered.
"""
populations = self.cohp
n_energies_below_efermi = len([energy for energy in self.energies if energy <= self.efermi])
n_energies_below_efermi = sum(energy <= self.efermi for energy in self.energies)

if populations is None:
return None
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/entries/mixing_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def process_entries(
processed_entry_list.append(entry)

if verbose:
count_type_1 = len([e for e in processed_entry_list if e.parameters["run_type"] in self.valid_rtypes_1])
count_type_2 = len([e for e in processed_entry_list if e.parameters["run_type"] in self.valid_rtypes_2])
count_type_1 = sum(entry.parameters["run_type"] in self.valid_rtypes_1 for entry in processed_entry_list)
count_type_2 = sum(entry.parameters["run_type"] in self.valid_rtypes_2 for entry in processed_entry_list)
print(
f"\nProcessing complete. Mixed entries contain {count_type_1} {self.run_type_1} and {count_type_2} "
f"{self.run_type_2} entries.\n"
Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/test_adsorption.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_generate_adsorption_structures(self):
assert len(structures) == 4
sites = self.asf_111.find_adsorption_sites()
# Check repeat functionality
assert len([site for site in structures[0] if site.properties["surface_properties"] != "adsorbate"]) == 4 * len(
assert len(site.properties["surface_properties"] != "adsorbate" for site in structures[0]) == 4 * len(
self.asf_111.slab
)
for n, structure in enumerate(structures):
Expand Down

0 comments on commit 18cfe85

Please sign in to comment.