The Student Room Group

Can someone pleasee explain this python code??

So I need to explain how this code works and what each part does but I'm soo baffed. Help Please?
Here's the code:
Sentence = input("Enter a sentence!")
s = Sentence.split()
another = [0]

for count, i in enumerate(s):
if s.count(i) < 2:
another.append(max(another) + 1)
else:
another.append(s.index(i) +1)

another.remove(0)

print(another)
Have you actually played with the code? If you don't have an IDE installed you could play with it somewhere like here https://repl.it/languages/python3

Let me know if you still get stuck on anything in particular :smile:
(edited 7 years ago)
Which parts of it do you understand, and which are you struggling with? :smile:
Original post by DD2508
So I need to explain how this code works and what each part does but I'm soo baffed. Help Please?
Here's the code:
Sentence = input("Enter a sentence!":wink:
s = Sentence.split()
another = [0]

for count, i in enumerate(s):
if s.count(i) < 2:
another.append(max(another) + 1)
else:
another.append(s.index(i) +1)

another.remove(0)

print(another)


What bit do you specifically not understand?
(edited 7 years ago)
Reply 4
I don't understand the bits with the hashtag:

Sentence = input("Enter a sentence!")
s = Sentence, split()
another = [0]#

for count, i in enumerate(s):colonhash:
if s.count(i) < 2:colonhash:
another.append(max(another) + 1) #
else:colonhash:
another.append(s.index(
Well [ ] designates a list, i.e. [foo, bar]. Does that point you in the right direction?
Original post by DD2508
I don't understand the bits with the hashtag:

Sentence = input("Enter a sentence!":wink:
s = Sentence, split()
another = [0]#

for count, i in enumerate(s):colonhash:
if s.count(i) < 2:colonhash:
another.append(max(another) + 1) #
else:colonhash:
another.append(s.index(


Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

Therefore, within the beginning of the for loop, it assigns count to the index of each item with the list, with i with the matching contents.

With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.
(edited 7 years ago)
Reply 7
Original post by _gcx
Enumerate splits a list into two iterables the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

With the second hastag, it checks the number of times i is feature in the iterable is less than two. Else catches it if this is not the case.


You really need to learn how to use proper programming terminology when answering questions.
Original post by Aklaol
You really need to learn how to use proper programming terminology when answering questions.


How constructive :h:

Spoiler

Reply 9
Original post by _gcx
How constructive :h:

Spoiler



Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.

1 - No it doesn't "split" an iterable, it will only return the index of the iterable along with the content of the iterable.

2 - It doesn't "query", the for loop will simply assign the iterable (count) as a index to each element in the iterable (i).

3 - It checks how many times a object (in this case i) has occured in a list (in this case s), and if the total is less than 2, then the program will append to the list (another) with the maximum element in the list + 1.

4 - If the above condition has returned false, then the program will append to the list (another) with the index(taking the ordinal argument of the iterable i) + 1 of the variable s. The program will then remove the first element in the another list, and finally print the another list.

Either learn how to answer questions properly, or don't answer them at all. You don't need to pretend to understand programming in order to make yourself relevant.
Original post by Aklaol
Enumerate splits an iterable into two: the index of the list (0,1,2,3,4) and the content, which are defined separated by commas.

Therefore, within the beginning of the for loop, it queries (count, i) respectively, assigning count to the index of each item with the list, with i with the matching contents.

With the second hastag, it checks if the number of times i is featured in the iterable is less than two. Else catches it if this is not the case.

1 - No it doesn't "split" an iterable, it will only return the index of the iterable along with the content of the iterable.

2 - It doesn't "query", the for loop will simply assign the iterable (count) as a index to each element in the iterable (i).

3 - It checks how many times a object (in this case i) has occured in a list (in this case s), and if the total is less than 2, then the program will append to the list (another) with the maximum element in the list + 1.

4 - If the above condition has returned false, then the program will append to the list (another) with the index(taking the ordinal argument of the iterable i) + 1 of the variable s. The program will then remove the first element in the another list, and finally print the another list.

Either learn how to answer questions properly, or don't answer them at all. You don't need to pretend to understand programming in order to make yourself relevant.

1.

That is arguable, if you treat the iterable as [0:blah, 1:blah], then in layman's terms it could be considered splitting, using the definition of "separating into two or more parts" Note that I am not talking about .split here, which is clearly something completely different. (I don't even know how you thought I meant that?)

2.

Point taken, that was some poor (wrong) wording on my part.

3.

That isn't a mistake, that's me not blatantly giving the OP the answer. (to questions they didn't ask, should I add) They were querying the two lines indicated with a #, and were not asking for an explanation of the contents of the loop. Perhaps I should have been more clear.

4.

Above

Bold: wow that's rough :h: :teehee: No hard feelings, really. I'm not sure if you feel the same, as evidenced from your unjustified bluntness.
(edited 7 years ago)
Reply 11
Original post by _gcx

1.

That is arguable, if you treat the iterable as [0:blah, 1:blah], then in layman's terms it could be considered splitting. Note that I am not talking about .split here.

2.

Point taken, that was some poor (wrong) wording on my part.

3.

That isn't a mistake, that's me not blatantly giving the OP the answer. They were querying the two lines indicated with a #, and were not asking for an explanation of the contents of the loop. Perhaps I should have been more clear.

4.

Above

Bold: wow that's rough :h: :teehee: No hard feelings, really. I'm not sure if you feel the same, as evidenced from your unjustified bluntness.




1. It's not splitting, you're beyond delusional if you'd ever consider it to be splitting.

3 and 4. Your answers were blunt, and lacked key terminology. I also suggest that you look up the definition of querying in respects to computer science.

The actual problem with you is that you use wrong terminology a lot, maybe you should educate yourself before helping others.
Original post by Aklaol
1. It's not splitting, you're beyond delusional if you'd ever consider it to be splitting.

3 and 4. Your answers were blunt, and lacked key terminology. I also suggest that you look up the definition of querying in respects to computer science.

The actual problem with you is that you use wrong terminology a lot, maybe you should educate yourself before helping others.


There was little more to explain, and I doubt the OP required much more explanation.

Your points are valid, but you really don't have to use a condescending tone over a few mistakes which don't (with the exception of query, which was a genuine mistake) significantly impact meaning. I am quite receptive to criticism regardless of the way it is comunicated, but others won't be when you speak to them in that tone. Just a bit of advice :h:

There's no nee to continue this discussion further, if you feel the need to, continue it via PM.

Quick Reply

Latest