Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109811

Re: fast dictionary initialization

From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: fast dictionary initialization
Date 2016-06-10 18:20 -0500
Message-ID <mailman.148.1465613257.2306.python-list@python.org> (permalink)
References <d2ee0910-1635-431e-944c-c8b4167f10c5@googlegroups.com> <20160610182014.0a9182a9@bigbox.christie.dr>

Show all headers | View raw


On 2016-06-10 14:07, maurice wrote:
> example:
> valuesList = [1,2,3]
> keysList   = ['1','2','3']
> 
> So the dictionary can basically convert string to int:
> 
> dictionary = {'1':1, '2':2, '3':3}

A couple similar options:


The most straightforward translation of your description:

  opt1 = dict(zip(keysList, valuesList))
  print(opt1["2"])

And one where you generate the strings on the fly:

  opt2 = dict((str(i), i) for i in range(1, 4))
  print(opt2["2"])

And one where you use the int() function instead of a mapping because
the whole idea of storing a dict worth of string-numbers-to-numbers
seems somewhat silly to me:

  print(int("2"))

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

fast dictionary initialization maurice <mauricioliveiraguarda@gmail.com> - 2016-06-10 14:07 -0700
  Re: fast dictionary initialization Random832 <random832@fastmail.com> - 2016-06-10 17:15 -0400
  Re: fast dictionary initialization Tim Chase <python.list@tim.thechases.com> - 2016-06-10 18:20 -0500

csiph-web