有人向我提出问题,但我不太明白他们要我做什么。他们给我的问题是:
本练习的目标是创建两个名为
和x_list
的列表,其中分别包含变量y_list
和x
的 10 个实例。您还需要通过连接您创建的两个列表来创建一个名为y
的列表,其中包含变量big_list
和x
,各 10 次。y
随之而来的代码是:
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!
要获取多个实例,只需将列表相乘即可:
x_list = [x]*10
y_list = [y]*10
x = object()
y = object()
# TODO: change this code
x_list = [x]*10
y_list = [y]*10
big_list = []
z = x_list + y_list
big_list.append(z)
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!")
要制作 10 个实例,我们只需将其与
10
相乘,然后在 x_list
中添加 y_list
和 big_list
。
x = object()
y = object()
# TODO: change this code
x_list = [x]*10
y_list = [y]*10
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!