Error checking whether an element is in a list - python

i am trying to check whether certain elements are in a list, and to execute numerical update but i keep getting an error (below).
"if h2output[1] not in h1output == True or h2output[2] not in h1output == True:
IndexError: list index out of range"
doublewin = 0
h1output = []
h2output = []
h3output = []
v1output = []
v2output = []
v3output = []
d1output = []
d2output = []
for i in h1:
if i not in h1output:
h1output.append(i)
if len(h1output) == 2:
doublewin += 1
for i in h2:
if i not in h2output:
h2output.append(i)
if len(h2output) == 2:
if h2output[1] not in h1output == True or h2output[2] not in h1output == True:
doublewin += 1

As len(h2output)==2, it has only 2 positions, which in python starts at zero, therefore h2output[2] is out of bounds, index must be 0 or 1

You have hardcoded indexes in h2output[1] and h2output[2]. Either one of them is causing the issue. Please check the size of the list.
Remove True booleans in if condition as it is unnecessary.

Related

Change Value in a list based on previous condition

I have a list of zeros and ones.
I am trying to replace the a value of 1 with a 0 if the previous value is also a 1 for a desired output as shown below.
list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = [1,0,0,0,0,0,1,0,1,0,0]
I've tried using a for loop to no avail. Any suggestions?
How about this for loop:
list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = []
ant=0
for i in list:
if ant ==0 and i==1:
new_list.append(1)
else:
new_list.append(0)
ant=i
question_list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = [question_list[0]] # notice we put the first element here
for i in range(1, len(question_list) + 1):
# check if the current and previous element are 1
if question_list[i] == 1 and question_list[i - 1] == 1:
new_list.append(0)
else:
new_list.append(question_list[i])
The idea here is we iterate over the list, while checking the previous element.

Strange behavior with lists in Python

I have noticed that probability_matrix seems to become adapted_given_board despite never being assigned it.
def build_probability_matrix(self):
r = 0
p = -1
adapted_given_board = self.probability_matrix
print(self.game.board.given_board)
for i in range(len(self.game.board.given_board)-1):
p += 1
if self.game.board.given_board[i] == '\n':
r += 1
p = -1
else:
adapted_given_board[r][p] = self.game.board.given_board[i]
print(adapted_given_board)
This assignment:
adapted_given_board = self.probability_matrix
is a reference, not a copy. That is, you're creating a new name for self.probability_matrix, not a new list that has a copy of the contents.
So when you do:
adapted_given_board[r][p] = self.game.board.given_board[i]
it's the exact same as if you'd done:
self.probability_matrix[r][p] = self.game.board.given_board[i]
Be careful about trying to use copy to fix this, since you're working with two-dimensional lists; you might end up just pushing the problem down one level. There is such a thing as deepcopy, but here's one idea for a very minimal fix that just allocates new entries in the matrix before you assign to them:
def build_probability_matrix(self):
r = 0
p = -1
adapted_given_board = [[]] # start with one row that has zero cells
print(self.game.board.given_board)
for i in range(len(self.game.board.given_board)-1):
p += 1
adapted_given_board[r].append(None) # add a cell
if self.game.board.given_board[i] == '\n':
r += 1
adapted_given_board.append([]) # add a row
p = -1
else:
adapted_given_board[r][p] = self.game.board.given_board[i]
print(adapted_given_board)
Or you could simply append your new elements rather than assigning them by index...
def build_probability_matrix(self):
adapted_given_board = [[]]
print(self.game.board.given_board)
for element in self.game.board.given_board:
if element == '\n':
adapted_given_board.append([])
else:
adapted_given_board[-1].append(element)
print(adapted_given_board)

Why can I print this list of lists, but only append all up to and not including the last element of each iteration?

I can print the following list of lists fine, but when I append to an empty list, it skips the last on each iteration or gives me an index out of range error when I add one more.
This works:
ordered_results = []
temp = []
A = len(results[1])-2
i = 1
while i < len(results):
x = 0
y = 1
while x < A:
temp = [results[i][0], results[0][x], results[i][y]]
print(temp)
x+=1
y+=1
temp = [results[i][0], results[0][x], results[i][y]]
print(temp)
i+=1
ordered_results
Note: len(results[0]) = 240 and len(results[1] = 241
If you replace "print" with ordered_results.append(temp) it skips:
results[i][0], results[0][239], results[i][240]
each iteration.
(Note the code was expanded as I am messing around trying to figure this out, it was more compact before).

Check whether there are duplicate values (which is > 0) in a list in Python

I want to check whether there are duplicate elements which is more than 0 in an array list.
if [1,0,0,0,1,2] = true
if [0,0,0,0,0,0] = false
How can I get this result?
I guess OP wants to handle natural numbers only. Try this:
def is_duplicated_natural_numbers(input):
# make it >0
natural_numbers_list = list(filter(lambda x: x > 0, input))
# remove duplicates
set_list = list(set(natural_numbers_list))
# if natural_numbers_list == set_list, no natural numbers duplicates
return natural_numbers_list != set_list
print(is_duplicated_natural_numbers([1,0,0,0,1,2])) # True
print(is_duplicated_natural_numbers([0,0,0,0,0,0])) # False
print(is_duplicated_natural_numbers([1,2,3,4,5,1])) # True
Use a dictionary to keep a count of elements and if an element occurs twice (which is not zero) then your answer is true so just break from the loop.
Try This :
l = [0,0,0,0]
dic = {}
flag = False
for i in l:
if i in dic:
dic[i]+=1
if dic[i]>1 and i!=0:
flag = True
break
else:
dic[i] = 1
print flag
Note : Better ways are there but this one is really simple to understand.

If-else inside a while statement

Learning to code with Python 3. I am confused why my else statement keeps executing with the code below:
def countSubStringMatch(target, key):
index = 0
instance_list = []
while index <= len(target):
match = target.find(key,index)
if match > -1:
instance_list.append(match)
index += 1
elif match == -1:
return None
return sorted(set((instance_list)))
target1 = 'mjzzmjzzmjzz'
key1 = 'zz'
print(countSubStringMatch(target1, key1))
The point of this code is to list the indexes at which the key starts. I had the code running fine when there are actual instances of the key within target, but I am trying to edit it to return None when there are no instances. This was my code before the edit:
def countSubStringMatch(target, key):
index = 0
instance_list = []
while index <= len(target):
match = target.find(key,index)
if match > -1:
instance_list.append(match)
index += 1
return sorted(set((instance_list)))
target1 = 'mjzzmjzzmjzz'
key1 = 'zz'
print(countSubStringMatch(target1, key1))
You'll eventually always get -1, as you inclement index. So yes, your elif is going to match and you end up returning None:
>>> target1 = 'mjzzmjzzmjzz'
>>> key1 = 'zz'
>>> target1.find(key1, 0)
2
>>> target1.find(key1, 3)
6
>>> target1.find(key1, 7)
10
>>> target1.find(key1, 11)
-1
So when index = 11, target.find() returns -1 for your sample input. Since len(target) is 12, that is still within your loop.
Only return None when instance_list is empty. Also, increment your index to step past the last-found index, there is no point in incrementing the index by 1 each time; that way you avoid all the duplicate indices and removes the need to use a set:
def countSubStringMatch(target, key):
index = 0
instance_list = []
while index <= len(target) - len(key):
match = target.find(key, index)
if match == -1:
break
instance_list.append(match)
index = match + 1
return instance_list or None
There is no point in searching from an index equal to the length of the target minus the length of the key; you are not going to find the key at index 11.
This returns None if no matches were found, rather than a list. You may want to reconsider this; testing for an empty list is just as easy and makes your API consistent (always returning a list, which may be empty):
>>> target1 = 'mjzzmjzzmjzz'
>>> key1 = 'zz'
>>> countSubStringMatch(target1, key1)
[2, 6, 10]
>>> countSubStringMatch(target1, 'foo') is None
True

Categories