Reputation: 199
I just started learning Pythonby myself last night. I have accomplished a few simple things like getting a file from a user, opening and reading that file into a list line by line, calculating the number of integers per line, calculating the number of unique integers per line using set, and calculating the number that appears the most per line.
An additional interesting concept that I am looking into and would like to accomplish is calculating the number of integers based on the number of adjacent integers. That might sound a bit confusing so I will explain below:
Say I have a list with the values:
['1', '2', '2', '2', '2', '2', '3', '3', '4', '4', '4', '4', '5', '5', '6', '7', '7', '7', '1', '1\n']
(I believe I would have to convert the strings to int?) The first integer is a 1 and the second integer is a 2. The difference is not 0 is the two integers are not the same so do not increment a counter. Take the next integer which is a 2 and the previous integer which is a 2. The difference is 0 so the two integers are the same so increment a counter. And so on. The final value would be 8 in this case.
I looked in adjacent integer calculations on SO and the Internet, and only found subtraction algorithms which I thought could apply.
Here is what I have so far without currently complicating with converting the strings to int (which I have a question on after pertaining to 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] for n in range(1,len(x))]
print xdiff
all([xdiff[0] == xdiff[n] for n in range(1,len(xdiff))])
for 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 is the 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 to prints are just for testing, but the last print should be 9 when the calculations are correct.
Any suggestions of how to improve my algorithm to calculate the total number of integers in a list excluding the same adjacent integers (like I demonstrated above for clarification)? Also, since I just started teaching myself Python, any suggestions on general improvement for my code is greatly appreciated. Thank you.
UPDATE: So I realized that my calculation value is incorrect and the total number should be 8 and not 9. Here is a better explanation: I have the following numbers in a list: [1,2,2,2,2,2,3,3,4,4,4,4,5,5,6,7,7,7,1,1] There are 20 elements total. The number of adjacent integers is 8. Start at 1 and look at 2. 1 and 2 are not the same so increment. Look at next value of 2 and previous value of 2. 2 and 2 are the same. Look at the next value of 2 and the previous value of 2. 2 and 2 are the same. Look at the next value of 2 and the previous value of 2. 2 and 2 are the same. Look at the next value of 2 and the previous value of 2. 2 and 2 are the same. Look at the next value of 3 and the previous value of 2. 3 and 2 are not the same so increment. And so on.
Now all of the answers are one less than the correct answer, so is that due to the starting at data[0] and I just need to add one to my 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 the file corresponding to the name of the file
#the user entered. Open and read the file.
with open(filename, 'r') as file:
#Read the file in line by line
for line in file:
#Remove all empty lines and lines that start with a # sign
if line.strip() and not line.startswith("#"):
#Calculate the number of integers per line
names_list.append(line)
#print "integers:", len(line.split())
print names_list
#Calculate the number of bursts
result = sum(int(names_list[i]) == int(names_list[i+1]) for i 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
Upvotes: 0
Views: 100
Reputation: 723
here is a working code:
numbers = [1,3,7,11,25,36,57,678,999]
count = sum([numbers[i] == numbers[i+1] for i in range(len(numbers)-1)])
>>> count
8
For your 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] for i in range(len(data)-1)])
>>> result
7
Upvotes: 1
Reputation: 31339
The nice thing about python is using what you already get for free. Like zip
:
>>> numbers = [1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, -6, 0]
>>> pairs = zip(numbers, numbers[1:])
>>> sum(x == y for x, y in pairs)
7
This generates all pairs by zipping these two lists:
[1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, -6, 0] # numbers
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, -6, 0] # numbers[1:]
# generates: [(1, 0), (0, 0), (0, 0), (0, 0), (0, 1), (1, 0),
# (0, 1), (1, 0), (0, 0), (0, 0), (0, 1), (1, 0),
# (0, 1), (1, 1), (1, 0), (0, 0), (0, -6), (-6, 0)]
Then, it checks which are the same (It also uses the fact that True
counts as 1
, so a simple sum
works).
Keeping indices and handling edge cases can usually be avoided.
Upvotes: 2
Reputation: 160587
More simply, use a counter that increments by 1 each time and checks whether adjacent values are equal:
l1 = ['1', '2', '2', '2', '2', '2', '3', '3', '4', '4', '4', '4', '5', '5', '6', '7', '7', '7', '1', '1\n']
nums = [int(num) for num in l1]
sum(nums[i] == nums[i+1] for i in range(len(nums)-1))
Out[157]: 12
Upvotes: 1