另一种思路
print [m+n+m for m in '123456789' for n in '0123456789'] 输出结果也对啊?
print [m+n+m for m in '123456789' for n in '0123456789'] 输出结果也对啊?
2016-04-13
题主的代码:
print [m+n+m for m in '123456789' for n in '0123456789'] ## 打印结果如下: ['101', '111', '121', '131', '141', '151', '161', '171', '181', '191', '202', '212', '222', '232', '242', '252', '262', '272', '282', '292', '303', '313', '323', '333', '343', '353', '363', '373', '383', '393', '404', '414', '424', '434', '444', '454', '464', '474', '484', '494', '505', '515', '525', '535', '545', '555', '565', '575', '585', '595', '606', '616', '626', '636', '646', '656', '666', '676', '686', '696', '707', '717', '727', '737', '747', '757', '767', '777', '787', '797', '808', '818', '828', '838', '848', '858', '868', '878', '888', '898', '909', '919', '929', '939', '949', '959', '969', '979', '989', '999']
列表的每一个元素都是字符串,而不是课程要求的数字。 稍加修改就可以返回正确结果:
print [int(m+n+m) for m in '123456789' for n in '0123456789'] ## 解释, 给 m+n+m 字符串加上int() 转换成 整数。
举报