Похожие чаты

People = [['See', 22], ['Aerry', 35], ['Tonny', 55], ['Nurdin', 22]] print(people) x

= 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

28 ответов

18 просмотров

You have an infinite loop in that while. Why do you have it, if you're already iterating with the outer for loop?

Bruce-Minanga Автор вопроса
Link 🪈
You have an infinite loop in that while. Why do yo...

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]

Bruce Minanga
I don't have any idea how i will use it without fo...

I meant the while loop, it is unnecessary, what you need is an if statement, not a while

Bruce-Minanga Автор вопроса
Link 🪈
I meant the while loop, it is unnecessary, what yo...

Hehe, so i can't use while loop inside for loop

Bruce Minanga
Hehe, so i can't use while loop inside for loop

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

Bruce-Minanga Автор вопроса
Link 🪈
You have an infinite loop in that while. Why do yo...

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

Bruce-Minanga Автор вопроса
Bruce-Minanga Автор вопроса
Link 🪈
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

Bruce Minanga
I don't even understand why i should use max and n...

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...

Bruce-Minanga Автор вопроса
Link 🪈
Well, you said you wanted brian and sii, but in yo...

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])

Bruce Minanga
It's not about being in the middle, it's about pri...

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

Bruce-Minanga Автор вопроса
Link 🪈
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

Bruce Minanga
screenshot I don't know why if i use the input function, i do...

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.

Bruce-Minanga Автор вопроса
Bruce-Minanga Автор вопроса
Link 🪈
screenshot Potential points of bugs: 1. Your uncommented code...

I wanted the second minimum from the original list

Bruce-Minanga Автор вопроса
Link 🪈
Refer to this

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

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

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта