Linux 拨号vps windows公众号手机端

python怎么提取list中特定的元素

lewis 8年前 (2017-04-02) 阅读数 9 #程序编程
文章标签 python

要提取列表中特定的元素,可以使用列表解析或循环遍历的方法。
使用列表解析方法:

my_list=[1,2,3,4,5,6]

specific_elements=[xforxinmy_listifx>3]

print(specific_elements)

输出结果:

[4,5,6]

使用循环遍历的方法:

my_list=[1,2,3,4,5,6]

specific_elements=[]

forxinmy_list:

ifx>3:

specific_elements.append(x)

print(specific_elements)

输出结果:

[4,5,6]

以上两种方法都可以提取列表中大于3的元素。根据具体需求,可以根据不同的判断条件来提取特定的元素。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门