How to compare list in Python with some class using imlicator in?

I would like to compare list which contain some class using implicator in. Code below:

class Word: def __init__(self, p_name): = p_name def __eq__(self, other): return == def __str__(self): return "_name_: " + t1 = [] t1.append(Word("John")) t2 = [] t2.append(Word("John")) if t1 in t2: print("the same")

I got an error that "list has no attribute 'name'". I know that I can write some loops but I would like to use implicator in if it is possible in such situation.



if t1 in t2:
print("the same")

Should be one of these:

# Check if a single word is in t2.
w = Word("John")
if w in t2:
print("the same")

# Check if any element of t1 is in t2.
if any(w in t2 for w in t1):
print("the same")

# Check if all elements of t1 are in t2.
if all(w in t2 for w in t1):
print("the same")

You should not be checking if one list is in another list. You either want to check if a particular item is in a list, or you want to compare all of the items in t1 with the items in t2.

def __eq__(self, other):
return isinstance(other, Word) and ==

It would also be a good idea to add a check to __eq__ that other is a Word. Your code blows up because other is a list not a Word, and so the lookup fails.

Category:python Time:2017-12-05 Views:1

Related post

  • How to not replace when preceded with some characters using String's replaceAll 2012-03-21

    I need to replace some words in a text, but I need to put conditions in the replacement strategy as follows: I want to replace word1 with word2: String word1 = "word1"; String word2 = "word2"; but I don't want to replace word1 if it's preceded by wor

  • C++: Comparing list of doubles with some invalid values (QNAN) 2011-01-10

    i need to compare two std::list < double >, but some doubles may be invalid numbers (QNAN). If any invalid numbers are list entries the compare process won't work, because a comparison of the same invalid value will always result in 'false'. Wh

  • how to compare two IP belongs to same Class in MFC(VC++)? 2010-11-22

    how to compare two IP belongs to same Class(CLASS A,B,C,D) in MFC(VC++) ? I have one IP as and other one as now i can compare whether they lie in same class or Not ? Any code is highly appreciated. Thanks --------------Solu

  • How To Instantiate a java.util.ArrayList with Generic Class Using Reflection 2011-01-27

    How To Instantiate a java.util.ArrayList with Generic Class Using Reflection? I am writing a method that sets java.util.List on target object. A target object and a generic type of list is knowing in runtime: public static void initializeList(Object

  • how to get list of port which are in use at server 2009-09-17

    how to get list of ports which are in use at server --------------Solutions------------- Open up a command prompt then type... netstat -a There are a lot of options and tools. If you just want a list of listening ports and their owner processes try.

  • QuickGraph - How can I associate an Edge with a Class? (i.e. like you can with a Vertex) 2010-04-27

    Q1 - How can I associate an Edge with a Class? (i.e. like you can with a Vertex) In my case there are various types of edges I want to be able to model. So my real question I guess is how can I associate some level of data with Edges (e.g. edge type)

  • Click event for a list of buttons with same class name 2012-04-18

    Can we write a click event in jquery for a list of buttons with same class name? The html part is something like this <a id="c1768" class="MyButton button " href="#" style="" name="c1768">Add to cart</a> <a id="c1769" class="MyButton b

  • How can I open a file with extension .odg using Vista? 2012-09-09

    How can I open a file with extension .odg using Vista? --------------Solutions------------- Hi Defiant503, Thanks for visiting forums. I did some research and a file ending in .odg is a OpenOffice/StarOffice OpenDocument (Ver 2)

  • How to convert List of Object to XML doc using XStream 2010-11-02

    How to convert List of Object to XML doc using XStream ? and how to deserialize it back ? This is my xml <?xml version="1.0" encoding="UTF-8"?> <persons> <person> <fullname>Guilherme</fullname> <age>10</age>

  • Select first and last element with particular class using jQuery 2010-11-02

    I have a list of spans with particular class "place" and some of them have class "activated". Is there a way to select the first item with class "activated" and the last? <span class="place" onclick="activate();">1</span> <span class="

  • how to compare list with string 2013-06-24

    This question is an exact duplicate of: How to compare string with arraylist 1 answer im comparing database values with packageinfo this is screenshot i get all database values in list List arraylist = db.getAllApps(); how do

  • Flatten list of lists in Python with non-iterable types? 2010-01-28

    Yes, I know this subject has been covered before (here, here, here, here), but as far as I know, all solutions, except for one, fail on a list like this: L = [[[1, 2, 3], [4, 5]], 6] Where the desired output is [1, 2, 3, 4, 5, 6] Or perhaps even bett

  • How to compare list of X to list of Y in C# by using generics? 2010-07-24

    I have 2 classes, X and Y. Both classes have same similar property like below. class X { public string T1 { get; set; } public string T2 { get; set; } public string T3 { get; set; } } class Y { public string T1 { get; set; } public string T2 { get; s

  • Comparing lists in Python 2010-12-07

    Say I have list1 = [1,2,3,4] and list2 = [5,6,7,8]. How would I compare the first element, 1, in list1 with the first element, 5, in list2? And 2 with 6, 3 with 7, and so on. I'm trying to use a for loop for this, but I'm not sure how to do it. I und

  • How to compare size_t and pid_t with int 2009-11-21

    What is the right way to use logical operators with size_t and pid_t types? I mean: is legal to write something like the following? e.g.: size_t sz; /* some kind of assignment */ if(sz > 0){ /* do something */ } e.g.: void f(pid_t pid,...){ if(pid

  • Eclipse Display view and checking if list contains object with some property 2011-08-04

    i have Class ComplexObject { Complex() {} public String first; public String second; } List<ComplexObject> allObjects = fillListWithSomeData(); Is there some way in Display view to check if allObjects list contains object with field first set t

  • Padding a list in python with particular value 2011-08-11

    Possible Duplicate: Some built-in to pad a list in python I have a method that will return a list (instance variable) with 4 elements. Another method is used to assign values to the list. But at the moment I can't guarantee that the list has 4 elemen

  • How to compare dates in Python? 2011-10-24

    I'm a Python beginner. I need to see if a date has more than X days. How can I do this in Python? I have tested something like: if, 1, 12) > datetime.timedelta(3): I got the error: TypeError: can't compare to datet

  • How to compare c++ std::map with custom comparison function for associated data? 2012-03-29

    std::map<String, double> m1,m2; m1["A"] = 20; m2["A"] = 20.01; if (m1 == m2) cout << "True"; else cout << "False"; The sample code prints False because 20 is not equal to 20.1. However in my application I want to treat these value a

Copyright (C), All Rights Reserved.

processed in 0.597 (s). 13 q(s)