我已經閱讀了這個answer,這可能是在Python中隨機化字符串列表的最佳方式。我只是想知道,然後如果這是這樣做的最有效的方式,因爲我通過下面的代碼具有約3000萬的元素列表:Python中數百萬元素的隨機列表高效
import json
from sets import Set
from random import shuffle
a = []
for i in range(0,193):
json_data = open("C:/Twitter/user/user_" + str(i) + ".json")
data = json.load(json_data)
for j in range(0,len(data)):
a.append(data[j]['su'])
new = list(Set(a))
print "Cleaned length is: " + str(len(new))
## Take Cleaned List and Randomize it for Analysis
shuffle(new)
如果有一個更有效的方式來做到這一點,我d非常感謝關於如何做到這一點的任何建議。
感謝,
謝謝你的建議,itervalues怎麼會因爲我以爲數據是一個列表工作?而不是一個字典 - 這似乎也是我運行它時的問題:'AttributeError:'list'object has no attribute'itervalues'' – eWizardII
他的意思是'a.update(d ['su']代表數據中的d )'.itervalues'用於字典。基本上你沒有理由在這裏使用'range'。 – milkypostman