马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hveagle 于 2022-7-5 14:02 编辑
5. 鱼C-T恤总共提供了有 2 种颜色("BLACK", "WHITE")和 10 个码数("WS", "WM", "WL", "S", "M", "L", "XL", "2XL", "3XL", "4XL")的选择,请使用列表推导式统计所有颜色和尺码的组合(要求将每个组合使用一个内嵌的列表包含起来,如下所示)。
>>> colors = ["BLACK", "WHITE"]
>>> sizes = ["WS", "WM", "WL", "S", "M", "L", "XL", "2XL", "3XL", "4XL"]
>>>
>>> # 请使用列表推导式,统计得出下面的结果
>>>
>>> result
[['BLACK', 'WS'], ['BLACK', 'WM'], ['BLACK', 'WL'], ['BLACK', 'S'], ['BLACK', 'M'], ['BLACK', 'L'], ['BLACK', 'XL'], ['BLACK', '2XL'], ['BLACK', '3XL'], ['BLACK', '4XL'], ['WHITE', 'WS'], ['WHITE', 'WM'], ['WHITE', 'WL'], ['WHITE', 'S'], ['WHITE', 'M'], ['WHITE', 'L'], ['WHITE', 'XL'], ['WHITE', '2XL'], ['WHITE', '3XL'], ['WHITE', '4XL']]
C:\Users\Administrator>py
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> colors = ["BLACK", "WHITE"]
>>> sizes = ["WS", "WM", "WL", "S", "M", "L", "XL", "2XL", "3XL", "4XL"]
>>> [[x,y] for x in colors for y in sizes]
[['BLACK', 'WS'], ['BLACK', 'WM'], ['BLACK', 'WL'], ['BLACK', 'S'], ['BLACK', 'M'], ['BLACK', 'L'], ['BLACK', 'XL'], ['BLACK', '2XL'], ['BLACK', '3XL'], ['BLACK', '4XL'], ['WHITE', 'WS'], ['WHITE', 'WM'], ['WHITE', 'WL'], ['WHITE', 'S'], ['WHITE', 'M'], ['WHITE', 'L'], ['WHITE', 'XL'], ['WHITE', '2XL'], ['WHITE', '3XL'], ['WHITE', '4XL']]
>>>
|