Python - 过滤类似大小写的字符串


当需要过滤类似大小写的字符串时,列表解析可以与“isupper”和“islower”方法一起使用。

示例

以下是此示例

my_list = ["Python", "good", "FOr", "few", "CODERS"]

print("The list is :")
print(my_list)

my_result = [sub for sub in my_list if sub.islower() or sub.isupper()]

print("The strings with same case are :")
print(my_result)

输出

The list is :
['Python', 'good', 'FOr', 'few', 'CODERS']
The strings with same case are :
['good', 'few', 'CODERS']

说明

  • 定义了一个列表并显示在控制台上。

  • 列表解析用于遍历列表并检查字符串是大写还是小写。

  • 此结果被分配给一个变量。

  • 作为输出显示在控制台上。

更新时间: 2021-09-16

160 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.