Finding occurrences of a word in a string in python 3

If you’re going for efficiency: import re count = sum(1 for _ in re.finditer(r’\b%s\b’ % re.escape(word), input_string)) This doesn’t need to create any intermediate lists (unlike split()) and thus will work efficiently for large input_string values. It also has the benefit of working correctly with punctuation – it will properly return 1 as the count … Read more

Why won’t re.groups() give me anything for my one correctly-matched group?

To the best of my knowledge, .groups() returns a tuple of remembered groups. I.e. those groups in the regular expression that are enclosed in parentheses. So if you were to write: print re.search(r'(1)’, ‘1’).groups() you would get (‘1’,) as your response. In general, .groups() will return a tuple of all the groups of objects in … Read more

Pattern matching field names with jq

Your statement does not work, because you try to feed the data object into match, but match can only work on strings. The following expression will do what you want. The to_entries converts the object to an array of keys and values. Then we iterate over this array by using map and select all entries … Read more

Is there a way to check if two object contain the same values in each of their variables in python?

If you want the == to work, then implement the __eq__ method in your class to perform the rich comparison. If all you want to do is compare the equality of all attributes, you can do that succinctly by comparison of __dict__ in each object: class MyClass: def __eq__(self, other) : return self.__dict__ == other.__dict__

ElasticSearch: How to search for a value in any field, across all types, in one or more indices?

Either the query_string query or the match query would be what you’re looking for. query_string will use the special _all field if none is specified in default_field, so that would work out well. curl -XPOST ‘localhost:9200/_search?pretty’ -d ‘{ “query”: { “query_string”: { “query”: “abc” } } }’ And with match you can just specify the … Read more

tech