美文网首页
Python中分解变量

Python中分解变量

作者: yxuang | 来源:发表于2017-10-24 18:03 被阅读0次

    1.分解为单独变量
    data = ['yxuang', 50, 95.5, (2011, 12, 21)]
    name, age, score, date = data
    print(name, age, score, date)

    yxuang 50 95.5 (2011, 12, 21)

    2.丢弃某些值
    data = ['yxuang', 50, 95.5, (2011, 12, 21)]
    _, age, score, _ = data
    print(age, score)

    50 95.5

    3.分解N个元素
    data = ['yxuang', 50, 95.5, (2011, 12, 21)]
    first, *middle, last = data
    print(first, middle, last)

    yxuang [50, 95.5] (2011, 12, 21)

    4.丢弃多个值
    data = ['yxuang', 50, 95.5, (2011, 12, 21)]
    name, _, (_, day) = data
    print(name, day)

    yxuang 21

    相关文章

      网友评论

          本文标题:Python中分解变量

          本文链接:https://www.haomeiwen.com/subject/vrjxpxtx.html