0

i'm not sure if this is the correct syntax to do this, but i want to print out a specific element in a list.

user,activity,data=readfile('data.txt')
kclust,clusters=kcluster(data,k=3)
for i in range(len(kclust)):
    print "Cluster %d: ??" % (i+1,clusters[i])
    print [[userobjectIds[r] for r in kclust[i]][:3]]
    print 

the '??' is where i've tried %d and %o but get: "TypeError:%o format:a number is required, not list"

4
  • 1
    Are you expecting to print a number? You can verify that clusters[i] actually contains a number (or whatever type you intended) by reading the output of print type(clusters[i]). Commented Jun 22, 2011 at 19:51
  • 1
    Any time you're doing for i in range(len(foo)): you're probably doing it wrong. Do instead for i, sublist in kclust.enumerate(): and the list comp can be [userobjectIds[v] for v in sublist[:3]] Commented Jun 22, 2011 at 20:03
  • @Daenyth: Should be enumerate(kclust) (unless klust is something different than a list). But this is definitely the correct approach. Commented Jun 22, 2011 at 20:12
  • @Felix Kling: Derp, brain fart. You're right of course. Commented Jun 22, 2011 at 20:13

2 Answers 2

5

You can use %r:

'r': String (converts any Python object using repr()).

print "Cluster %d: %r" % (i+1,clusters[i])
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know about kcluster function, but it seems it returns a list, not a number. Alternatively, you could try:

print "Cluster %d:"%(i+1), clusters[i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.