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


Groups > comp.lang.python > #83406 > unrolled thread

Array of hash table

Started byjyoti690saini@gmail.com
First post2015-01-08 23:17 -0800
Last post2015-01-09 20:38 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Array of hash table jyoti690saini@gmail.com - 2015-01-08 23:17 -0800
    Re: Array of hash table Dave Angel <davea@davea.name> - 2015-01-09 02:30 -0500
    Re: Array of hash table Peter Otten <__peter__@web.de> - 2015-01-09 08:55 +0100
    Re: Array of hash table Jason Friedman <jsf80238@gmail.com> - 2015-01-09 20:38 -0700

#83406 — Array of hash table

Fromjyoti690saini@gmail.com
Date2015-01-08 23:17 -0800
SubjectArray of hash table
Message-ID<05c22fd5-bfae-4e09-9894-341f468e08dc@googlegroups.com>
Hello,

   Can any one tell me how to create 
   graph={
  "nodes": [
    {
      "id": "n0",
      "label": "A node",
      "x": 0,
      "y": 0,
      "size": 3
    },
    {
      "id": "n1",
      "label": "Another node",
      "x": 3,
      "y": 1,
      "size": 2
    },
    {
      "id": "n2",
      "label": "And a last one",
      "x": 1,
      "y": 3,
      "size": 1
    }
  ],
  "edges": [
    {
      "id": "e0",
      "source": "n0",
      "target": "n1",
      "label" : "dfghujikoi"
    },
    {
      "id": "e1",
      "label" : "dfghujikoi",
      "source": "n1",
      "target": "n2"
      
    },
    {
      "id": "e2",
      "source": "n2",
      "target": "n0",
      "label" : "dfghujikoi"
    }
  ]
}

using python?

its like a hash table and value is an array of hash table ?
I tried but it was giving error of "List out of index" .

[toc] | [next] | [standalone]


#83408

FromDave Angel <davea@davea.name>
Date2015-01-09 02:30 -0500
Message-ID<mailman.17508.1420788672.18130.python-list@python.org>
In reply to#83406
On 01/09/2015 02:17 AM, jyoti690saini@gmail.com wrote:
> Hello,
>
>     Can any one tell me how to create
>     graph={
>    "nodes": [
>      {
>        "id": "n0",
>        "label": "A node",
>        "x": 0,
>        "y": 0,
>        "size": 3
>      },
>      {
>        "id": "n1",
>        "label": "Another node",
>        "x": 3,
>        "y": 1,
>        "size": 2
>      },
>      {
>        "id": "n2",
>        "label": "And a last one",
>        "x": 1,
>        "y": 3,
>        "size": 1
>      }
>    ],
>    "edges": [
>      {
>        "id": "e0",
>        "source": "n0",
>        "target": "n1",
>        "label" : "dfghujikoi"
>      },
>      {
>        "id": "e1",
>        "label" : "dfghujikoi",
>        "source": "n1",
>        "target": "n2"
>
>      },
>      {
>        "id": "e2",
>        "source": "n2",
>        "target": "n0",
>        "label" : "dfghujikoi"
>      }
>    ]
> }
>
> using python?
>
> its like a hash table and value is an array of hash table ?
> I tried but it was giving error of "List out of index" .
>

You just did.  If you'd like to see it, add a print(graph) .  What's 
your real question?

BTW, in Python, it's called a dict, not a hash table.  But you did it 
correctly.

And if you want to fetch a particular element, use:

print(graph["edges"][1]["id"])

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#83411

FromPeter Otten <__peter__@web.de>
Date2015-01-09 08:55 +0100
Message-ID<mailman.17511.1420790268.18130.python-list@python.org>
In reply to#83406
jyoti690saini@gmail.com wrote:

> Hello,
> 
>    Can any one tell me how to create
>    graph={
>   "nodes": [
>     {
>       "id": "n0",
>       "label": "A node",
>       "x": 0,
>       "y": 0,
>       "size": 3
>     },
>     {
>       "id": "n1",
>       "label": "Another node",
>       "x": 3,
>       "y": 1,
>       "size": 2
>     },
>     {
>       "id": "n2",
>       "label": "And a last one",
>       "x": 1,
>       "y": 3,
>       "size": 1
>     }
>   ],
>   "edges": [
>     {
>       "id": "e0",
>       "source": "n0",
>       "target": "n1",
>       "label" : "dfghujikoi"
>     },
>     {
>       "id": "e1",
>       "label" : "dfghujikoi",
>       "source": "n1",
>       "target": "n2"
>       
>     },
>     {
>       "id": "e2",
>       "source": "n2",
>       "target": "n0",
>       "label" : "dfghujikoi"
>     }
>   ]
> }
> 
> using python?
> 
> its like a hash table and value is an array of hash table ?
> I tried but it was giving error of "List out of index" .

(1) Was that something like

>>> [][1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

? Please always copy and paste the traceback, don't rephrase.

(2) Show us the code that caused the error. If you provide some context it 
is easier for us to identify your problem. But if I were to guess: are you 
coming from a language where lists grow automatically? In Python they don't.

Instead of

mylist = []
for i in range(3):
   mylist[i] = 42 # WRONG, raises IndexError

you usually write

mylist = []
for i in range(3):
   mylist.append(42)

though in this particular case

mylist = [42] * 3

would work, too.

[toc] | [prev] | [next] | [standalone]


#83601

FromJason Friedman <jsf80238@gmail.com>
Date2015-01-09 20:38 -0700
Message-ID<mailman.17615.1421058919.18130.python-list@python.org>
In reply to#83406
>    Can any one tell me how to create
>    graph={
>   "nodes": [
>     {
>       "id": "n0",
>       "label": "A node",
>       "x": 0,
[ ... elided ... ]
>     }
>   ]
> }


Taking a guess and guessing that graphviz might be useful for you:
https://code.google.com/p/pydot/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web