-1

I'm working with a nested list. I'm using a for loop to iterate through it. Sometimes a particular item in the list is dependent on a subsequent nested list. I tried iterating and using next, but that uses up the items that I have not accessed yet. Here is my list. When I look at the first nested list, I don't want to print it if a subsequent list has data that refers to an apple and says that it was a "bad_apple". There could be future nested lists that are followed by "good_apple", and I would want to print that one.

fruit = [
  ["apple", "round", "small", "red"], 
  ["banana", "long", "large", "yellow"],
  ["apple", "round", "large", "bad_apple"]
  ["apple", "round", "medium", "red"], 
  ["banana", "long", "large", "yellow"],
  ["apple", "round", "large", "good_apple"] 
]

for i in fruit:
    # print i only if there is no "bad_apple" in the following 3 nested lists
    print i

So in this example, I would not want to print the first apple, the "small" one, but I would want to print the 2nd apple, the "medium" one since it is followed by a subsequent list that has "good_apple" in the list. I only want to see 2 or 3 nested lists into the "future".

rebar
  • 80
  • 2
  • 6
  • 2
    Not very clear what you're asking. I think you need to post a more complete example – Cfreak Sep 04 '14 at 21:29
  • by subsequent do you mean the very next list or any of the following lists? – Padraic Cunningham Sep 04 '14 at 21:30
  • What would be the desired output for your example? – Erik Vesteraas Sep 04 '14 at 21:30
  • 2
    You could take multiple passes of the list collecting intermediate results. For instance, fill in `bad_fruit=set()` on one pass and check on the second. Or a dict with i[0] as the key and a list of i[1:] as the value so that you have a much smaller check second time around. – tdelaney Sep 04 '14 at 21:33

5 Answers5

1

With a helpful generator function, the code isn't too bad.

for before, i, after in partitions(fruit):
  if not any(j[0] == i[0] and 'bad_'+i[0] == j[3] for j in after[:3]):
    print i

As you can see, the partitions() function returns each element in the list, along with its preceding and succeeding elements. Then it is just up to the loop body to determine the bad_apple condition.

Here is the complete program:

def partitions(l):
    preceding = []
    subsequent = l[:]
    while subsequent:
        current = subsequent.pop(0)
        yield preceding, current, subsequent
        preceding.append(current)

fruit = [
  ["apple", "round", "small", "red"],
  ["banana", "long", "large", "yellow"],
  ["apple", "round", "large", "bad_apple"],
  ["apple", "round", "medium", "red"],
  ["banana", "long", "large", "yellow"],
  ["apple", "round", "large", "good_apple"]
]

for before, i, after in partitions(fruit):
  if not any(j[0] == i[0] and 'bad_'+i[0] == j[3] for j in after[:3]):
    print i
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

fruit[0][1] will give 'round'

fruit[1][2] will give 'long' and so on

so for the loop you can try something like

for i in fruit:
    for j in i:
        print j

not sure of the syntax but its almost close

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Karan Shah
  • 17
  • 7
0

like this:

using your list as the example:

fruit = [
  ["apple", "round", "large", "red"], 
  ["banana", "long", "large", "yellow"],
  [ "apple", "round", "large", "bad_apple"] 
]

where ["apple", "round", "large", "red"] is the first sublist

you can use:

for list in fruit:
    for item in list:
        print(item)

in some other scenarios it can also be useful to use this form:

print(fruit[1][3])

where this would print the 4th item in the second sublist (remembering the indexes start at 0 for the first item in the list)

James

James Kent
  • 5,763
  • 26
  • 50
0

You're going to have to do a double nested iteration on the list if you want future information for each element you parse (unless you plan to use space to memorize stuff as you go along.)

for i in range(len(fruit)):
    badFruit = False
    for j in range(len(fruit)):
        for attribute in fruit[j]:
            if "bad_" + fruit[i] == attribute:
                badFruit = True
                break
        if badFruit:
            break
    else:
        print(fruit[i])

This is one of the many possible solutions.

Cocksure
  • 270
  • 1
  • 10
0

If you want to check any subsequent list after the current element for aspecific value use enumerate:

fruit = [
  ["apple", "round", "large", "red"],
  ["banana", "long", "large", "yellow"],
  [ "apple", "round", "large", "bad_apple"]
]
for ind, ele in enumerate(fruit[:-1]):
    if "apple" in ele and any("bad_apple" in x for x in fruit[ind+1]):
        print "foo"
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321