开始选择IDS我有姓名和年龄的字典:具有相同字母的Python字典
classmates = {'Anne':15, 'Laura':17, 'Michel':16, 'Lee':15, 'Mick':17, 'Liz':16}
我想选择所有名称以字母“L”。我能做到这一点是这样的:
for name, age in classmates.items():
if "L" in name:
print(name)
或
Lnames = [name for name in classmates.items() if "L" in name]
有没有当我有上百万个条目的,我需要重复操作上百万次做的更有效的方法?
看看['str.startswith '](https://docs.python.org/3/library/stdtypes.html#str.startswith) – PRMoureu
不要在名称中使用''L',它会搜索整个名称为''L''。相反,使用'name.startswith(“L”)'这将只是看名字的开始。 –
true,我可以使用'name.startswith(“L”)',但这种方法快多少? – aLbAc