首页 > 编程 > Python > 正文

【Python】内建的isinstance函数可以判断一个变量是不是字符串

2019-11-08 00:49:59
字体:
来源:转载
供稿:网友

来自于廖雪峰的Python3教程课后练习 如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:

>>> L = ['Hello', 'World', 18, 'Apple', None]>>> [s.lower() for s in L]Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <listcomp>AttributeError: 'int' object has no attribute 'lower'

使用内建的isinstance函数可以判断一个变量是不是字符串:

>>> x = 'abc'>>> y = 123>>> isinstance(x, str)True>>> isinstance(y, str)False

请修改列表生成式,通过添加if语句保证列表生成式能正确地执行:

# -*- coding: utf-8 -*-L1 = ['Hello', 'World', 18, 'Apple', None]L2 = [x.lower() for x in L1 if isinstance(x, str)]# 期待输出: ['hello', 'world', 'apple']PRint(L2)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表