簡體   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