如何创建只有一个元素的 Python 集? [英] How do I create a Python set with only one element?

查看:40
本文介绍了如何创建只有一个元素的 Python 集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个字符串,并且想要创建一个初始包含该字符串的集合,是否有比以下更 Pythonic 的方法?

If I have a string, and want to create a set that initially contains only that string, is there a more Pythonic approach than the following?

mySet = set()
mySet.add(myString)

下面给出了 myString 中的一组字母:

The following gives me a set of the letters in myString:

mySet = set(myString)

推荐答案

在 2.7 和 3.x 中,您可以使用:

In 2.7 as well as 3.x, you can use:

mySet = {'abc'}

这篇关于如何创建只有一个元素的 Python 集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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