我有一个问题,但我真的不明白他们要求我做什么。他们给我的问题是:
本练习的目标是创建两个名为
x_list
和y_list
的列表,分别包含变量x
和y
的10个实例。您还需要通过连接您创建的两个列表来创建名为big_list
的列表,其中包含变量x
和y
,每个变量10次。
随之而来的代码是:
x = object()
y = object()
# change this code
x_list = [x]
y_list = [y]
big_list = x_list + y_list
print "x_list contains %d objects" % len(x_list)
print "y_list contains %d objects" % len(y_list)
print "big_list contains %d objects" % len(big_list)
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print "Almost there..."
if big_list.count(x) == 10 and big_list.count(y) == 10:
print "Great!"
结果是:
x_list contains 10 objects
y_list contains 10 objects
big_list contains 20 objects
Almost there...
Great!
答案 0 :(得分:5)
要获得多个实例,只需将列表相乘:
x_list = [x]*10
y_list = [y]*10