Re: absolute newbie: divide a list into sublists (nested lists?) of fixed length
On 11 Apr., 22:14, ergconce...@googlemail.com wrote: > Hi, > I have a list looking like > > [ 0.84971586, 0.05786009, 0.9645675, 0.84971586, 0.05786009, > 0.9645675, 0.84971586, 0.05786009, 0.9645675, 0.84971586, > 0.05786009, 0.9645675] > > and I would like to break this list into subsets of fixed length (say, > three elements), i.e. to convert the list into a form such as the one > generated by the following example code which I have found: > > >>>import numpy > >>>s = numpy.random.random((3,3)) > >>>s > > array([[ 0.11916176, 0.96409475, 0.72602155], > [ 0.84971586, 0.05786009, 0.96456754], > [ 0.81617437, 0.845342 , 0.09109779]]) > > How can I create such a 2d array (i.e., something like a symmetric > matrix) from my data? > > Thanks in advance, > > Bernard > > PS: Note that the numpy import is not important here, it is just the > structure of the data that matters.. Not sure if that's the best solution, but you could try: my_list = [] for x in range(3): my_list.append([]) for y in range(3): my_list[x].append(some_value) -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex similar to "^(?u)\w$", but without digits?
On 12 Apr., 02:31, "Mark Tolonen" wrote: > "Andreas" wrote in message > > news:f953c845-3660-4bb5-8ba7-00b93989c...@b1g2000vbc.googlegroups.com... > > > Hello, > > > I'd like to create a regex that captures any unicode character, but > > not the underscore and the digits 0-9. "^(?u)\w$" captures them also. > > Is there a possibility to restrict an expression like "\w" to "\w > > without [0-9_]"? > > '(?u)[^\W0-9_]' removes 0-9_ from \w. > > -Mark Hello Mark, haven't tried it yet, but it looks good! @John: Sorry for being imprecise, I meant *letters*, not *characters*, so requirement 2 fits my needs. Regards, Andreas -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex similar to "^(?u)\w$", but without digits?
On 12 Apr., 02:31, "Mark Tolonen" wrote: > "Andreas" wrote in message > > news:f953c845-3660-4bb5-8ba7-00b93989c...@b1g2000vbc.googlegroups.com... > > > Hello, > > > I'd like to create a regex that captures any unicode character, but > > not the underscore and the digits 0-9. "^(?u)\w$" captures them also. > > Is there a possibility to restrict an expression like "\w" to "\w > > without [0-9_]"? > > '(?u)[^\W0-9_]' removes 0-9_ from \w. > > -Mark Hello Mark, haven't tried it yet, but it looks good! @John: Sorry for being imprecise, I meant *letters*, not *characters*, so requirement 2 fits my needs. Regards, Andreas -- http://mail.python.org/mailman/listinfo/python-list
simple integer subclass
I'm trying to define a subclass of int called int1. An int1-object shall behave exactly like an int-object, with the only difference that the displayed value shall be value + 1 (it will be used to display array indices starting at 1 instead of 0). Right now I have: class int1(int): def __str__(self): return int.__str__(self + 1) However, if I calculate with int1 and int- (or other number) objects, the result is always coerced to an int (or other number object), e.g: a = int1(5) b = 5 print a # "6" print a+b #"10" How can I tell int1 to be the "default integer object"? Do I need to overload *every* mathematical operation method of int, or is there an easier way? -- http://mail.python.org/mailman/listinfo/python-list
Re: simple integer subclass
On 3 Aug., 03:22, Carl Banks wrote:> > You are creating an object that differs from a built-in, int, in a > highly misleading way that only makes sense in a very limited context, > and this object's modified behavior gives no clue that it's been > modified in such as way. (That is, it's not possible to tell if the > object's not a regular int just by looking at __str__()'s return > value.) To make matters worse, you want to program this object to > coerce other integers, so there's a risk of these objects escaping > from the context where they make sense. > > This is just a bad idea. The type is not the place to implement > behavior that makes sense only in a limited context. Instead, do > something like this: > > print "Item %d is %s." % (i+1, s[i]) I see your concerns. I started with the approach to add +1 directly before displaying the int. However, since there are some variables that shall be displayed normally and others that are indices I want to show starting at 1, I thought the easiest way would be to define a type that does the job, then I would only need to define it once and not take care everywhere whether I have a normal variable or a displayed index. Thinking about it, it might really be dangerous to coerce always to int1, since sometimes I might want a normal int as result (I can't tell yet for sure). I'm just thinking about only overloading the operations if the int1 is on the left-hand side (so __op__ coerces to int1, while __rop__ doesn't). This would make operations non-commutative - but I also would need to put more brains in every calculation, which could finally take more effort than only "upgrading" the display :-??? Seems I end up with your suggestion - if noone else has an idea ;-) The application will be a browsergame, and most gamers start counting at 1, so they would probably wonder about a "level 0 item" ;-) If there didn't already exist lots of code, I would redesign the whole data-structure - I think that's "lessons learned" for the next project -.- -- http://mail.python.org/mailman/listinfo/python-list
Re: simple integer subclass
Hello everyone, thanks for all the suggestions. I did effort to redesign parts of the data structure the last days, but not all (only those I could easily keep track of in my code). For the rest I add +1 before the presentation and comment it. Seems the easiest way now. Andreas -- http://mail.python.org/mailman/listinfo/python-list