-3

我收到了一个问题,但我真的不明白他们要我做什么。他们给我的问题是:

本练习的目标是创建两个名为x_list和的列表y_list,它们分别包含变量x和的 10 个实例y。您还需要创建一个名为 的列表big_list,其中包含变量xy,每个 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!
4

1 回答 1

5

要获得多个实例,只需将列表相乘:

x_list = [x]*10
y_list = [y]*10
于 2013-10-02T09:34:36.257 回答