How do you check if a list's item is equal to another item in the same list
up vote
1
down vote
favorite
I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).
Let's say we have a string:
grades=str(input("Enter a string"))
in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:
grades=" ".join(grades)
grades.split(" ")
I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:
x=len(grades)
for i in range(0, x):
if grades[i] == # here is were I'm having trouble
I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.
python python-3.x list
add a comment |
up vote
1
down vote
favorite
I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).
Let's say we have a string:
grades=str(input("Enter a string"))
in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:
grades=" ".join(grades)
grades.split(" ")
I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:
x=len(grades)
for i in range(0, x):
if grades[i] == # here is were I'm having trouble
I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.
python python-3.x list
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).
Let's say we have a string:
grades=str(input("Enter a string"))
in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:
grades=" ".join(grades)
grades.split(" ")
I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:
x=len(grades)
for i in range(0, x):
if grades[i] == # here is were I'm having trouble
I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.
python python-3.x list
I understand the basics of this problem however I need help on how I can do this the most efficient way possible (taking the least amount of time for the programmer however not substituting stability of the code or efficiency).
Let's say we have a string:
grades=str(input("Enter a string"))
in my code, I would join a space between all characters in the string above and then split the characters into separate items in the same list:
grades=" ".join(grades)
grades.split(" ")
I then want to use loops of some sort to search the list for repeating items. However, I want to learn how I can do this the most efficient way possible:
x=len(grades)
for i in range(0, x):
if grades[i] == # here is were I'm having trouble
I want to know how I can search whether 1 item in the list is equal to any item in the whole list itself. Kind regards.
python python-3.x list
python python-3.x list
edited Nov 10 at 10:56
mehrdad-pedramfar
4,02511234
4,02511234
asked Nov 10 at 10:47
Coder Cody
408
408
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
I make an example:
from collections import Counter
a =[1,2,3,4,1,2]
c = Counter(a)
for k,v in c.items():
if v>1:
print(k,'repeated more than once')
Here the c
will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1)
. the keys are the array values and values are the count of them.
So I write the for
for your understanding. You can do anything with c
, it acts like a dict
.
>> [k for k,v in c.items() if v>1]
[1, 2]
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
Nov 10 at 11:01
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
Nov 10 at 11:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I make an example:
from collections import Counter
a =[1,2,3,4,1,2]
c = Counter(a)
for k,v in c.items():
if v>1:
print(k,'repeated more than once')
Here the c
will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1)
. the keys are the array values and values are the count of them.
So I write the for
for your understanding. You can do anything with c
, it acts like a dict
.
>> [k for k,v in c.items() if v>1]
[1, 2]
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
Nov 10 at 11:01
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
Nov 10 at 11:02
add a comment |
up vote
4
down vote
accepted
I make an example:
from collections import Counter
a =[1,2,3,4,1,2]
c = Counter(a)
for k,v in c.items():
if v>1:
print(k,'repeated more than once')
Here the c
will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1)
. the keys are the array values and values are the count of them.
So I write the for
for your understanding. You can do anything with c
, it acts like a dict
.
>> [k for k,v in c.items() if v>1]
[1, 2]
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
Nov 10 at 11:01
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
Nov 10 at 11:02
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I make an example:
from collections import Counter
a =[1,2,3,4,1,2]
c = Counter(a)
for k,v in c.items():
if v>1:
print(k,'repeated more than once')
Here the c
will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1)
. the keys are the array values and values are the count of them.
So I write the for
for your understanding. You can do anything with c
, it acts like a dict
.
>> [k for k,v in c.items() if v>1]
[1, 2]
I make an example:
from collections import Counter
a =[1,2,3,4,1,2]
c = Counter(a)
for k,v in c.items():
if v>1:
print(k,'repeated more than once')
Here the c
will be a Counter object like this Counter(1: 2, 2: 2, 3: 1, 4: 1)
. the keys are the array values and values are the count of them.
So I write the for
for your understanding. You can do anything with c
, it acts like a dict
.
>> [k for k,v in c.items() if v>1]
[1, 2]
edited Nov 10 at 11:01
answered Nov 10 at 10:51
mehrdad-pedramfar
4,02511234
4,02511234
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
Nov 10 at 11:01
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
Nov 10 at 11:02
add a comment |
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
Nov 10 at 11:01
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
Nov 10 at 11:02
2
2
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
Thank you however once tested I found an error, it is much easier if you do not use both k and v in the 4th line of code. You can just use k or v, not both.
– Coder Cody
Nov 10 at 10:57
I get
TypeError: 'int' object is not iterable
for the line: for k,v in c:
.– quamrana
Nov 10 at 11:01
I get
TypeError: 'int' object is not iterable
for the line: for k,v in c:
.– quamrana
Nov 10 at 11:01
1
1
sorry I forgot
.items()
. it is fixed now.@quamrana– mehrdad-pedramfar
Nov 10 at 11:02
sorry I forgot
.items()
. it is fixed now.@quamrana– mehrdad-pedramfar
Nov 10 at 11:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238167%2fhow-do-you-check-if-a-lists-item-is-equal-to-another-item-in-the-same-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown