i started teaching myself python last night via python documentation, tutorials , questions.
so far can ask user file, open , read file, remove # , beginning \n in file, read each line array, , count number of integers per line.
i want calculate number of unique integers per line. realized python uses set capability thought work calculation. however, receive value of 1 greater prior value (i show you). looked @ other posts related sets , not see not missing , have been stumped while.
here code:
with open(filename, 'r') file: line in file: if line.strip() , not line.startswith("#"): #calculate number of integers per line names_list.append(line) #print "there ", len(line.split()), " numbers on line" #print names_list #calculate number of unique integers myset = set(names_list) print myset myset_count = len(myset) print "unique:",myset_count
for further explanation:
names_list is:
['1 2 3 4 5 6 5 4 5\n', '14 62 48 14\n', '1 3 5 7 9\n', '123 456 789 1234 5678\n', '34 34 34 34 34\n', '1\n', '1 2 2 2 2 2 3 3 4 4 4 4 5 5 6 7 7 7 1 1\n']
and my_set is:
set(['1 2 3 4 5 6 5 4 5\n', '1 3 5 7 9\n', '34 34 34 34 34\n', '14 62 48 14\n', '1\n', '1 2 2 2 2 2 3 3 4 4 4 4 5 5 6 7 7 7 1 1\n', '123 456 789 1234 5678\n'])
the output receive is:
unique: 1 unique: 2 unique: 3 unique: 4 unique: 5 unique: 6 unique: 7
the output should occur is:
unique: 6 unique: 3 unique: 5 unique: 5 unique: 1 unique: 1 unique: 7
any suggestions why set per line not calculating correct number of unique integers per line? suggestions on how improve code in general (if like) because started learning python myself last night , love tips. thank you.
the problem iterating on file appending each line list names_list
. after that, build set out of these lines. text file not seem have duplicate lines, printing length of set displays current number of lines have processed.
here's commented fix:
with open(filename, 'r') file: line in file: if line.strip() , not line.startswith("#"): numbers = line.split() # splits string whitespace , gives list unique_numbers = set(numbers) # builds set of strings in numbers print(len(unique_numbers)) # prints number of items in set
note using processed line , build set (after splitting line). original code stores lines , builds set lines in each loop.
Comments
Post a Comment