繁体   English   中英

Python中的串联,串联列表和字符串

[英]Concatenation in Python, concatenating lists and strings

我不确定这是否是正确的语法,但是我想打印出列表中的特定元素。

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 

'??' 是我尝试%d和%o的地方,但得到:“ TypeError:%o format:数字是必需的,而不是列表”

您可以使用%r

'r':字符串(使用repr()转换任何Python对象)。

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

我不知道kcluster函数,但似乎它返回一个列表,而不是一个数字。 或者,您可以尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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