Identifying index of list of lists - list

Hey I have a list containing four lists and I would like to iterate over the entire thing and print the index for all occurrences of a specific number or variable. So for example I have:
list_of_lists = [[0,0,0,0],[0,0,0,0],[0,2,0,0],[0,0,0,0]]
lx = 0
for x in list_of_lists:
ly = 0
for a in x:
if a == 2:
print(lx,ly)
ly += 1
lx += 1
Now this does print "2 1" which is correct but rather than print once, it prints infinitely. I'm not sure what is wrong with my logic but I'm pretty sure I am making a simple error. I am new to programming as you may be able to tell. Thanks for any help!

I don't think you did anything wrong here. I tried your code and mine runs only once, are you sure your indentations are ok in your original code?

Related

Why won't my list.append() not append despite working in print output

Below is my code for the leet code problem: here
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
tripletsList = []
uniqueNums = set(nums)
uniqueNums = list(nums)
for x in range(len(uniqueNums)):
for y in range(len(uniqueNums)):
for z in range(len(uniqueNums)):
print(uniqueNums[x],uniqueNums[y],uniqueNums[z])
if (uniqueNums[x] + uniqueNums[y] + uniqueNums[z]) == 0:
if [[uniqueNums[x],uniqueNums[y],uniqueNums[z]]] not in tripletsList:
tripletsList.append([uniqueNums[x],uniqueNums[y],uniqueNums[z]])
print(tripletsList)
I realize it isn't the optimal solution but in my print line the output shows the correct combination, yet my final triplets list is not eliminating anything which leads me to believe I am not using the "not in" function of python correctly. Any help is greatly appreciated and I must be missing something that is just trivial
I tried first printing every combination of the unique list (yes tons of overlap), then I tried limiting the triplets that made it pass the == 0 check to only triplets that were not already in the triplets list (failed)

Python loop query and issue

I'm new to Python so patience may be required on your behalf :) If this is not the right area for this then please let me know. I'm having trouble with a for loop navigating through a list. The idea here is to print "True" when a consecutive set of numbers are found in the list however it shouldn't print true if the sequence contains less than 3 consecutive iterations.
For example:
Ideally the program would say "You have 3 sequences in your list". In this case it would be (1,2,3), (10,11,12) and (22,23,24). Notice there are 2 other consecutive sets of numbers in the list (7,8) and (15,16) however the program should ignore them because it's less than 3 (as mentioned above). Please find code below however I'm at a slight road block. Appreciate any pointers you may have.
list1=[1,2,3,7,8,10,11,12,15,16,22,23,24]
for i in range(len(list1)-1):
if (list1[i] + 1 == list1[i+1]):
count+=1
if count >=3:
print("True")
else:
continue
continue
print(count)
I'm sure I have read the answer in similar posts but I would like to understand the code I'm missing rather than inserting code that doesn't make sense to me.
Thanks
As the query is to have only unique numbers in the sequences .. so below code will just work fine...
list1=[1,2,3,4,5,6,7,8,9,10]
count=0
i=0
while i < (len(list1)-3):
if list1[i]+1 == list1[i+1]:
if list1[i+1]+1 == list1[i+2]:
print(list1[i], list1[i+1], list1[i+2])
i=i+2
count=count+1
i=i+1
print(count)

When a draw occurs when tracking most occurrences in a list how to find element with highest index?

lines = ["Pizza", "Vanilla","Los Angeles Pikes","Cookie Washington Tennis Festival","Water Fiesta","Watermelon"]
best= max(set(lines), key=lines.count)
print (best)
The code above returns the greatest occurrence of an element in the list, but in case there is a draw, I want it to return the element with the greatest index. So here I want Watermelon to be printed and if anything is added without a break in the tie the highest index of the draw should be printed.
I need a solution with simple basic code like that seen above and without the importing of libraries. If you could help find a good solution for this it would be really helpful.
You could add the index normalized to a value greater than the length of the array to the result of count. The normalized index will always be less than 1.0, so that it will not affect the first-order comparison, but will guarantee that there are no ties. I would use a small function to do this:
lines = ["Pizza", "Vanilla", "Los Angeles Pikes",
"Cookie Washington Tennis Festival",
"Water Fiesta", "Watermelon"]
def key(x):
return lines.count(x) + lines.index(x) / (len(lines) + 1)
best = max(set(lines), key=key)
print(best)
While your original code returned lines = "Los Angeles Pikes" in my version of Python (because of the way the hashes turned out), the new version returns "Watermelon", as expected.
You can also use a lambda, but I find that a bit harder to read:
best = max(set(lines), key=lambda x: lines.count(x) + lines.index(x) / (len(lines) + 1))

How to create multiple loops in python

I'm trying to create a basic maths program which will randomly generate a numerical question and then allows 3 attempts to complete it before it moves onto the next question however cant figure out how to make it do both together.
My code currently looks like this
print "What is your name?"
score = 0
Attempt = 0
loop = True
Name = raw_input()
import random
for i in range (1,6):
question_1_part_1 = random.randint(1,30)
question_1_part_2 = random.randint(1,30)
print "What is", question_1_part_1, "+", question_1_part_2, "?"
while Attempt <3: # inputing this while loop here means it will retry the same question.
Guess = raw_input()
Guess = int(Guess)
answer = question_1_part_1 + question_1_part_2
if Guess == answer:
print "Well done"
score = score + 1
else: print "try again"
Attempt = Attempt + 1
if Attempt == 3: print "You failed"
print Name, "your score is", score
A simple break statement will take you out of the loop when the answer is correct.
if Guess == answer:
print "Well done"
score += 1
break
else: print "try again"
Note the change to your assignment; this is considered a cleaner increment.
To answer your comment ... you don't use this when the person gets the question wrong. Your program logic freezes out the user after three wrong guesses total, on all questions. If you want them to have up to three guesses on every problem, then you have to reset the Attempt counter for every question. Pull that line into the outer loop:
for i in range (1,6):
Attempt = 0
loop = True
question_1_part_1 = random.randint(1,30)
question_1_part_2 = random.randint(1,30)
In the future, I strongly recommend that you program incrementally: get a few lines working before you add more. Insert tracing print statements to check that the values and program flow are correct. Don't remove them until that part of the code is solid -- and even then only comment them out until the entire program is working. Some of your problems stem from trying to write more than you can comfortably hold in your mind at once -- which is common at most levels of programming. :-)
Add another argument to your while loop.
while Attempt <3 and Guess != ... :
rest of loop code
Then you are exiting your loop when they get the correct answer. Or you could break from the statement when they get the answer right.

Exponent disappearing when printing numbers

this is my first post to Stack Overflow. I have used it many times before but this is the first error that I have not found a solution to thus far. I am having a problem with a Python script where when I am changing an element in a list, the item replacing the element in the list drops the scientific notation aspect (i.e. E+08). Here is a snippet of the code and terminal output that is very strange to me. Thanks in advance for all help!
for i in range(0, len(EinsteinCof)):
for j in range(0, len(finalindexing)):
if i == finalindexing[j][0]:
if len(finalindexing[j]) == 3:
EinsteinFinal = float(EinsteinCof[finalindexing[j][0]]) + float(EinsteinCof[finalindexing[j][1]]) + float(EinsteinCof[finalindexing[j][2]])
print str(EinsteinFinal)
EinsteinCof[finalindexing[j][0]] = str(EinsteinFinal)
print str(EinsteinCof[finalindexing[j][0]])
Terminal Output:
4.1079763384e+13
4.1079763384 # <-- Where did e+13 go?
2269472400.0
2269472400.0
3.1777892e+12
3.1777892e+1 # <--where did e+12 go?
9.062911e+11
9.062911e+11