1

I decided to make myself a little bit more useful, so I thought that learning computer languages would be a pretty good idea. I started with python, because I everyone I asked recommended it for a beginner.

I found an interactive website to do some exercises before going to the developing tool for practice. I know most of you will laugh, but I got stuck at exactly this point:

x = object()
y = object()

# change this code
x_list = [x]
y_list = [y]
big_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!"

Well, I figured out the big_list. I have to write [x_list * 10 + y_list * 10]

The thing that I cannot figure out the last 30 minutes, is what values to I need to set in the object brackets (1st line of the command)

Thanks for your help in advance!

4

3 回答 3

1

I'm guessing you're looking at: Basic Operators - Learn Python

They are showing how operators + and * can apply to lists as well as other types. I'm too unhelpful to just give a solution, but here's a nudge in the right direction (I hope):

>>> 3 * 6
18
>>> "hi" * 6
'hihihihihihi'
>>> ["hi"] * 6
['hi', 'hi', 'hi', 'hi', 'hi', 'hi']
>>> my_list = ["hi"]
>>> my_list *= 6
>>> my_list
['hi', 'hi', 'hi', 'hi', 'hi', 'hi']
>>> my_list.count("hi") == 6
True
>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]

When learning and exploring Python in general, I highly recommend experimentation like this in an interactive python shell.

于 2013-10-23T22:30:31.757 回答
0

我认为你对object.

object只是一个引用内置类型的标识符,在python中所有类都继承自对象。仅出于作者使用它的目的,您只是在创建一个对象实例,因此您不需要向它传递任何内容。

要验证这一点,请启动 python 解释器并执行此操作

>>>x=object()
>>>print x
<object object at 0x01E214D8>

现在,如果您打印文档字符串

>>>print x.__doc__
'The most base type'

现在回到你的代码

x_list = [x]在这里,您将 x(类型对象)添加到列表中。

如果你现在这样做x_list = [x,1,'hello'],你的列表有一个对象、整数和字符串

特别是这个例子你不需要做任何事情x,你应该在列表上应用的所有操作(x_list、y_list 和 big_list)。Python 有非常好的dir功能,当带参数传递时,它返回有效属性列表dir(x_list)

于 2013-10-24T05:58:29.293 回答
0

好吧,从您的问题来看, object() 没有任何论据。我的意思是在括号中。

它只是用来创建一个对象类型的空对象。

通常我们在 python 中创建的类会继承这个对象。

于 2013-10-24T05:51:37.290 回答