快速返回没有Python中特定元素的列表的方法 - python

如果我有任意顺序的卡片套装列表,如下所示:

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

我想返回一个没有'c'的列表

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

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

参考方案

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

如果您不需要单独的noclubs

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

Python:检查是否存在维基百科文章 - python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

Python-如何检查Redis服务器是否可用 - python

我正在开发用于访问Redis Server的Python服务(类)。我想知道如何检查Redis Server是否正在运行。而且如果某种原因我无法连接到它。这是我的代码的一部分import redis rs = redis.Redis("localhost") print rs 它打印以下内容<redis.client.Redis o…

如何将Python字节字符串表示形式转换为字节? - python

我在文本文件中存储了许多Python字节对象,这些Python打印的内容类似于"b'\x80\x03}q\x00.'"如何将每个对象转换回字节对象?换句话说,我正在尝试找到一个执行convert("b'\x80\x03}q\x00.'") == b'\x80\x03}q…