My problem is this,
i need to try to create a dictionary that will hold the count values of each cluster from a dataset i am using.
I want my programme to use a while loop to enter keys into the dictionar开发者_JAVA技巧y based on an integer from user input.
here is some code
numberofclusters = raw_input("Enter the number of clusters")
clusters = {}
while numberofclusters >= 0:
so if the user entered 3 the dictionary should look like this
{ cluster1: 0, cluster2: 0, cluster3: 0
}
how would i get the current number from the numberofclusters
variable? so that i can append "cluster[x]"
to the dictionary?
For starters, you'll want to re-think your loop logic. As it stands now, your while loop will continue infinitely -- numberOfClusters will always be greater than 0 (unless the user enters a negative value or 0 as input).
You should consider using a for loop, instead:
for i in range(0,numberOfClusters):
# loop logic
This will iterate from 0 up to numberOfClusters, and you'll have access to which iteration you are on by reading the variable "i".
Hope this helps.
>>> num_of_clusters = int(raw_input('Number: '))
Number: 3
>>> clusters = {}
>>> for i in range(1, num_of_clusters+1):
clusters['cluster{0}'.format(i)] = 0
>>> clusters
{'cluster2': 0, 'cluster3': 0, 'cluster1': 0}
精彩评论