python - Difference of elements to find same adjacent -


i started learning pythonby myself last night. have accomplished few simple things getting file user, opening , reading file list line line, calculating number of integers per line, calculating number of unique integers per line using set, , calculating number appears per line.

an additional interesting concept looking , accomplish calculating number of integers based on number of adjacent integers. might sound bit confusing explain below:

say have list values:

['1', '2', '2', '2', '2', '2', '3', '3', '4', '4', '4', '4', '5', '5', '6', '7', '7', '7', '1', '1\n'] 

(i believe have convert strings int?) first integer 1 , second integer 2. difference not 0 2 integers not same not increment counter. take next integer 2 , previous integer 2. difference 0 2 integers same increment counter. , on. final value 8 in case.

i looked in adjacent integer calculations on , internet, , found subtraction algorithms thought apply.

here have far without complicating converting strings int (which have question on after pertaining this, please):

x = [1,2,2,2,2,2,3,3,4,4,4,4,5,5,6,7,7,7,1,1] count = 0 xdiff = [x[n]-x[n-1] n in range(1,len(x))] print xdiff all([xdiff[0] == xdiff[n] n in range(1,len(xdiff))]) n in range(1,len(xdiff)):     last = x[n-1] if xdiff!=0:       print "different numbers"       print last       if xdiff==0:         print "same numbers"         count+=1 

here output:

[1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, -6, 0] different numbers 7 

the first prints testing, last print should 9 when calculations correct.

any suggestions of how improve algorithm calculate total number of integers in list excluding same adjacent integers (like demonstrated above clarification)? also, since started teaching myself python, suggestions on general improvement code appreciated. thank you.

update: realized calculation value incorrect , total number should 8 , not 9. here better explanation: have following numbers in list: [1,2,2,2,2,2,3,3,4,4,4,4,5,5,6,7,7,7,1,1] there 20 elements total. number of adjacent integers 8. start @ 1 , @ 2. 1 , 2 not same increment. @ next value of 2 , previous value of 2. 2 , 2 same. @ next value of 2 , previous value of 2. 2 , 2 same. @ next value of 2 , previous value of 2. 2 , 2 same. @ next value of 2 , previous value of 2. 2 , 2 same. @ next value of 3 , previous value of 2. 3 , 2 not same increment. , on.

now of answers 1 less correct answer, due starting @ data[0] , need add 1 total?

update: output:

['1', '2', '3', '4', '5', '6', '5', '4', '5\n'] bursts: 0  ['14', '62', '48', '14\n'] bursts: 0  ['1', '3', '5', '7', '9\n'] bursts: 0  ['123', '456', '789', '1234', '5678\n'] bursts: 0  ['34', '34', '34', '34', '34\n'] bursts: 4   ['1\n'] bursts: 0  ['1', '2', '2', '2', '2', '2', '3', '3', '4', '4', '4', '4', '5', '5', '6', '7', '7', '7', '1', '1\n'] bursts: 12 

code:

#open file corresponding name of file #the user entered. open , read file. open(filename, 'r') file:      #read file in line line     line in file:          #remove empty lines , lines start # sign         if line.strip() , not line.startswith("#"):              #calculate number of integers per line             names_list.append(line)             #print "integers:", len(line.split())              print names_list             #calculate number of bursts             result = sum(int(names_list[i]) == int(names_list[i+1]) in range(len(names_list)-1))             print "bursts:", result 

output should be:

bursts:9 bursts:4 bursts:5 bursts:5 bursts:1 bursts:1 bursts:8 

here working code:

numbers = [1,3,7,11,25,36,57,678,999] count = sum([numbers[i] == numbers[i+1] in range(len(numbers)-1)]) >>> count 8 

for example:

data = [1,2,2,2,2,2,3,3,4,4,4,4,5,5,6,7,7,7,1,1] result = sum([data[i] == data[i+1] in range(len(data)-1)]) >>> result 7 

Comments