【问题标题】:SyntaxError: invalid syntax astype('float32')[:limit] in python3 [duplicate]SyntaxError:python3 中的无效语法 astype('float32')[:limit] [重复]
【发布时间】:2020-01-12 22:28:41
【问题描述】:

这段代码在python2中运行,但是当我想在python3中运行时,出现这个错误:

    images, targets = data
    rng_state = numpy.random.get_state()
    numpy.random.shuffle(images)
    numpy.random.set_state(rng_state)
    numpy.random.shuffle(targets)
    if limit is not None:
        print "WARNING ONLY FIRST {} MNIST DIGITS".format(limit)
        images = images.astype('float32')[:limit]
        targets = targets.astype('int32')[:limit]
    if n_labelled is not None:
        labelled = numpy.zeros(len(images), dtype='int32')
        labelled[:n_labelled] = 1

那么在 python3 中正确的语法是什么?

      images = images.astype('float32')[:limit]
             ^
   SyntaxError: invalid syntax

【问题讨论】:

  • print("WARNING ONLY FIRST {} MNIST DIGITS".format(limit))
  • @CristiFati 哦,谢谢,我没注意。

标签: python python-3.x


【解决方案1】:

在 Python 3 中,print 是一个函数,因此您需要将其参数括在括号中:

print("WARNING ONLY FIRST {} MNIST DIGITS".format(limit))

【讨论】:

  • 是的,我不注意。谢谢你:)
猜你喜欢
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多