Skip to content
Advertisement

Everything is str, but still get this error (python) unsupported operand type(s) for &: ‘str’ and ‘int’

JavaScript

There is no string in connection_issue :(. And the code works fine for tablet i.e., if I just changed the .str.contains(*connection_issue) back to .isin(connection_issue), the rest, including .str.contains(*tablet), runs perfectly fine.

Advertisement

Answer

@matchifang has the right explanation as to why!

If you want to add more tags in the future based on different keywords, it would be good to have a more dynamic way of checking for tags, I recommend the following solution:

JavaScript

In this solution, we’re making the “later” tags in the dict a higher priority, and we’re using an OrderedDict to guarantee that the order is respected (because regular dictionaries in Python don’t guarantee order). We’re creating the dict from a list of tuples because if we created it from another dict (and using a Python version <3.6) Python would first create the unordered dict so it couldn’t guarantee order either.

Then, we’re iterating over all the tag/keyword combinations, and looking for instances of any of the tags (which we’re putting together in the format @matchifang mentioned), but by adding case=False we’re making the search case-independent so both uppercase and lowercase values will match.

Advertisement