简体   繁体   中英

How to check if a list exists in python?

I have a networkx graph. I am adding nodes by adding edges

G.add_edge(route[i-1],route[i]);

Now once the node is created by directly adding edges, I add a list named

G.node[route[i]]['position'] = list()

and I append positions to it when I get same nodes again and again

G.node[route[i]]['position'].append( i - 3 )

Now when I want to append how do I check whether the list exist? does doing

G.node[route[i]]['position'] = list()

clear the list of already existing elements?

edit----- my earlier question was confusing I want to keep adding to the list but I cant append unless a list exists, right? So I have to do do G.node[route[i]]['position'] = list() in my loop So next time when I want to add to the same list in another loop instance how do I know that a list exists for G.node[route[i]]['position'] and I dont have to create it again.

edit----- I think my list itself is a key here so I did

                    if not 'position' in G.node[route[i]]:

and it works

G.node[route[i]]['position'] = list() will leave the slot G.node[route[i]]['position'] holding an empty list, but it will not affect the list that it previously held, which other objects may have a reference to.

Instead, use: del l[:] to empty the list.

If you want to have a list automatically created, use collections.defaultdict to have newly created entries default to a list.

Yes, that clears the existing list. You could try

G.node[route[i]].setdefault('position', []).append(...)

whenever you want to append elements.

Not sure if this is what you mean, but assigning list() should make sure that there is a list to append to. If there's already a list the assignment creates a new one (see answer of Marcin). Test:

>>> a = list()
>>> for i in range(10):
...     a.append(i)
...
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = a
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = list()
>>> a
[]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1) How do I check whether the list exist?

Use isinstance() and do something like:

if not ininstance(G.node[route[i]]['position'], list):
    G.node[route[i]]['position'] = list()
G.node[route[i]]['position'].append(i - 3)

Or use type like:

if not type(G.node[route[i]]['position']) is list

I must say that this kind of checking is rather un-pythonic, usually you should know what G.node[route[i]]['position'] was before becoming a list and check for that.

For example, if it was None you could do (assuming that the key 'position' exists, otherwise just call get('position') ):

if G.node[route[i]]['position'] is None:
    G.node[route[i]]['position'] = list()
G.node[route[i]]['position'].append(i - 3)

2) Does doing .. = list() clear the list of already existing elements?

The answer is No.

list() will instantiate a new empty list.

You may want to take a look at this SO question: How to empty a list in Python? .
In short:

G.node[route[i]]['position'][:] = []

will clear your list.

Just use get:

    if G.node[route[i]].get('position') is None:
        G.node[route[i]]['position'] = list()
    else:
        G.node[route[i]]['position'].append(stuff)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM