I am reading from a file and creating two lists, then sorting them after sorting I am trying to compare each elements number of occurances in a list but printing them with that number for example if a list is something like {1,2,3,3,3,5,5,5,6} then I want to print a report like 1 count =1 2 count =1 3 count = 3 5 count = 5 6 count =6.

this is what I have so far just one small thing I need to get it done.

def main():
    data = open("Iris.csv",'rU')
    petalWidths = []
    sepalWidths = []
    for line in data:
        line =  line.split(',')
        sepalWidths.append(line[1])
        petalWidths.append(line[3])
    sepalWidths.sort()
    checkForWidthCount(sepalWidths)
    petalWidths.sort()
    print sepalWidths

##        print petalWidths

def checkForWidthCount(mylist):


    for x in range(0,len(mylist)-1):
            count = 0
            for y in range (0,len(mylist)-1):

                    if mylist[x] == mylist[y]:
                            count+=1
                    else:
                            y+=count

            #x+=count                                
    print mylist[x],count

main()

The set function gives a set of the unique items in a list. Iterate through that and print the count method of the list for each item.

#!/usr/bin/env python
def checkForWidthCount(mylist):
    for x in set(mylist):#Iterate through set of unique list items
        print x, 'count = ', mylist.count(x)

ExampleOfList = [1,2,3,3,3,5,5,5,6]
checkForWidthCount(ExampleOfList)

You don't check the last element, so possibly want to use

        for x in range(0,len(mylist)-1):
            ## don't compare "x" to itself and go to the end of the list
            for y in range (x+1,len(mylist)):

This will give you a count for each item, so you will get three different "3" counts. Since the list is sorted, you can store the number in a separate variable and compare this number with previous number and only count if they are different.

Edited 4 Years Ago by woooee: None

The values are sorted as long as they are under 10 but they are sorted as strings, I do not understand the bit about 5 count 5, 6 count 6.

You could just us itertools.groupby and check the lengths of the groups, if it is just counts.

I guess this would explain better, following is my output that is printing each element in the list and printing the count of the element its working fine if you see 2.0 just had one occurance and so it was printed with 1 and rest of that had more occurances they were printed with their count. What I want is to keep the count same but instead of printing duplicates just print one unique

2.0 1     2.2 3     2.2 3     2.2 3    2.3 4    2.3 4    2.3 4    2.3 4    2.4 3    2.4 3    2.4 3    2.5 8    2.5 8    2.5 8    2.5 8    2.5 8    2.5 8    2.5 8    2.5 8    2.6 5    2.6 5    2.6 5    2.6 5    2.6 5

desired output 

2.0 1     2.2 3     2.3 4    2.4 3   2.5 8    2.6 5
data = '''2.0 2.2 2.2 2.2 2.3 2.3 2.3 2.3 2.4 2.4 2.4 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.6 2.6 2.6 2.6 2.6'''.split()
prev = None
for d in data:
    if prev != d:
        if prev:
            print prev, count
        prev = d
        count = 0
    count += 1

print prev, count

'''Output:
2.0 1
2.2 3
2.3 4
2.4 3
2.5 8
2.6 5
'''

Edited 4 Years Ago by pyTony

this is third time I am trying to post,

can you explain

if prev != d:
    if prev:
        print prev, count
    prev = d
    count = 0
count += 1

how that is working?

that is great if you could explain the logic in words? I know for loop is simple for loop that is just iterating through the list until the end, then if prev is not equal d then check if prev? what is that if doing? Thanks

The block is ready counted when we meet start of next block or end of file after the for exits (easy to forget but last block has not next block after).

This question has already been answered. Start a new discussion instead.