标签:bin span 字符串操作 包括 spl empty 例子 name middle
>>> data = [ ‘ACME‘, 50, 91.1, (2012, 12, 21) ] >>> name, shares, price, date = data >>> name ‘ACME‘ >>> date (2012, 12, 21) >>> name, shares, price, (year, mon, day) = data >>> name ‘ACME‘ >>> year 2012 >>> mon 12 >>> day 21
>>> s = ‘Hello‘ >>> a, b, c, d, e = s >>> a ‘H‘ >>> b ‘e‘ >>> e ‘o‘
其实这种方法可以用在任何可迭代对象上面,包括迭代器和生成器
>>> data = [ ‘ACME‘, 50, 91.1, (2012, 12, 21) ] >>> _, shares, price, _ = data >>> shares 50 >>> price 91.1
# *在中间和最后时效果一样 >>> first, *middle, last = [1,2,3,4,5] >>> first 1 >>> middle [2, 3, 4] >>> last 5 # *在前面 *trailing, current = (10, 8, 7, 1, 9, 5, 10, 3) >>> trailing [10, 8, 7, 1, 9, 5, 10] >>> current 3
值得注意的是,*号变量的返回值永远是列表类型
>>> line = ‘nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false‘ >>> uname, *fields, homedir, sh = line.split(‘:‘) >>> uname ‘nobody‘ >>> homedir ‘/var/empty‘ >>> sh ‘/usr/bin/false‘
>>> record = (‘ACME‘, 50, 123.45, (12, 18, 2012)) >>> name, *_, (*_, year) = record >>> name ‘ACME‘ >>> year 2012
标签:bin span 字符串操作 包括 spl empty 例子 name middle
原文地址:https://www.cnblogs.com/regit/p/9363283.html