gpt4 book ai didi

python - 长元组拆包的习惯用法

转载 作者:太空狗 更新时间:2023-10-29 16:53:30 25 4
gpt4 key购买 nike

<分区>

场景:您有一个长元组作为 SQL 查询的结果,并希望将其解压缩为单个值。在符合 PEP8 的同时做到这一点的最佳方法是什么?到目前为止,我有以下三种选择:

  1. 单次赋值,使用反斜杠分割成多行

    person_id, first_name, last_name, email, \
    birth_date, graduation_year, home_street, \
    home_city, home_zip, mail_street, mail_city, \
    mail_zip = row
  2. 单一赋值,将左侧分组在括号和断行中,不带反斜杠

    (person_id, first_name, last_name, email,
    birth_date, graduation_year, home_street,
    home_city, home_zip, mail_street, mail_city,
    mail_zip) = row
  3. 拆分成多个任务,每个任务适合一行

    person_id, first_name, last_name, email = row[0:4]
    birth_date, graduation_year, home_street = row[4:7]
    home_city, home_zip, mail_street, mail_city = row[7:11]
    mail_zip = row[11]

这三个选项中哪个是最好的?有没有更好的?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com