Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. def findnth(haystack, needle, n):
  2. parts= haystack.split(needle, n+1)
  3. if len(parts)<=n+1:
  4. return -1
  5. return len(haystack)-len(parts[-1])-len(needle)
  6.  
  7. 'foo bar bar bar'.replace('bar', 'XXX', 1).find('bar')
  8.  
  9. def find_nth(haystack, needle, n):
  10. start = haystack.find(needle)
  11. while start >= 0 and n > 1:
  12. start = haystack.find(needle, start+len(needle))
  13. n -= 1
  14. return start
  15.  
  16. >>> find_nth("foofoofoofoo", "foofoo", 2)
  17. 6
  18.  
  19. def find_nth_overlapping(haystack, needle, n):
  20. start = haystack.find(needle)
  21. while start >= 0 and n > 1:
  22. start = haystack.find(needle, start+1)
  23. n -= 1
  24. return start
  25.  
  26. >>> find_nth_overlapping("foofoofoofoo", "foofoo", 2)
  27. 3
  28.  
  29. def find_2nd(string, substring):
  30. return string.find(substring, string.find(substring) + 1)
  31.  
  32. >>> import re
  33. >>> s = "ababdfegtduab"
  34. >>> [m.start() for m in re.finditer(r"ab",s)]
  35. [0, 2, 11]
  36. >>> [m.start() for m in re.finditer(r"ab",s)][2] #index 2 is third occurrence
  37. 11
  38.  
  39. def findnth(haystack, needle, n):
  40. parts= haystack.split(needle, n+1)
  41. if len(parts)<=n+1:
  42. return -1
  43. return len(haystack)-len(parts[-1])-len(needle)
  44.  
  45. def find_nth(s, x, n=0, overlap=False):
  46. l = 1 if overlap else len(x)
  47. i = -l
  48. for c in xrange(n + 1):
  49. i = s.find(x, i + l)
  50. if i < 0:
  51. break
  52. return i
  53.  
  54. In [1]: import _find_nth, find_nth, mmap
  55.  
  56. In [2]: f = open('bigfile', 'r')
  57.  
  58. In [3]: mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
  59.  
  60. In [4]: %time s = mm[:]
  61. CPU times: user 813 ms, sys: 3.25 s, total: 4.06 s
  62. Wall time: 17.7 s
  63.  
  64. In [5]: %timeit find_nth.findnth(s, 'n', 1000000)
  65. 1 loops, best of 3: 29.9 s per loop
  66.  
  67. In [6]: %timeit find_nth.find_nth(s, 'n', 1000000)
  68. 1 loops, best of 3: 774 ms per loop
  69.  
  70. In [7]: %timeit find_nth.find_nth(mm, 'n', 1000000)
  71. 1 loops, best of 3: 1.21 s per loop
  72.  
  73. #include <Python.h>
  74. #include <string.h>
  75.  
  76. off_t _find_nth(const char *buf, size_t l, char c, int n) {
  77. off_t i;
  78. for (i = 0; i < l; ++i) {
  79. if (buf[i] == c && n-- == 0) {
  80. return i;
  81. }
  82. }
  83. return -1;
  84. }
  85.  
  86. off_t _find_nth2(const char *buf, size_t l, char c, int n) {
  87. const char *b = buf - 1;
  88. do {
  89. b = memchr(b + 1, c, l);
  90. if (!b) return -1;
  91. } while (n--);
  92. return b - buf;
  93. }
  94.  
  95. /* mmap_object is private in mmapmodule.c - replicate beginning here */
  96. typedef struct {
  97. PyObject_HEAD
  98. char *data;
  99. size_t size;
  100. } mmap_object;
  101.  
  102. typedef struct {
  103. const char *s;
  104. size_t l;
  105. char c;
  106. int n;
  107. } params;
  108.  
  109. int parse_args(PyObject *args, params *P) {
  110. PyObject *obj;
  111. const char *x;
  112.  
  113. if (!PyArg_ParseTuple(args, "Osi", &obj, &x, &P->n)) {
  114. return 1;
  115. }
  116. PyTypeObject *type = Py_TYPE(obj);
  117.  
  118. if (type == &PyString_Type) {
  119. P->s = PyString_AS_STRING(obj);
  120. P->l = PyString_GET_SIZE(obj);
  121. } else if (!strcmp(type->tp_name, "mmap.mmap")) {
  122. mmap_object *m_obj = (mmap_object*) obj;
  123. P->s = m_obj->data;
  124. P->l = m_obj->size;
  125. } else {
  126. PyErr_SetString(PyExc_TypeError, "Cannot obtain char * from argument 0");
  127. return 1;
  128. }
  129. P->c = x[0];
  130. return 0;
  131. }
  132.  
  133. static PyObject* py_find_nth(PyObject *self, PyObject *args) {
  134. params P;
  135. if (!parse_args(args, &P)) {
  136. return Py_BuildValue("i", _find_nth(P.s, P.l, P.c, P.n));
  137. } else {
  138. return NULL;
  139. }
  140. }
  141.  
  142. static PyObject* py_find_nth2(PyObject *self, PyObject *args) {
  143. params P;
  144. if (!parse_args(args, &P)) {
  145. return Py_BuildValue("i", _find_nth2(P.s, P.l, P.c, P.n));
  146. } else {
  147. return NULL;
  148. }
  149. }
  150.  
  151. static PyMethodDef methods[] = {
  152. {"find_nth", py_find_nth, METH_VARARGS, ""},
  153. {"find_nth2", py_find_nth2, METH_VARARGS, ""},
  154. {0}
  155. };
  156.  
  157. PyMODINIT_FUNC init_find_nth(void) {
  158. Py_InitModule("_find_nth", methods);
  159. }
  160.  
  161. from distutils.core import setup, Extension
  162. module = Extension('_find_nth', sources=['_find_nthmodule.c'])
  163. setup(ext_modules=[module])
  164.  
  165. In [8]: %timeit _find_nth.find_nth(mm, 'n', 1000000)
  166. 1 loops, best of 3: 218 ms per loop
  167.  
  168. In [9]: %timeit _find_nth.find_nth(s, 'n', 1000000)
  169. 1 loops, best of 3: 216 ms per loop
  170.  
  171. In [10]: %timeit _find_nth.find_nth2(mm, 'n', 1000000)
  172. 1 loops, best of 3: 307 ms per loop
  173.  
  174. In [11]: %timeit _find_nth.find_nth2(s, 'n', 1000000)
  175. 1 loops, best of 3: 304 ms per loop
  176.  
  177. def find_nth(s, x, n):
  178. i = -1
  179. for _ in range(n):
  180. i = s.find(x, i + len(x))
  181. if i == -1:
  182. break
  183. return i
  184.  
  185. print find_nth('bananabanana', 'an', 3)
  186.  
  187. def find_nth(s, x, n, i = 0):
  188. i = s.find(x, i)
  189. if n == 1 or i == -1:
  190. return i
  191. else:
  192. return find_nth(s, x, n - 1, i + len(x))
  193.  
  194. print find_nth('bananabanana', 'an', 3)
  195.  
  196. text = "This is a test from a test ok"
  197.  
  198. firstTest = text.find('test')
  199.  
  200. print text.find('test', firstTest + 1)
  201.  
  202. import itertools
  203. import re
  204.  
  205. def find_nth(haystack, needle, n = 1):
  206. """
  207. Find the starting index of the nth occurrence of ``needle`` in
  208. ``haystack``.
  209.  
  210. If ``needle`` is a ``str``, this will perform an exact substring
  211. match; if it is a ``RegexpObject``, this will perform a regex
  212. search.
  213.  
  214. If ``needle`` doesn't appear in ``haystack``, return ``-1``. If
  215. ``needle`` doesn't appear in ``haystack`` ``n`` times,
  216. return ``-1``.
  217.  
  218. Arguments
  219. ---------
  220. * ``needle`` the substring (or a ``RegexpObject``) to find
  221. * ``haystack`` is a ``str``
  222. * an ``int`` indicating which occurrence to find; defaults to ``1``
  223.  
  224. >>> find_nth("foo", "o", 1)
  225. 1
  226. >>> find_nth("foo", "o", 2)
  227. 2
  228. >>> find_nth("foo", "o", 3)
  229. -1
  230. >>> find_nth("foo", "b")
  231. -1
  232. >>> import re
  233. >>> either_o = re.compile("[oO]")
  234. >>> find_nth("foo", either_o, 1)
  235. 1
  236. >>> find_nth("FOO", either_o, 1)
  237. 1
  238. """
  239. if (hasattr(needle, 'finditer')):
  240. matches = needle.finditer(haystack)
  241. else:
  242. matches = re.finditer(re.escape(needle), haystack)
  243. start_here = itertools.dropwhile(lambda x: x[0] < n, enumerate(matches, 1))
  244. try:
  245. return next(start_here)[1].start()
  246. except StopIteration:
  247. return -1
  248.  
  249. from re import finditer
  250. from itertools import dropwhile
  251. needle='an'
  252. haystack='bananabanana'
  253. n=2
  254. next(dropwhile(lambda x: x[0]<n, enumerate(re.finditer(needle,haystack))))[1].start()
  255.  
  256. >>> s="abcdefabcdefababcdef"
  257. >>> j=0
  258. >>> for n,i in enumerate(s):
  259. ... if s[n:n+2] =="ab":
  260. ... print n,i
  261. ... j=j+1
  262. ... if j==2: print "2nd occurence at index position: ",n
  263. ...
  264. 0 a
  265. 6 a
  266. 2nd occurence at index position: 6
  267. 12 a
  268. 14 a
  269.  
  270. import re
  271. indices = [s.start() for s in re.finditer(':', yourstring)]
  272.  
  273. n = 2
  274. nth_entry = indices[n-1]
  275.  
  276. num_instances = len(indices)
  277.  
  278. def iter_find(haystack, needle):
  279. return [i for i in range(0, len(haystack)) if haystack[i:].startswith(needle)]
  280.  
  281. >>> iter_find("http://stackoverflow.com/questions/1883980/", '/')
  282. [5, 6, 24, 34, 42]
  283.  
  284. def findN(s,sub,N,replaceString="XXX"):
  285. return s.replace(sub,replaceString,N-1).find(sub) - (len(replaceString)-len(sub))*(N-1)
  286.  
  287. len("substring".join([s for s in ori.split("substring")[:2]]))
  288.  
  289. c = os.getcwd().split('\')
  290. print '\'.join(c[0:-2])
  291.  
  292. def Find(String,ToFind,Occurence = 1):
  293. index = 0
  294. count = 0
  295. while index <= len(String):
  296. try:
  297. if String[index:index + len(ToFind)] == ToFind:
  298. count += 1
  299. if count == Occurence:
  300. return index
  301. break
  302. index += 1
  303. except IndexError:
  304. return False
  305. break
  306. return False
  307.  
  308. # return -1 if nth substr (0-indexed) d.n.e, else return index
  309. def find_nth(s, substr, n):
  310. i = 0
  311. while n >= 0:
  312. n -= 1
  313. i = s.find(substr, i + 1)
  314. return i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement