社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Georgy ThatGuyRussell

Georgy ThatGuyRussell 最近创建的主题
Georgy ThatGuyRussell 最近回复了
7 年前
回复了 Georgy ThatGuyRussell 创建的主题 » 需要有关python 3.7的简单脚本的帮助[duplicate]

正如Martijn Pieters所说,正确且最快的格式是:

if 1 in {x, y, z}:

使用他的建议,现在您将拥有单独的if语句,这样Python将读取每个语句,无论前者是否 True 或 False . 例如:

if 0 in {x, y, z}:
    mylist.append("c")
if 1 in {x, y, z}:
    mylist.append("d")
if 2 in {x, y, z}:
    mylist.append("e")
...

这样可以,但是 如果 你很容易使用字典(看我在那里做了什么),你可以通过制作一个初始字典将数字映射到你想要的字母,然后使用for循环来清理这个问题:

num_to_letters = {0: "c", 1: "d", 2: "e", 3: "f"}
for number in num_to_letters:
    if number in {x, y, z}:
        mylist.append(num_to_letters[number])