4

I have two lists named extra and be in my code.

extra output is [7,27] and

be output is

'Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

I need to find the 7th and 27th word elements of extra (i.e. elements don't count if they are an empty string). It should be Nonsocial Play and Groom but the for-loop I have only prints Nonsocial Play

These are the for-loops I am using:

for x in extra:
count = 0
for y in be:
    if y != '':
        if x == count:
            print(be[x])
            count += 1
        elif x != count:
            count += 1

If you have any idea why it isn't working, please let me know! EDIT: I want to print these statements but I also need to delete them

bphi
  • 3,115
  • 3
  • 23
  • 36
katiedidkatie
  • 157
  • 10
  • Please fix the indentation of the first for statement. – wwii Jul 31 '18 at 16:07
  • 2
    All the answers are suggesting rewrites, but you're just printing the wrong thing: (`be[x]` is wrong) – c2huc2hu Jul 31 '18 at 16:13
  • Do you want to delete the items from the list or replace them with an empty string - `''`? After processing does the list order have to be the same. – wwii Jul 31 '18 at 17:28
  • @wwii I want to delete those items from the list. The order of the list should be the same besides that – katiedidkatie Jul 31 '18 at 18:54

8 Answers8

2

I created simple function filter_iter(itr) that filters the iterable in argument itr from any empty values. Then you can access the resulting filtered list with values from list extra:

extra = [7,27]
be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

def filter_iter(itr):
    return [i for i in itr if i]

be = filter_iter(be)
print([be[e] for e in extra])

Prints:

['Nonsocial Play', 'Groom']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
2

You can also just do

extra = [7,27]
be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

>>> [x for i, x in enumerate(s for s in be if s) if i in extra]
['Nonsocial Play', 'Groom']
bphi
  • 3,115
  • 3
  • 23
  • 36
1

Python 2

new_be=filter(lambda a: a != '', be) #we delete from the list all the '' and we save the new list in new_be
world=[]
for i in extra:
    world.append(new_be[int(i)])

print (world)

Python 3

    new_be=list(filter(("").__ne__, be)) #we delete from the list all the '' and we save the new list in new_be
    world=[]
    for i in extra:
        world.append(new_be[int(i)])

    print (world)

['Nonsocial Play', 'Groom']

Carlo 1585
  • 1,455
  • 1
  • 22
  • 40
1

Single line of code using list comprehension with enumerate and filter

[j for i, j in enumerate(list(filter(lambda x: x != '', be))) if i in extra]
['Nonsocial Play', 'Groom']

Testing for speed:

%timeit [j for i, j in enumerate(list(filter(lambda x: x != '', be))) if i in extra]
45.2 µs ± 5.48 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
W Stokvis
  • 1,409
  • 8
  • 15
1

When the count reaches x, print the current item from be

for x in extra:
    count = 0
    for y in be:
        if y != '':
            if x == count:
                print(y)
                #print(be[x])
                count += 1
            elif x != count:
                count += 1

Deleting items from a sequence while iterating over it doesn't work.
Make deques of extra and be and use the rotate method to visit the items in be. Design the logic so that be items are popped off when they meet the criteria and extra items are popped off when a be item is found. Keep track of rotations so the order can be reconstructed.

extra.sort()
extraa = collections.deque(extra)
bee = collections.deque(be)
#print(len(bee))
rotation_count = 0
word_count = 0
while extraa:
    while bee[0] == '':
        bee.rotate(-1)
        rotation_count += 1
    if word_count == extraa[0]:
        print(bee.popleft())
        extraa.popleft()
    else:
        bee.rotate(-1)
        rotation_count += 1
    word_count += 1
#print(len(bee))

bee.rotate(rotation_count)
be = list(bee)
wwii
  • 23,232
  • 7
  • 37
  • 77
1

Here is a solution using using filter the elements, enumerate to iterate over the resulting iterator along with its index, and itertools.islice to get only certain range of indexes from it

>>> from itertools import islice
>>> extra = [7,27]
>>> be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']
>>> [x for i,x in enumerate(islice(filter(bool, be), min(extra), max(extra)+1), min(extra)) if i in extra]
['Nonsocial Play', 'Groom']
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

It isn't working because the indentation isn't correct.It would also be a good idea to include a break once the element is found since you are starting a new count after finding the first element. You are also printing be[x] which wrong. It should be y instead as y is the string you want to print.

for x in extra:
    count = 0
    for y in be:
        if y != '':
            if x == count:
                print(y)
                count += 1
                break
            elif x != count:
                count += 1
Rik
  • 467
  • 4
  • 14
0

This:

[[w for w in be if w != ''][k] for k in extra]
lashgar
  • 5,184
  • 3
  • 37
  • 45
  • 1
    Again, seems inefficient to filter the list every single time you want to lookup a single index? – G_M Jul 31 '18 at 16:11
  • @G_M Friendly one-liner. Performance is a concern? It can be reused: `beF = [w for w in be if w != '']` – lashgar Jul 31 '18 at 16:15
  • What if there were millions of indexes in `extra`? As shown in your comment, it's such a simple fix in this case that "premature optimization" concerns seem odd. – G_M Jul 31 '18 at 16:19