在 Python 中返回没有特定元素的列表的快速方法 [英] A quick way to return list without a specific element in Python

查看:46
本文介绍了在 Python 中返回没有特定元素的列表的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个像这样任意顺序的卡片套装列表:

suits = ["h", "c", "d", "s"]

我想返回一个没有 'c'

的列表

noclubs = ["h", "d", "s"]

有没有简单的方法可以做到这一点?

解决方案

>>>西装 = ["h","c","d","s"]>>>noclubs = 列表(套装)>>>noclubs.remove("c")>>>无俱乐部['h', 'd', 's']

如果您不需要单独的noclubs

<预><代码>>>>西装 = ["h","c","d","s"]>>>西装.remove("c")

If I have a list of card suits in arbitrary order like so:

suits = ["h", "c", "d", "s"]

and I want to return a list without the 'c'

noclubs = ["h", "d", "s"]

is there a simple way to do this?

解决方案

>>> suits = ["h","c", "d", "s"]
>>> noclubs = list(suits)
>>> noclubs.remove("c")
>>> noclubs
['h', 'd', 's']

If you don't need a seperate noclubs

>>> suits = ["h","c", "d", "s"]
>>> suits.remove("c")

这篇关于在 Python 中返回没有特定元素的列表的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆