|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 奋斗中的鱼 于 2023-7-27 21:05 编辑
我想打印一个列表[[1,2,3],4,5,6](如下),可是他一直不行,求大神指教!
注意,列表是[[1,2,3],4,5,6],不是[[1,2,3],[4,5,6]]!!
>>>lis1 = [[1,2,3],i for i in range(4,7)]
SyntaxError: did you forget parentheses around the comprehension target?
>>>lis1 = [[1,2,3],(i for i in range(4,7))]
>>>lis1
[[1, 2, 3], <generator object <genexpr> at 0x000001D272123B90>]
>>>lis1 = [[1,2,3],i for i in range(4,7)]
SyntaxError: did you forget parentheses around the comprehension target?
>>>lis1 = [1,2,3,i for i in range(4,7)]
SyntaxError: did you forget parentheses around the comprehension target?
>>>lis1 = [([1,2,3],i for i in range(4,7))]
SyntaxError: invalid syntax
本帖最后由 smallwh 于 2023-7-28 11:15 编辑
*是starred expression(星号表达式)
函数传参时,在列表、元组、字符串等前加上*,能将它拆分成单个元素。 >>> print(list(zip(*[[1,2,3],[4,5,6],[7,8,9]])))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> print(list(zip([1,2,3],[4,5,6],[7,8,9])))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
用[]生成列表和函数类似
|
|