2024-06-16 11:43:04 发布
网友
假设下面的列表中只有字符串。你知道吗
appliances = ['blender', 'microwave', 'oven', 'toaster']
如何生成一个新的列表,该列表由list appliance的元素组成,其中只包含包含字母v的字符串?运行完后,新列表如下所示:
short = ['oven', 'microwave']
试试这个:
short = [x for x in appliances if 'v' in x]
您需要的工具是列表理解(http://docs.python.org/tutorial/datastructures.html#list-comprehensions)和in运算符(http://docs.python.org/reference/expressions.html#in)。你知道吗
in
试试这个:
您需要的工具是列表理解(http://docs.python.org/tutorial/datastructures.html#list-comprehensions)和
in
运算符(http://docs.python.org/reference/expressions.html#in)。你知道吗相关问题 更多 >
编程相关推荐