hveagle 发表于 2022-7-5 13:41:03

求助(列表VII)问答题5

本帖最后由 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']]

hveagle 发表于 2022-7-5 13:46:35

本帖最后由 hveagle 于 2022-7-5 13:56 编辑

=====开始求助=====

wp231957 发表于 2022-7-5 13:53:20

C:\Users\Administrator>py
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) 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"]
>>> [ 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']]
>>>

傻眼貓咪 发表于 2022-7-5 13:57:52

colors = ["BLACK", "WHITE"]
sizes = ["WS", "WM", "WL", "S", "M", "L", "XL", "2XL", "3XL", "4XL"]
result = [ for a in colors for b in sizes]
print(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']]

hveagle 发表于 2022-7-5 13:58:19

=====结束求助=====

hveagle 发表于 2022-7-5 14:00:10

即将把奖励鱼币设置为0
页: [1]
查看完整版本: 求助(列表VII)问答题5