= min(people, key=lambda x: x[1])
for i in people:
while i[1] == x[1]:
people.remove(i)
print(people)
Why am i getting an error here when i use the while loop? I wanted to remove elements with minimum numbers and for my case its ['Nurdin', 22] and ['See', 22]
I wanted the statement inside while loop to run if any element has number 22 but it decided to bring an error
You have an infinite loop in that while. Why do you have it, if you're already iterating with the outer for loop?
I don't have any idea how i will use it without for loop. If there is no for loop, then what will i use as i? I mean this i[1]
I meant the while loop, it is unnecessary, what you need is an if statement, not a while
Hehe, so i can't use while loop inside for loop
You can, it depends for what perpes..
You can, but in this case, it isn't a solution for your problem
Also, you're trying to modify a list while iterating over it, that's not recommended, as it will bring weird bugs that are harder to find and debug
please don't modify the list while you are iterating over it
I don't know why if i use the input function, i don't get the result that i want though. My output was supposed to print the second last minimum persons having minimum number. I expected the output to be : Brian Sii But it gave me Mamma as the output
Use max instead of min
I don't even understand why i should use max and not min lol, I thought min was supposed to do it for me
Well, you said you wanted brian and sii, but in your screenshot, brian and sii have 55. But, now that i see, 55 is in the middle...
It's not about being in the middle, it's about printing the second last number so Brian and Sii are having second last number as you can see from that list. Here is the code : people = [] for _ in range(int(input())): people.append([input(), float(input())]) for i in people: x = max(people, key=lambda x: x[1]) if i[1] == x[1]: people.remove(i) for z in people: y = max(people, key=lambda x: x[1]) if z[1] == y[1]: print(z[0])
To post more than a few lines of code, use a pastebin like: * dpaste.org * linkode.org * bin.kv2.dev * hastebin.com If you are stuck in a terminal, you can use ix.io or paste.rs to paste from the CLI: <command to print output> |& curl -F 'f:1=<-' ix.io <command to print output> |& curl --data-binary @- https://paste.rs
Print the entire list
5 Brian 55 Sii 55 Terry 62 Tonny 22 Mom 22 Here is my input. Just input them in that order to create the list
Potential points of bugs: 1. Your uncommented code doesn't check for second minimum, but the absolute minimum. The commented code probably does check for the second. 2. You're recalculating x in each iteration, meaning it will change (the minimum will change) as you modify the list. 3. You're modifying the list inside its own iteration, i already said that's a big source of bugs. 4. You're iterating over your modified list, it will certainly be different from your original one, with less items than the ones you inputted.
Print it at the end too
Print what?
I wanted the second minimum from the original list
I wanted to do it like this: 1. Remove the minimum list from that original list 2. print the minimum list from the original list after 1 is successful I don't know why it did not work for me that way. So this is what i was trying
https://t.me/c/1111136772/184187
Tbf I don't understand what you're trying to do in either of the code snippets. If you give us an example with input/output we might provide more insights into a potential pythonic solution
Manipulating source list is usually not recommended. Find the item in the list that match whatever you want, and copy it into new list. With that you can verify that your filtering command is correct
Обсуждают сегодня