Re: side by side python
On Mar 21, 9:31 am, Robert wrote: > Can I install Python 2.7 and 3.2 (from python.org) side by side on OSX > without them stepping all over each other? Yes, sure! Look for "python environment" http://pypi.python.org/pypi/virtualenv Regards -- http://mail.python.org/mailman/listinfo/python-list
Re: Get keys from a dicionary
Hi
Sorry ! My mistake.
>>> myDict = {}
>>> myDict['foo'] = {}
>>> myDict['foo']['bar'] = 'works'
-
>>> def myFunction( MyObj ):
... # MyObj is a nested dicionary (normaly 2 steps like myDict['foo']
['bar'])
... # I want inspect this MyObj
... # what keys was pass
... print MyObj.keys() ## WRONG
... # So What I want is :
... # return foo bar
>>> result = myFunction( myDict['foo']['bar'] )
>>> result
Should print :
... foo bar
Best Regards
macm
On Nov 11, 2:09 pm, Jon Clements wrote:
> On Nov 11, 1:31 pm, macm wrote:
>
> > Hi Folks
>
> > I pass a nested dictionary to a function.
>
> > def Dicty( dict[k1][k2] ):
> > print k1
> > print k2
>
> > There is a fast way (trick) to get k1 and k2 as string.
>
> > Whithout loop all dict. Just it!
>
> > Regards
>
> > macm
>
> I've tried to understand this, but can't tell if it's a question or
> statement, and even then can't tell what the question or statement
> is...
>
> Care to eloborate?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Get keys from a dicionary
Ok Sorry!!
Sorry the noise!!
def func(object):
print "%s" % object
Regards
On Nov 11, 2:33 pm, macm wrote:
> Hi
>
> Sorry ! My mistake.
>
> >>> myDict = {}
> >>> myDict['foo'] = {}
> >>> myDict['foo']['bar'] = 'works'
>
> -
>
> >>> def myFunction( MyObj ):
>
> ... # MyObj is a nested dicionary (normaly 2 steps like myDict['foo']
> ['bar'])
> ... # I want inspect this MyObj
> ... # what keys was pass
> ... print MyObj.keys() ## WRONG
> ... # So What I want is :
> ... # return foo bar
>
>
>
> >>> result = myFunction( myDict['foo']['bar'] )
> >>> result
>
> Should print :
>
> ... foo bar
>
> Best Regards
>
> macm
>
> On Nov 11, 2:09 pm, Jon Clements wrote:
>
>
>
>
>
>
>
> > On Nov 11, 1:31 pm, macm wrote:
>
> > > Hi Folks
>
> > > I pass a nested dictionary to a function.
>
> > > def Dicty( dict[k1][k2] ):
> > > print k1
> > > print k2
>
> > > There is a fast way (trick) to get k1 and k2 as string.
>
> > > Whithout loop all dict. Just it!
>
> > > Regards
>
> > > macm
>
> > I've tried to understand this, but can't tell if it's a question or
> > statement, and even then can't tell what the question or statement
> > is...
>
> > Care to eloborate?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Get keys from a dicionary
On Nov 11, 2:25 pm, John Gordon wrote: > In <8f5215a8-d08f-4355-a5a2-77fcaa32c...@j10g2000vbe.googlegroups.com> macm > writes: > > > I pass a nested dictionary to a function. > > def Dicty( dict[k1][k2] ): > > That's not valid syntax. > > > print k1 > > print k2 > > There is a fast way (trick) to get k1 and k2 as string. > > Are you stating this can be done, or are you asking if it can be done? > Questions usually end with a question mark. (Are you a native English > speaker?) > > > Whithout loop all dict. Just it! > > print "%s" % x > > -- > John Gordon A is for Amy, who fell down the stairs > [email protected] B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" Hi John I am not a native English speaker. Sorry bad english. Regards macm -- http://mail.python.org/mailman/listinfo/python-list
Automatic Distutils generator
Hi Folks
I am trying run Distutils setup inside a script.
The Docs dont tell me much and I cant find any examples.
This script will generate shared libraries recursive to all files in a
dir.
-
import os
import sys
from distutils.core import setup as d
from distutils.extension import Extension
from Cython.Distutils import build_ext
fileList = []
rootdir = sys.argv[1]
fileType = '.pyx'
for root, subFolders, files in os.walk(rootdir):
for file in files:
if file[-4:] == fileType:
fileList.append(os.path.join(root,file))
# But Here I want automatic Distutils generator
# I want replace manual entry like this:
# python setup.py build_ext --inplace
d.setup(name = str(file) + '_Cython',
ext_modules=[
Extension(file[:-4], [file])
],
cmdclass = {'build_ext': build_ext})
d.run_setup()
print 'Files convert: \n'
print fileList
Who can help me fix it?
Best Regards
Mario
--
http://mail.python.org/mailman/listinfo/python-list
Partition Recursive
Hi Folks I have this: url = 'http://docs.python.org/dev/library/stdtypes.html? highlight=partition#str.partition' So I want convert to myList = ['http',':','//','docs','.','python','.','org','/','dev','/','library','/','stdtypes','.','html','?','highlight','=','partition','#','str','.','partition'] The reserved char are: specialMeaning = ["//",";","/", "?", ":", "@", "=" , "&","#"] Regards Mario -- http://mail.python.org/mailman/listinfo/python-list
Re: Partition Recursive
Hi urlparse isnt a option. My reasult must be: myList = ['http',':','//','docs','.','python','.','org','/','dev','/','library','/', 'stdtypes','.','html','?','highlight','=','partition','#','str','.','partition'] re module is slow. Even I make a loop in urlparse.urlsplit I can lost specialMeaning order. Seen easy but best aproach will be recursive. Regards Mario On Dec 23, 3:57 pm, Jon Clements wrote: > On Dec 23, 5:26 pm, macm wrote: > > > > > > > > > > > Hi Folks > > > I have this: > > > url = 'http://docs.python.org/dev/library/stdtypes.html? > > highlight=partition#str.partition' > > > So I want convert to > > > myList = > > ['http',':','//','docs','.','python','.','org','/','dev','/','library','/', > > 'stdtypes','.','html','?','highlight','=','partition','#','str','.','partit > > ion'] > > > The reserved char are: > > > specialMeaning = ["//",";","/", "?", ":", "@", "=" , "&","#"] > > > Regards > > > Mario > > I would use urlparse.urlsplit, then split further, if required. > > >>> urlsplit(url) > > SplitResult(scheme='http', netloc='docs.python.org', path='/dev/ > library/stdtypes.html', query='highlight=partition', > fragment='str.partition') > > Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: round in 2.6 and 2.7
On Dec 23, 4:57 pm, Hrvoje Niksic wrote: > I stumbled upon this. Python 2.6: > > Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> 9.95 > 9.9493 > >>> "%.16g" % 9.95 > '9.949' > >>> round(9.95, 1) > > 10.0 > > So it seems that Python is going out of its way to intuitively round > 9.95, while the repr retains the unnecessary digits. However, with 2.7 > it's exactly the opposite: > > Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> 9.95 > 9.95 > >>> "%.16g" % 9.95 > '9.949' > >>> round(9.95, 1) > > 9.9 > > Is the change to round() expected? My guess is use decimal module. Regards mario -- http://mail.python.org/mailman/listinfo/python-list
Re: Partition Recursive
Thanks all
In [11]: reps = 5
In [12]: t = Timer("url = 'http://docs.python.org/dev/library/
stdtypes.html? highlight=partition#str.partition' ;sp =
re.compile('(//?|[;?:@=.])'); filter(len, sp.split(url))", 'import
re')
In [13]: print sum(t.repeat(repeat=reps, number=1)) / reps
4.94003295898e-05
In [65]: t = Timer("url = 'http://docs.python.org/dev/library/
stdtypes.html? highlight=partition#str.partition' ;sp =
re.compile('(//?|[;?:@=.])'); filter(None, sp.split(url))", 'import
re')
In [66]: print sum(t.repeat(repeat=reps, number=1)) / reps
3.50475311279e-05
Ian with None is a litle fast, thanks kj!
Hi Mr. James, speed is always important. But ok re is fine. (but could
be e-07)
In next step I'll go to cython to win something.
Regards
Mario
On Dec 24, 3:33 am, Ian Kelly wrote:
> On 12/23/2010 10:03 PM, kj wrote:
>
> import re # sorry
> sp = re.compile('(//?|[;?:@=.])')
> filter(len, sp.split(url))
>
> Perhaps I'm being overly pedantic, but I would likely have written that
> as "filter(None, sp.split(url))" for the same reason that "if string:"
> is generally preferred to "if len(string):".
>
> Cheers,
> Ian
--
http://mail.python.org/mailman/listinfo/python-list
[python] pass the name of args
Hi Folks
def myDef(x)
doSomething x
result = x.
return coolThings
-
WhatYourName = ('python','is','cool')
myDef(WhatYourName)
so what I am looking for in myDef
result = WhatYourName
--
again :
IhaveOtherName = ('some','thing')
myDef(IhaveOtherName)
so what I am looking for in myDef
result = IhaveOtherName
--
Is it possible with python?
Regards
macm
--
http://mail.python.org/mailman/listinfo/python-list
How find all childrens values of a nested dictionary, fast!
Hi Folks
How find all childrens values of a nested dictionary, fast!
>>> a = {'a' : {'b' :{'/' :[1,2,3,4], 'ba' :{'/' :[41,42,44]} ,'bc'
>>> :{'/':[51,52,54], 'bcd' :{'/':[68,69,66]}}},'c' :{'/' :[5,6,7,8]}}, 'ab' :
>>> {'/' :[12,13,14,15]}, 'ac' :{'/' :[21,22,23]}}
>>> a['a']
{'c': {'/': [5, 6, 7, 8]}, 'b': {'ba': {'/': [41, 42, 44]}, '/': [1,
2, 3, 4], 'bc': {'bcd': {'/': [68, 69, 66]}, '/': [51, 52, 54]}}}
>>> a['a']['b']
{'ba': {'/': [41, 42, 44]}, '/': [1, 2, 3, 4], 'bc': {'bcd': {'/':
[68, 69, 66]}, '/': [51, 52, 54]}}
>>> a['a']['b'].values()
[{'/': [41, 42, 44]}, [1, 2, 3, 4], {'bcd': {'/': [68, 69, 66]}, '/':
[51, 52, 54]}]
Now I want find all values of key "/"
Desire result is [41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54]
I am trying map, reduce, lambda.
Regards
macm
--
http://mail.python.org/mailman/listinfo/python-list
Re: How find all childrens values of a nested dictionary, fast!
Hi Folks Thanks a lot Script from Diez works: print list(f(a)) but should be print list(f(a['a']['b'])) to fit my example. About Peter script I am receiving >>> for v in f(a['a']['b']): ... b.extend(v) ... Traceback (most recent call last): File "", line 2, in TypeError: 'int' object is not iterable I am trying understand this error. Best Regards and thanks a lot again! Mario macm On 4 nov, 15:26, Peter Otten <[email protected]> wrote: > macm wrote: > > How find all childrens values of a nested dictionary, fast! > > >>>> a = {'a' : {'b' :{'/' :[1,2,3,4], 'ba' :{'/' :[41,42,44]} ,'bc' > >>>> :{'/':[51,52,54], 'bcd' :{'/':[68,69,66]}}},'c' :{'/' :[5,6,7,8]}}, > >>>> 'ab' : {'/' :[12,13,14,15]}, 'ac' :{'/' :[21,22,23]}} a['a'] > > {'c': {'/': [5, 6, 7, 8]}, 'b': {'ba': {'/': [41, 42, 44]}, '/': [1, > > 2, 3, 4], 'bc': {'bcd': {'/': [68, 69, 66]}, '/': [51, 52, 54]}}} > >>>> a['a']['b'] > > {'ba': {'/': [41, 42, 44]}, '/': [1, 2, 3, 4], 'bc': {'bcd': {'/': > > [68, 69, 66]}, '/': [51, 52, 54]}} > >>>> a['a']['b'].values() > > [{'/': [41, 42, 44]}, [1, 2, 3, 4], {'bcd': {'/': [68, 69, 66]}, '/': > > [51, 52, 54]}] > > > Now I want find all values of key "/" > > > Desire result is [41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54] > > > I am trying map, reduce, lambda. > > Hmm, I'm trying none of these and get a different result: > > >>> def f(d): > > ... stack = [d.iteritems()] > ... while stack: > ... for k, v in stack[-1]: > ... if k == "/": > ... yield v > ... else: > ... stack.append(v.iteritems()) > ... break > ... else: > ... stack.pop() > ...>>> b = [] > >>> for v in f(a): > > ... b.extend(v) > ...>>> b > > [5, 6, 7, 8, 41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54, 21, 22, 23, 12, > 13, 14, 15] > > What am I missing? > > Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: How find all childrens values of a nested dictionary, fast!
Peter Ok! Both works fine! Thanks a lot! >>> for v in f(a['a']['b']): ... b.extend(v) b Now I will try find which script is the fast! Regards macm On 4 nov, 15:56, macm wrote: > Hi Folks > > Thanks a lot > > Script from Diez works: > > print list(f(a)) > > but should be > > print list(f(a['a']['b'])) > > to fit my example. > > About Peter script > > I am receiving > > >>> for v in f(a['a']['b']): > > ... b.extend(v) > ... > Traceback (most recent call last): > File "", line 2, in > TypeError: 'int' object is not iterable > > I am trying understand this error. > > Best Regards and thanks a lot again! > > Mario > macm > > On 4 nov, 15:26, Peter Otten <[email protected]> wrote: > > > macm wrote: > > > How find all childrens values of a nested dictionary, fast! > > > >>>> a = {'a' : {'b' :{'/' :[1,2,3,4], 'ba' :{'/' :[41,42,44]} ,'bc' > > >>>> :{'/':[51,52,54], 'bcd' :{'/':[68,69,66]}}},'c' :{'/' :[5,6,7,8]}}, > > >>>> 'ab' : {'/' :[12,13,14,15]}, 'ac' :{'/' :[21,22,23]}} a['a'] > > > {'c': {'/': [5, 6, 7, 8]}, 'b': {'ba': {'/': [41, 42, 44]}, '/': [1, > > > 2, 3, 4], 'bc': {'bcd': {'/': [68, 69, 66]}, '/': [51, 52, 54]}}} > > >>>> a['a']['b'] > > > {'ba': {'/': [41, 42, 44]}, '/': [1, 2, 3, 4], 'bc': {'bcd': {'/': > > > [68, 69, 66]}, '/': [51, 52, 54]}} > > >>>> a['a']['b'].values() > > > [{'/': [41, 42, 44]}, [1, 2, 3, 4], {'bcd': {'/': [68, 69, 66]}, '/': > > > [51, 52, 54]}] > > > > Now I want find all values of key "/" > > > > Desire result is [41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54] > > > > I am trying map, reduce, lambda. > > > Hmm, I'm trying none of these and get a different result: > > > >>> def f(d): > > > ... stack = [d.iteritems()] > > ... while stack: > > ... for k, v in stack[-1]: > > ... if k == "/": > > ... yield v > > ... else: > > ... stack.append(v.iteritems()) > > ... break > > ... else: > > ... stack.pop() > > ...>>> b = [] > > >>> for v in f(a): > > > ... b.extend(v) > > ...>>> b > > > [5, 6, 7, 8, 41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54, 21, 22, 23, 12, > > 13, 14, 15] > > > What am I missing? > > > Peter > > -- http://mail.python.org/mailman/listinfo/python-list
How convert list to nested dictionary?
Hi Folks
How convert list to nested dictionary?
>>> l
['k1', 'k2', 'k3', 'k4', 'k5']
>>> result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}
Regards
macm
--
http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
Hi Chris
Thanks for your hint.
I am reading this
http://www.amk.ca/python/writing/functional
Do you have good links or books to me learn "Functional Programming"?
but I am not asking "...because is easy but because is hard."
Show me, please! if you can.
Thanks is advance.
Best regards
macm
On 4 nov, 16:53, Chris Rebert wrote:
> On Thu, Nov 4, 2010 at 11:48 AM, macm wrote:
> > Hi Folks
>
> > How convert list to nested dictionary?
>
> >>>> l
> > ['k1', 'k2', 'k3', 'k4', 'k5']
> >>>> result
> > {'k1': {'k2': {'k3': {'k4': {'k5': {}}
>
> We don't do homework.
> Hint: Iterate through the list in reverse order, building up your
> result. Using reduce() is one option.
>
> Cheers,
> Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
Hi Folks
thanks a lot all. All solutions work fine.
while I am doing my home work.
Reading "Learning Python" and much more.
Let me ask again to close my doubts:
>>> l = ['k1', 'k2', 'k3', 'k4', 'k5']
>>> d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
>>> d
{'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}
>>> d['k1']['k2']['k3']['k4']['k5']
{'/': [1, 2, 3]}
>>> d['k1']['k2']['k3']['k4']['k5']['/']
[1, 2, 3]
>>>
now I want generate the "index" to access the element.
==> d['k1']['k2']['k3']['k4']['k5']['/'] from l
So again I have only.
>>> l = ['k1', 'k2', 'k3', 'k4', 'k5']
z = ?magicCode?
z = d['k1']['k2']['k3']['k4']['k5']['/']
>>> z
[1, 2, 3]
>>>z.append('4')
>>>z
[1, 2, 3, 4]
Best Regards
macm
On 5 nov, 11:57, Peter Otten <[email protected]> wrote:
> Boris Borcic wrote:
> > Arnaud Delobelle wrote:
> >> macm writes:
>
> >>> Hi Folks
>
> >>> How convert list to nested dictionary?
>
> >>>>>> l
> >>> ['k1', 'k2', 'k3', 'k4', 'k5']
> >>>>>> result
> >>> {'k1': {'k2': {'k3': {'k4': {'k5': {}}
>
> >>> Regards
>
> >>> macm
>
> >> reduce(lambda x,y: {y:x}, reversed(l), {})
>
> > d={}
> > while L : d={L.pop():d}
>
> Iterating over the keys in normal order:
>
> >>> keys = "abcde"
> >>> d = outer = {}
> >>> for key in keys:
>
> ... outer[key] = inner = {}
> ... outer = inner
> ...>>> d
>
> {'a': {'b': {'c': {'d': {'e': {}}
>
> The "functional" variant:
>
> >>> d = {}
> >>> reduce(lambda outer, key: outer.setdefault(key, {}), "abcde", d)
> {}
> >>> d
>
> {'a': {'b': {'c': {'d': {'e': {}}
>
> In a single expression if you are willing to pay the price:
>
> >>> reduce(lambda (accu, outer), key: (accu, outer.setdefault(key, {})),
>
> "abcde", ({},)*2)[0]
> {'a': {'b': {'c': {'d': {'e': {}}
>
> Peter
--
http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
Hi Peter Thanks a lot for your tips and codes, Cake Recipes are good to learn! So I post just basic issues. Hopping a good soul like you can help me! But I am still learning... : ) Best Regards macm On 5 nov, 15:40, Peter Otten <[email protected]> wrote: > macm wrote: > > thanks a lot all. All solutions work fine. > > > while I am doing my home work. > > Reading "Learning Python" and much more. > > > Let me ask again to close my doubts: > > >>>> l = ['k1', 'k2', 'k3', 'k4', 'k5'] > >>>> d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]}) > >>>> d > > {'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}} > >>>> d['k1']['k2']['k3']['k4']['k5'] > > {'/': [1, 2, 3]} > >>>> d['k1']['k2']['k3']['k4']['k5']['/'] > > [1, 2, 3] > > > now I want generate the "index" to access the element. > > > ==> d['k1']['k2']['k3']['k4']['k5']['/'] from l > > > So again I have only. > >>>> l = ['k1', 'k2', 'k3', 'k4', 'k5'] > > > z = ?magicCode? > > > z = d['k1']['k2']['k3']['k4']['k5']['/'] > > You'll eventually have to start and write your first line of code. Why not > doing it right now? It is sure more rewarding than copying other people's > canned solutions and it can even be fun. > > Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: How convert list to nested dictionary?
Ok. Done
>>> def appendNested(nest, path, val):
... nest2 = nest
... for key in path[:-1]:
... nest2 = nest2[key]
... nest2[path[-1]].append(val)
... return nest
...
>>>
>>> l = ['k1','k2','k3','k4','k5']
>>> ndict = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
>>> x = l + list('/')
>>> print appendNested(ndict, x, 5)
{'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3, 5]}}
>>>
I need make lambda but now is friday night here and I guess I will go
drink a beer or three!
Cheers!
macm
On 5 nov, 16:51, macm wrote:
> Hi Peter
>
> Thanks a lot for your tips and codes,
>
> Cake Recipes are good to learn! So I post just basic issues.
>
> Hopping a good soul like you can help me!
>
> But I am still learning... : )
>
> Best Regards
>
> macm
>
> On 5 nov, 15:40, Peter Otten <[email protected]> wrote:
>
> > macm wrote:
> > > thanks a lot all. All solutions work fine.
>
> > > while I am doing my home work.
> > > Reading "Learning Python" and much more.
>
> > > Let me ask again to close my doubts:
>
> > >>>> l = ['k1', 'k2', 'k3', 'k4', 'k5']
> > >>>> d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
> > >>>> d
> > > {'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}
> > >>>> d['k1']['k2']['k3']['k4']['k5']
> > > {'/': [1, 2, 3]}
> > >>>> d['k1']['k2']['k3']['k4']['k5']['/']
> > > [1, 2, 3]
>
> > > now I want generate the "index" to access the element.
>
> > > ==> d['k1']['k2']['k3']['k4']['k5']['/'] from l
>
> > > So again I have only.
> > >>>> l = ['k1', 'k2', 'k3', 'k4', 'k5']
>
> > > z = ?magicCode?
>
> > > z = d['k1']['k2']['k3']['k4']['k5']['/']
>
> > You'll eventually have to start and write your first line of code. Why not
> > doing it right now? It is sure more rewarding than copying other people's
> > canned solutions and it can even be fun.
>
> > Peter
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Learning Pyhton - Functional Programming - How intersect/difference two dict with dict/values? fast!
Hi Folks,
dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],
54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],
12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':
[[9],34]}
dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
intersect = filter(dict1.has_key, dict2.keys())
intersect
result:
['ax', 'ac', 'ab']
expect result dict1 intersection with dict2
{'ac':[1,3,'79b'], 'ab':[2,'d3']} # look last key/value 'ax' (dict1,
dict2) even intersec a key but not values from list so not valid
and difference from dict3
dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
result from (intersect - dict3)
{'ac':['79b'], 'ab':[2,'d3']}
Thanks in advance!
Before someone blame me.
Yes I am trying learn python Functional Programming! ; )
Best Regards
macm
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning Pyhton - Functional Programming - How intersect/difference two dict with dict/values? fast!
Sorry Mr. Nagle and Folks
I had a bad day today.
I need forward but with fever, a grasp and headache is hard.
You are absolute right, I was rude and I ask your pardon.
About my code, sorry I thought was the best way to copy and paste in
python console.
Best Regards
macm
On Nov 9, 7:03 pm, "D'Arcy J.M. Cain" wrote:
> On Tue, 09 Nov 2010 15:55:16 -0500
>
> Terry Reedy wrote:
> > To echo John Nagle's point, if you want non-masochist volunteers to read
> > your code, write something readable like:
>
> > dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> > 'ac': [[1,3,'78a','79b'], 54],
> > 'ad': [[56,57,58,59], 34],
> > 'ax': [[56,57,58,59], 34]}
>
> I have been learning to like this form:
>
> dict1 = dict(
> ab = [[1,2,3,'d3','d4',5], 12],
> ac = [[1,3,'78a','79b'], 54],
> ad = [[56,57,58,59], 34],
> ax = [[56,57,58,59], 34],
> )
>
> Of course, it only works when all the keys are strings.
>
> --
> D'Arcy J.M. Cain | Democracy is three
> wolveshttp://www.druid.net/darcy/ | and a sheep voting on
> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning Pyhton - Functional Programming - How intersect/difference two dict with dict/values? fast!
Hi Folks
I am studing yet (with fever, grasp and headache).
I know I can do better, but first I should learn more about
"dictionary comprehension syntax" in python 2.65
>>> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],54],'ad':
>>> [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
>>> dict2 =
>>> {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae':
>>> [[56,57,58,59],34],'ax':[[9],34]}
>>>
>>> # Arnaud Delobelle
... def intersectList(iterables):
... nexts = [iter(iterable).next for iterable in iterables]
... v = [next() for next in nexts]
... while True:
... for i in xrange(1, len(v)):
... while v[0] > v[i]:
... v[i] = nexts[i]()
... if v[0] < v[i]: break
... else:
... yield v[0]
... v[0] = nexts[0]()
...
>>> def intersect(s1, s2):
... d = {}
... e = {}
... r1 = filter(s1.has_key, s2.keys())
... for x in r1:
... d[x] = list(intersectList([s1[x][0],s2[x][0]]))
... if len(d[x]) > 0:
... e[x] = d[x]
... return e
...
>>> intersect(dict1,dict2)
{'ac': [1, 3, '79b'], 'ab': [2]}
>>>
Best Regards
Mario
On 10 nov, 07:51, Paul Rudin wrote:
> Lawrence D'Oliveiro writes:
> > In message , Terry Reedy
> > wrote:
>
> >> To echo John Nagle's point, if you want non-masochist volunteers to read
> >> your code, write something readable like:
>
> >> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> >> 'ac': [[1,3,'78a','79b'], 54],
> >> 'ad': [[56,57,58,59], 34],
> >> 'ax': [[56,57,58,59], 34]}
>
> > How come Python itself doesn’t display things that way?
>
> try: pprint.pprint(dict1)
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning Pyhton - Functional Programming - How intersect/difference two dict with dict/values? fast!
... and this works!
>>> def intersect(s1, s2):
... d = {}
... e = {}
... r1 = filter(s1.has_key, s2.keys())
... for x in r1:
... d[x]= filter(lambda z:z in s1[x][0],s2[x][0])
... if len(d[x]) > 0:
... e[x] = d[x]
... return e
...
>>> intersect(dict1,dict2)
{'ac': [1, 3, '79b'], 'ab': [2]}
>>>
but how I pass d[x] and make the filter?
>>> result = [filter(lambda z:z in dict1[x][0],dict2[x][0]) for x in
>>> filter(dict1.has_key, dict2.keys())]
>>> result
[[], [1, 3, '79b'], [2]]
>>>
but should be {'ac': [1, 3, '79b'], 'ab': [2]}
Best Regards
Mario
On 10 nov, 18:14, macm wrote:
> Hi Folks
>
> I am studing yet (with fever, grasp and headache).
>
> I know I can do better, but first I should learn more about
> "dictionary comprehension syntax" in python 2.65
>
> >>> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],54],'ad':
> >>> [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
> >>> dict2 =
> >>> {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae':
> >>> [[56,57,58,59],34],'ax':[[9],34]}
>
> >>> # Arnaud Delobelle
>
> ... def intersectList(iterables):
> ... nexts = [iter(iterable).next for iterable in iterables]
> ... v = [next() for next in nexts]
> ... while True:
> ... for i in xrange(1, len(v)):
> ... while v[0] > v[i]:
> ... v[i] = nexts[i]()
> ... if v[0] < v[i]: break
> ... else:
> ... yield v[0]
> ... v[0] = nexts[0]()
> ...>>> defintersect(s1, s2):
>
> ... d = {}
> ... e = {}
> ... r1 = filter(s1.has_key, s2.keys())
> ... for x in r1:
> ... d[x] = list(intersectList([s1[x][0],s2[x][0]]))
> ... if len(d[x]) > 0:
> ... e[x] = d[x]
> ... return e
> ...>>>intersect(dict1,dict2)
>
> {'ac': [1, 3, '79b'], 'ab': [2]}
>
>
>
> Best Regards
>
> Mario
>
> On 10 nov, 07:51, Paul Rudin wrote:
>
> > Lawrence D'Oliveiro writes:
> > > In message , Terry
> > > Reedy
> > > wrote:
>
> > >> To echo John Nagle's point, if you want non-masochist volunteers to read
> > >> your code, write something readable like:
>
> > >> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> > >> 'ac': [[1,3,'78a','79b'], 54],
> > >> 'ad': [[56,57,58,59], 34],
> > >> 'ax': [[56,57,58,59], 34]}
>
> > > How come Python itself doesn’t display things that way?
>
> > try: pprint.pprint(dict1)
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Open Multiples Files at same time with multiprocessing - How "declare" dynamically the var?
Hi Folks
My approach to open multiples files at same time is:
def openFiles(self,file,q):
fp = open(file, 'rb')
fp.seek(0)
fcontent = fp.read()
fp.close()
q.put(fcontent)
return
def testOpen(self):
L =
['file1.txt','file2.txt','file3.txt','file4.txt','file5.txt']
d1 = []
for x in L:
z=L.index(x)
q = Queue()
m = Process(target=self.openFiles, args=(x,q,))
m.start()
d1.append(q.get()) # <= This get is locking ? It is mean:
"wait m.start(), like m.join()??"
print list(d1)
return
Is the best way? Is q.get() locking the loop?
I feel that q.get() is locking so I would like to "declare"
dynamically the var q{z} in python? is it possible? How?
def testOpen(self):
L =
['file1.txt','file2.txt','file3.txt','file4.txt','file5.txt']
d1 = []
for x in L:
z=L.index(x)
q{z} = Queue()
m{z} = Process(target=self.openFiles, args=(x,q{z},))
m{z}.start()
for x in L:
z=L.index(x)
d1.append(q{z}.get()) # <= So now I am sure that q{z}.get() isn't
lock
print list(d1)
return
I tried use list but didn't work look below one shot.
Best Regards
macm
I tried :
def testOpen(self):
L = ['file1.txt','file2.txt',
'file3.txt','file4.txt',
'file5.txt']
d1 = []
q = []
m = []
for x in L:
z=L.index(x)
q.insert(z, Queue())
m.insert(z,Process(target=self.openFiles, args=(x,q[z])))
m[z].start()
# Now I am sure. Isnt lock
for x in L:
z=L.index(x)
d1.append(q[z].get())
print list(d1)
return
--
http://mail.python.org/mailman/listinfo/python-list
