-1

Hi guys im new to python. I have a question concerning a double-for loop:

for i in range (rounds):
    for n, data in G.nodes ().items ():
        if data ["example"] == "x":

            for neighbor in nx.all_neighbors (G, n):
                if -------------             

    for n, data in G.nodes ().items ():
        if data ["test"] == "I" and random.random () < somevalue
            data ["test"] = "R"

        if data ["test"] == "NI":
            DO Something

This loop at the end is changing some status in data["test"] -- Do Something. My Question: How can i setup that the status/variable is changing after 2 ROUNDS and not next round?

Thanks, Patrick

  • Please remove parts of code that are unrelated to your problem. They distract. – Psytho Jan 17 '18 at 17:38
  • Your question is totally unclear. What is a "round"? What exactly is the behavior you want? Most of the stuff in your example seems superfluous and doesn't aid in understanding your intent. Can you make a simple, basic example? – juanpa.arrivillaga Jan 17 '18 at 17:43
  • Hi. thank you guys for your awnser. Sorry for my unclear question. – Patrick Sacher Jan 17 '18 at 17:51

1 Answers1

2

It's not really clear what problem you're trying to solve, but if you want to skip the first item in G.nodes(), then there are a couple of ways you can do it. In Python 2, you could change the for loop to:

for n, data in G.nodes().items()[1:]:

The [1:] at the end causes the loop to iterate over all the elements of the list except the first one. This notation is called a slice and you can find more information at Understanding Python's slice notation.

In Python 3, since items() returns an iterable, you can't use slice notation and one way to do it would be to use itertools.islice:

import itertools

for n, data in itertools.islice(G.nodes().items(), 1, None):
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285