Re: Unpacking sequences and keywords in one function call
ram wrote: > Stupid question #983098403: > > I can't seem to pass an unpacked sequence and keyword arguments to a > function at the same time. What am I doing wrong? > > def f(*args, **kw): > for a in args: > print 'arg:', a > for (k,v) in kw.iteritems(): > print k, '=', v > f(1,2) > arg: 1 > arg: 2 > f(*[1,2]) > arg: 1 > arg: 2 > f(1,2, a=1) > arg: 1 > arg: 2 > a = 1 > f(*[1,2], a=1) > File "", line 1 > f(*[1,2], a=1) > ^ > SyntaxError: invalid syntax > > Thanks, > Rick > I don't know if it's because there's some potential ambiguity (that I'm not seeing), but yeah, you just can't do that. This should work though... >>> f(*[1, 2], **{'a':1}) noah -- http://mail.python.org/mailman/listinfo/python-list
Re: re.match -- not greedy?
Carl Banks wrote: > EXI-Andrews, Jack wrote: >> the '*' and '+' don't seem to be greedy.. they will consume less in >> order to match a string: >> > import re;re.match('(a+)(ab)','aaab').groups() >> ('aa', 'ab') >> >> this is the sort of behaviour i'd expect from >>'(a+?)(ab)' >> >> a+ should greedily consume a's at the expense of the string not matching > > It's called backtracking. If a match fails, the regexp engine > recursively unwinds some of the greedy matching and tries again. > >> in real life, i am trying to match #defines: >> > re.match( '#define ([A-Za-z0-9_]+)([^(])', '#define >> abc(d)').groups() >> ('ab', 'c') >> >> i want this example to fail because the first character after a string >> of letters is a '(' >> i want to match only #defines without parameters. > > Probably the easiest thing to do is to attempt to match zero or one > opening parentheses, and bail out if it did end up matching the > parenthesis. > > m = re.match(r"#define ([A-Za-z0-9_]+)(\(?)","#define abc(d)") > if m and m.groups(2) != "(": > ... > > (BTW, it's good practice to use a raw strings for the regular > expressions as I did.) > Another way that seems clearer to me is to use negative lookahead assertions. >>>defPatt = re.compile(r'#define\s+(\w+)\b(?!\()') >>> defPatt.match("#define abc(x)") >>> defPatt.match("#define foo_BarBaz") <_sre.SRE_Match object at 0xb7dd5820> >>> defPatt.match("#define foo_BarBaz").groups() ('foo_BarBaz',) In general '\w' is the same as [A-Za-z0-9_] There are other considerations too... I don't know if #define abc (x) But the main thing here is the use of '\b' to require there to be a word boundary at the end your match and a (?!\() to require that the match is not followed by a '(' see http://docs.python.org/lib/re-syntax.html noah >> so what's the definition of greedy? > > It means match as much you can *while getting a match*. > > Just remember regexp parsing is not a one way street: if it hits a dead > end, it'll go back and try another path. In fact, greediness usually > doesn't affect *whether* a regexp matches; it only affects the > groupings. I'm suddenly curious if there are any cases at all where > greediness changes whether it finds a match. > >> (pls copy me on responses) > > Ah, too bad you won't see it then > > > Carl Banks > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is time.time() < time.time() always true?
Ben Finney wrote: > Really? Where does Python guarantee that the left side *must* be > evaluated before the right side of a comparison? (If the right side > were to be evaluated first, the left might end up with a greater > value.) > http://docs.python.org/ref/evalorder.html -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to align words?
Robert R. wrote: > Hello, > > i would like to write a piece of code to help me to align some sequence > of words and suggest me the ordered common subwords of them > > s0 = "this is an example of a thing i would like to have".split() > s1 = "another example of something else i would like to have".split() > s2 = 'and this is another " example " but of something ; now i would > still like to have'.split() > ... > alist = (s0, s1, s2) > > result should be : ('example', 'of', 'i', 'would', 'like', 'to', 'have' > > but i do not know how should i start, may be have you a helpful > suggestion? > a trouble i have if when having many different strings my results tend > to be nothing while i still would like to have one of the, or maybe, > all the best matches. > > best. > Your requirements are a little vague... how are these three strings handled? s1 = "hello there dudes" s2 = "dudes hello there" s3 = "there dudes hello" they all share the 3 words, but what order do you want them back? here is a simplistic approach using sets that results in a list of words that are in all strings ordered arbitrarily by their order in the first string ( it also doesn't worry about matches (or lack of) due to punctuation and case and crap like that) >>> strList = [] >>> strList.append('this is an example of a thing i would like to have') >>> strList.append('another example of something else i would like to have') >>> strList.append('and this is another " example " but of something ; now i would still like to have') >>> [word for word in strList[0].split() if word in reduce(lambda x, y: x.intersection(y), [set(str.split()) for str in strList])] ['example', 'of', 'i', 'would', 'like', 'to', 'have'] but you still have issues with mutiple matches and how they are handled etc... noah -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to align words?
Noah Rawlins wrote: > > >>> strList = [] > >>> strList.append('this is an example of a thing i would like to have') > >>> strList.append('another example of something else i would like to > have') > >>> strList.append('and this is another " example " but of something ; > now i would still like to have') > >>> [word for word in strList[0].split() if word in reduce(lambda x, y: > x.intersection(y), [set(str.split()) for str in strList])] > ['example', 'of', 'i', 'would', 'like', 'to', 'have'] > I think that ends up doing the set reduction over and over for every word in the first string, so you probably want to move that outside the list comprehension noah -- http://mail.python.org/mailman/listinfo/python-list