Re: list or dictionary

2016-09-21 Thread Ganesh Pal
Thanks , and it has to be re.match() On Thu, Sep 22, 2016 at 12:18 AM, MRAB wrote: > On 2016-09-21 19:35, Ganesh Pal wrote: > >> Thanks Steve for the clues , quickly tried out # Version 1 doesn't seen >> to work. >> >> >> for line in hostname: > ... regex = r'(.*) is array with i

Re: list or dictionary

2016-09-21 Thread MRAB
On 2016-09-21 19:35, Ganesh Pal wrote: Thanks Steve for the clues , quickly tried out # Version 1 doesn't seen to work. for line in hostname: ... regex = r'(.*) is array with id {}'.format(devid) ... mo = re.search(regex, line) ... print line, regex, mo ... if mo is not None

Re: list or dictionary

2016-09-21 Thread Ganesh Pal
Thanks Steve for the clues , quickly tried out # Version 1 doesn't seen to work. >>> for line in hostname: ... regex = r'(.*) is array with id {}'.format(devid) ... mo = re.search(regex, line) ... print line, regex, mo ... if mo is not None: ... print mo.group(1) ... RX-

Re: list or dictionary

2016-09-20 Thread Steve D'Aprano
On Wed, 21 Sep 2016 04:04 am, Ganesh Pal wrote: > I am on python 2.7 and Linux > > I have the stdout in the below form , I need to write a function to get > hostname for the given id. > > > Example: > stdout > 'hostname-1 is array with id 1\nhostname-2 is array with id 2\nhostname-3 > is

Re: list or dictionary

2016-09-20 Thread Chris Angelico
On Wed, Sep 21, 2016 at 4:04 AM, Ganesh Pal wrote: > 1. store it in list and grep for id and return > 2. store it in dict as key and value i.e hostname = { 'hostname1': 1} and > return key > 3. any other simple options. 4. Store it in dict, but the other way around. The key should be the thing yo

Re: list or dictionary

2016-09-20 Thread Lawrence D’Oliveiro
On Wednesday, September 21, 2016 at 6:05:41 AM UTC+12, Ganesh Pal wrote: > I am on python 2.7 ... Why? -- https://mail.python.org/mailman/listinfo/python-list

list or dictionary

2016-09-20 Thread Ganesh Pal
I am on python 2.7 and Linux I have the stdout in the below form , I need to write a function to get hostname for the given id. Example: >>> stdout 'hostname-1 is array with id 1\nhostname-2 is array with id 2\nhostname-3 is array with id 3\n' def get_hostname(id) return id what's a

Re: modifying values of List or Dictionary when iterating on them

2007-05-24 Thread aspineux
On 24 mai, 19:21, "Christopher Anderson" <[EMAIL PROTECTED]> wrote: > > d=dict(a=1, b=2, c=3) > > for k, v in d.iteritems(): > > d[k]=d[k]+1 > > You might as well do: d[k] = v + 1, like for the list. ops, yes it was a typo -- http://mail.python.org/mailman/listinfo/python-list

Re: modifying values of List or Dictionary when iterating on them

2007-05-24 Thread Christopher Anderson
> d=dict(a=1, b=2, c=3) > for k, v in d.iteritems(): > d[k]=d[k]+1 You might as well do: d[k] = v + 1, like for the list. -- http://mail.python.org/mailman/listinfo/python-list

modifying values of List or Dictionary when iterating on them

2007-05-24 Thread aspineux
Hello I just want to update the data inside List or Dictionary without adding or deleting object. is this correct ? l=[1, 2, 3] for i, v in enumerate(l): l[i]=v+1 d=dict(a=1, b=2, c=3) for k, v in d.iteritems(): d[k]=d[k]+1 Both works, but : are they correct ? are they optimum for