finding similar objects in a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: finding similar objects in a list (/thread-11093.html) |
finding similar objects in a list - DRPR - Jun-22-2018 I am new to python and programming in general.I wanted to write a code which prints the similar elements from 2 lists .For example if a=[1,2,3,4,5] and b=[0,2,4,6] , I would want a c=[2,4]. Here is what I did: def simi(a,b): a,b,c=[],[],[] for i in a: for j in b: if i==j: c.append(i) else: continue return (c)[python] RE: finding similar objects in a list - buran - Jun-22-2018 check your line#2 Do you really want to overwrite the user supplied arguments a and b with empty lists? :-) Note that your code is not pythonic and there are better ways to do the same, without using nested loops. check using set
RE: finding similar objects in a list - volcano63 - Jun-22-2018 (Jun-22-2018, 06:57 AM)DRPR Wrote: I am new to python and programming in general.I wanted to write a code which prints the similar elements from 2 lists .For example if a=[1,2,3,4,5] and b=[0,2,4,6] , I would want a c=[2,4]. Here is what I did:
So this is how your function should look like def similar_elements(list1, list2): similars = [] for elem in list1: if elem in list2: similars.append(elem) return similarsOf course, experienced Pythonista would write it at least as [elem for elem in list1 if elem in list2]and there are couple of other ways to write it - but that will come in the future. Good luck with your studies! RE: finding similar objects in a list - DRPR - Jun-22-2018 (Jun-22-2018, 07:29 AM)volcano63 Wrote: [quote='DRPR' pid='50443' dateline='1529650625'] I am new to python and programming in general.I wanted to write a code which prints the similar elements from 2 lists .For example if a=[1,2,3,4,5] and b=[0,2,4,6] , I would want a c=[2,4]. Of course, experienced Pythonista would write it at least as[elem for elem in list1 if elem in list2]and there are couple of other ways to write it - but that will come in the future. Good luck with your studies! I did the method for the experienced pythonista! but it returned a an error: def simi1(list1,list2): c=[] [elem for elem in list1 if elem in list2] c.append(elem) return(c) Any tips?
RE: finding similar objects in a list - volcano63 - Jun-22-2018 (Jun-22-2018, 09:55 AM)DRPR Wrote: I did the method for the experienced pythonista! but it returned a an error: I probably should not have shown it (if I hadn't, someone had probably "corrected" me ). I have committed a sin I often berate others for - showing an advanced technique to a newbie without a disclaimer. This is the proper code def simi1(list1,list2): return [elem for elem in list1 if elem in list2]or - by set , as buran suggesteddef simi1(list1,list2): return set(list1).intersection(list2)The problem with those 2 solutions for you - jumping stages. Mastering loops is a step to mastering list comprehension. |