简体   繁体   中英

Need some specification on how to resolve below code?

for i in range(true_k):
 print(“Cluster %d”, % i),
 for ind in order_centroids[i, :10]:
 print(‘ %s’ % terms[ind])

Error says as below

for i in range(true_k):
 print(“Cluster %d”, % i),
 for ind in order_centroids[i, :10]:
 print(‘ %s’ % terms[ind])
  File "<ipython-input-21-459006fea4dd>", line 2
    print(“Cluster %d”, % i),
                 ^

SyntaxError: invalid character in identifier

print(“Cluster %d”, % i)

should be

print(“Cluster %d” % i)

You don't need the comma.

I got it resolved.Thanks everyone for your response i am pretty new in coding. The comma was definitely a mistake and then i wrote my code as below

for i in range(true_k):
 print('Cluster'+ '%s' % i),
 for ind in order_centroids[i, :10]:
  print(' %s' % terms[ind])

So, I have reason to believe you're writing your code in word or something due to having smart quotes and poor indentation. Go download Visual Studio Code and lookup how to use it, it's a very popular IDE and will assist you greatly in programming. As well as typing the correct type of quotation marks!

I believe this is what you need.

for i in range(true_k):
      print("Cluster {}".format(i))
      for ind in order_centroids[i, :10]:
         print("{}".format(terms[ind]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM