Parsing a tweet to extract hashtags into an array

A simple regex should do the job: >>> import re >>> s = “I love #stackoverflow because #people are very #helpful!” >>> re.findall(r”#(\w+)”, s) [‘stackoverflow’, ‘people’, ‘helpful’] Note though, that as suggested in other answers, this may also find non-hashtags, such as a hash location in a URL: >>> re.findall(r”#(\w+)”, “http://example.org/#comments”) [‘comments’] So another simple … Read more

Does Matlab accept non-integer indices?

Additional observations: x(1.2:3) should theoretically be interpreted as: subsref(x, substruct(‘()’,1.2:3)). However, as mentioned in the question, “No rounding takes place when the indexing array is a standard array“, which causes the explicit subscripted reference to fail. This suggests that a mechanism similar to logical short-circuiting or perhaps multithreaded partitioning (where intermediate variable are “not really … Read more

C++ vector of arrays

Unfortunately, std::array does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever — this “feature” is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard. So why doesn’t built-in brace initialization work … Read more

Select N evenly spaced out elements in array, including first and last

To get a list of evenly spaced indices, use np.linspace: idx = np.round(np.linspace(0, len(arr) – 1, numElems)).astype(int) Next, index back into arr to get the corresponding values: arr[idx] Always use rounding before casting to integers. Internally, linspace calls astype when the dtype argument is provided. Therefore, this method is NOT equivalent to: # this simply … Read more