Welcome to code review. #Style Check
Style
Check PEP0008 the official Python style guide for maintaining code within Python acceptable standards.
#Code
Code
- Functions: A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Since this a bad use of classes because a class is usually used for an object with multi-attributes and multi-methods and this is not the case here, you might use a regular function.
- Augmented assignment: Python supports augmented assignments ex:
i=i-1
isi -= 1
,count=count+1
iscount += 1
... - Semi-columnsSemicolons: are for combining multiple short statements on the same line, this
j=0;
(line 15) is an invalid use of semi-columnssemicolons. - Comparison to True and False: is usually done using
if something:
andif not something_else:
ex:flagarray[i]==False:
isif not flag_array[i]:
. Non-empty sequences and non-zero variables evaluate toTrue
ex:if len(ca)>0:
isif ca:
- Inefficiency: If you need to print duplicates of a list you shouldn't be entering each of the list members manually (what if you have a list of size 10,000,000 items? will you be manually entering one by one?)
Counter()
You could use counter dict from thecollections
library for counting list members
- Functions: A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Since this a bad use of classes because a class is usually used for an object with multi-attributes and multi-methods and this is not the case here, you might use a regular function.
- Augmented assignment: Python supports augmented assignments ex:
i=i-1
isi -= 1
,count=count+1
iscount += 1
... - Semi-columns: are for combining multiple short statements on the same line, this
j=0;
(line 15) is an invalid use of semi-columns. - Comparison to True and False: is usually done using
if something:
andif not something_else:
ex:flagarray[i]==False:
isif not flag_array[i]:
. Non-empty sequences and non-zero variables evaluate toTrue
ex:if len(ca)>0:
isif ca:
- Inefficiency: If you need to print duplicates of a list you shouldn't be entering each of the list members manually (what if you have a list of size 10,000,000 items? will you be manually entering one by one?)
Counter()
You could use counter dict from thecollections
library for counting list members
- Functions: A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Since this a bad use of classes because a class is usually used for an object with multi-attributes and multi-methods and this is not the case here, you might use a regular function.
- Augmented assignment: Python supports augmented assignments ex:
i=i-1
isi -= 1
,count=count+1
iscount += 1
... - Semicolons: are for combining multiple short statements on the same line, this
j=0;
(line 15) is an invalid use of semicolons. - Comparison to True and False: is usually done using
if something:
andif not something_else:
ex:flagarray[i]==False:
isif not flag_array[i]:
. Non-empty sequences and non-zero variables evaluate toTrue
ex:if len(ca)>0:
isif ca:
- Inefficiency: If you need to print duplicates of a list you shouldn't be entering each of the list members manually (what if you have a list of size 10,000,000 items? will you be manually entering one by one?)
Counter()
You could use counter dict from thecollections
library for counting list members
from collections import Counter
def get_duplicates(numbers: list):
"""Return list duplicates."""
num_duplicates = Counter(numbers)
return {item for item in numbersnum_duplicates if Counter(numbers)[item]num_duplicates[item] > 1}
if __name__ == '__main__':
list_of_duplicates = [1, 2, 2, 3, 4, 3, 5, 2, 7, 1, ]
print(get_duplicates(list_of_duplicates))
from collections import Counter
def get_duplicates(numbers: list):
"""Return list duplicates."""
return {item for item in numbers if Counter(numbers)[item] > 1}
if __name__ == '__main__':
list_of_duplicates = [1, 2, 2, 3, 4, 3, 5, 2, 7, 1, ]
print(get_duplicates(list_of_duplicates))
from collections import Counter
def get_duplicates(numbers: list):
"""Return list duplicates."""
num_duplicates = Counter(numbers)
return {item for item in num_duplicates if num_duplicates[item] > 1}
if __name__ == '__main__':
list_of_duplicates = [1, 2, 2, 3, 4, 3, 5, 2, 7, 1, ]
print(get_duplicates(list_of_duplicates))
Loading
Loading
lang-py