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 yesterday
mehrdad-pedramfar
3,32511232
3,32511232
asked yesterday
Jamie O connor
306
306
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.
– Jamie O connor
yesterday
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
yesterday
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
yesterday
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.
– Jamie O connor
yesterday
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
yesterday
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
yesterday
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.
– Jamie O connor
yesterday
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
yesterday
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
yesterday
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 yesterday
answered yesterday
mehrdad-pedramfar
3,32511232
3,32511232
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.
– Jamie O connor
yesterday
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
yesterday
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
yesterday
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.
– Jamie O connor
yesterday
I getTypeError: 'int' object is not iterable
for the line:for k,v in c:
.
– quamrana
yesterday
1
sorry I forgot.items()
. it is fixed now.@quamrana
– mehrdad-pedramfar
yesterday
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.
– Jamie O connor
yesterday
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.
– Jamie O connor
yesterday
I get
TypeError: 'int' object is not iterable
for the line: for k,v in c:
.– quamrana
yesterday
I get
TypeError: 'int' object is not iterable
for the line: for k,v in c:
.– quamrana
yesterday
1
1
sorry I forgot
.items()
. it is fixed now.@quamrana– mehrdad-pedramfar
yesterday
sorry I forgot
.items()
. it is fixed now.@quamrana– mehrdad-pedramfar
yesterday
add a comment |
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
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
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
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
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