Posts: 5
Threads: 1
Joined: Jan 2017
Hello everyone, I am new to python and also new here. I have written a code to check if an duplicate exits in a list but it gives an error. Can anyone help me with what I am doing wrong.
# Changable list downwards
itemlist=['boy','cat','dog','man','man','dog','dog']
# Changable list upwards
from time import sleep
streak=1
tcvar=0
error=0
length=len(itemlist)
while length>0:
test=itemlist[0]
listsize=length-1
tcvar=1
while not tcvar==length:
testx=itemlist[tcvar]
if testx==test:
print('Match found-{}-in value{}'.format(test,tcvar+streak))
del itemlist[tcvar+streak-1]
sleep(1.5)
error=1
tcvar+=1
del itemlist[0]
length=len(itemlist)
streak+=1
if error==1:
print('Process finished with errors')
else:
print('Process finished without errors')
[size=x-large] [/size]
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jan-31-2017, 02:24 PM
(This post was last modified: Jan-31-2017, 02:25 PM by metulburr.)
Your code looks really over-complicated.
you can use set to remove duplicates
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> set(itemlist)
set(['boy', 'man', 'dog', 'cat'])
>>> list(set(itemlist))
['boy', 'man', 'dog', 'cat'] If your trying to actually obtain which are duplicates
>>> import collections
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> [item for item, count in collections.Counter(itemlist).items() if count > 1]
['man', 'dog'] or grab the number of dups
>>> import collection
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> collections.Counter(itemlist).items()
[('boy', 1), ('man', 2), ('dog', 3), ('cat', 1)]
Recommended Tutorials:
Posts: 5
Threads: 1
Joined: Jan 2017
Jan-31-2017, 02:56 PM
(This post was last modified: Jan-31-2017, 02:56 PM by Server94.)
(Jan-31-2017, 02:24 PM)metulburr Wrote: Your code looks really over-complicated.
you can use set to remove duplicates
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> set(itemlist)
set(['boy', 'man', 'dog', 'cat'])
>>> list(set(itemlist))
['boy', 'man', 'dog', 'cat'] If your trying to actually obtain which are duplicates
>>> import collections
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> [item for item, count in collections.Counter(itemlist).items() if count > 1]
['man', 'dog'] or grab the number of dups
>>> import collection
>>> itemlist=['boy','cat','dog','man','man','dog','dog']
>>> collections.Counter(itemlist).items()
[('boy', 1), ('man', 2), ('dog', 3), ('cat', 1)] Thanks, these modules are completely new to me as I have just started learning. Though I am quite confused why my code fails.
It fails on qpython3 for android.
The faulty line is del itemlist[tcver+streak-1] It gives an out of index error.
Yes I am removing the duplicates from the list so that it does not calculates 'dog' three times.
Though your code solves my problem it would be better if I know where I am going wrong.
Thanks
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jan-31-2017, 03:07 PM
(This post was last modified: Jan-31-2017, 03:07 PM by metulburr.)
Quote:tcver+streak-1
then whatever index this is does not exist at the time of the traceback
Quote:Yes I am removing the duplicates from the list so that it does not calculates 'dog' three times.
Though your code solves my problem it would be better if I know where I am going wrong.
No offense, but your program is the reason why there are built-ins such as set(). Its meant for tedious repetitive tasks. And your code is quite all over the place and hard to read. And if its hard to read, then it is easy for bugs to fit in.
Even if you wanted to remove duplicates manually, there is a much easier to read method such as using "in" operator.
def remove_duplicates(seq):
newlist = []
for elem in seq:
if elem not in newlist:
newlist.append(elem)
return newlist
itemlist=['boy','cat','dog','man','man','dog','dog']
print(remove_duplicates(itemlist)) Output: ['boy', 'cat', 'dog', 'man']
Recommended Tutorials:
Posts: 5
Threads: 1
Joined: Jan 2017
(Jan-31-2017, 03:03 PM)metulburr Wrote: Even if you wanted to remove duplicates manually, there is a much easier to read method such as using "in" operator.
def remove_duplicates(numbers):
newlist = []
for number in numbers:
if number not in newlist:
newlist.append(number)
return newlist
print(remove_duplicates([1,2,3,4,5,5,5,6,3,2, 'tup','tup'])) Output: [1, 2, 3, 4, 5, 6, 'tup']
Thanks, seems like a much better fit for what I want to achive.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Well, if you want to remove the duplicated items from a list, in addition to set() you can use the list's methods.
In [1]: l = ['boy','cat','dog','man','man','dog','dog']
In [2]: for item in l:
...: if l.count(item) > 1:
...: l.remove(item)
...: print(l)
...:
['boy', 'cat', 'man', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog']
Posts: 5
Threads: 1
Joined: Jan 2017
(Jan-31-2017, 03:19 PM)wavic Wrote: Well, if you want to remove the duplicated items from a list, in addition to set() you can use the list's methods.
In [1]: l = ['boy','cat','dog','man','man','dog','dog']
In [2]: for item in l:
...: if l.count(item) > 1:
...: l.remove(item)
...: print(l)
...:
['boy', 'cat', 'man', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog', 'dog']
['boy', 'cat', 'man', 'dog']
Thanks wavic for another great method. Now I'm confused which one to use.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Doesn't matter if the list is small. If it's not well, don't know which is faster
More important is to learn the basics. The data types in Python and their methods, the loops, functions and classes... A few more thighs.
Posts: 12,031
Threads: 485
Joined: Sep 2016
In my humble opinion, the set method is the best.
Posts: 5
Threads: 1
Joined: Jan 2017
Jan-31-2017, 04:56 PM
(This post was last modified: Jan-31-2017, 04:56 PM by Server94.)
(Jan-31-2017, 03:45 PM)wavic Wrote: Doesn't matter if the list is small. If it's not well, don't know which is faster
More important is to learn the basics. The data types in Python and their methods, the loops, functions and classes... A few more thighs. Thanks, I'm still learning, even not from a teacher, just by online tutorials and some books. Hope I would have better understanding someday.
(Jan-31-2017, 03:47 PM)Larz60+ Wrote: In my humble opinion, the set method is the best. That is the technique I'm using currently alongside with the one with 'in' check. Thanks for your opinion.
|