返回没有单引号的东西

时间:2016-01-28 02:52:09

标签: python list return

所以我有一个看起来像这样的函数:

fid = open('data/data' + str(digit), 'rb')
dim = np.fromfile(fid, dtype=np.uint8) # read in entire dataset

# loop through the thousand examples and smartly index them to get 28x28 images
for i in xrange(0, 1000):
    # indices to go through data file
    index = i*28*28        
    nextindex = (i+1)*28*28

    # get the images in 28x28 format & convert all pixels to binary
    newdim = dim[index:nextindex]
    newdim = newdim.reshape(28, 28)

我想回头看起来像这样:

self=[1,2,3,4,5,6,7,8]

def evens(self):
    evens=[]
    for item in self:
        if item%2==0:
            evens.append(item)
    return 'intlist({})'.format(evens)

但我得到了这个:

intlist[2,4,6,8]

如何在没有单引号的情况下获得回报?

这个函数实际上是我写的一个类。

2 个答案:

答案 0 :(得分:3)

你是否在REPL中运行它?

你应该print()结果。默认情况下,REPL显示结果的repr

NB。查看关于使用self作为变量的评论

>>> self=[1,2,3,4,5,6,7,8]
>>> 
>>> def evens(self):
...     evens=[]
...     for item in self:
...         if item%2==0:
...             evens.append(item)
...     return 'intlist({})'.format(evens)
... 
>>> evens(self)
'intlist([2, 4, 6, 8])'
>>> print(evens(self))
intlist([2, 4, 6, 8])

答案 1 :(得分:1)

打印功能正常

python3:

>>> self=[1,2,3,4,5,6,7,8]
>>> print('intlist({})'.format(self))
intlist([1, 2, 3, 4, 5, 6, 7, 8])