Stupid loop help por favor

This is the place for queries that don't fit in any of the other categories.

Stupid loop help por favor

Postby bbesase » Tue May 14, 2013 5:12 pm

So I have this loop to cycle through a list, and I need it to go through every item in the list and determine if it ends in a certain way or not and then do something. Here is my code.

Code: Select all
def cut(path):
    newList = []
    n = 0
    #test = getUnitTest(path)
    test =str(foundfiles[n])
    for lines in foundfiles[n]:
        if not test.endswith('.UnitTests.vbproj'):
            print test
            n=n+1
    else:
        if test.endswith('.UnitTests.vbproj'):
            del test
            n=n+1
    return newList


It doesn't go to every item in the list, it stop after about 3 iterations when there are over 25 in the list. The items in the list are as of this format: blah.blah.blah.UnitTests.vbproj with sometimes more after the vbproj part. I just can't get it looping right. any help would be greatly appreciated, and thank you in advance!
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: Stupid loop help por favor

Postby metulburr » Tue May 14, 2013 5:58 pm

Code: Select all
    for lines in foundfiles[n]:
        if not test.endswith('.UnitTests.vbproj'):
            print test
            n=n+1
    else:
        if test.endswith('.UnitTests.vbproj'):
            del test
            n=n+1


I dont believe i have seen someone use an else clause with the for loop since first learning python. I thought it was weird when i switched from c++ to python and i still think it is weird. I havent seen it since some book i read back when used it for an example. I havent seen anyone use it since and I would never use.

I believe your goal is loop a list getting each index.endsiwith() ?
Code: Select all
my_list = [
   'test',
   'tester',
   'testing',
   'testify'
]

for i in my_list:
   if i.endswith('er'):
      print('{} ends with {}'.format(i, 'er'))
   else:
      print('{} does not end in er'.format(i))


However i see that you are del the index. If you are going to delete an index from a list, you loop through a copy of that list instead, and delete from the original list:
Code: Select all
my_list = [
   'test',
   'tester',
   'testing',
   'testify'
]

for i in my_list[:]:
   if i.endswith('er'):
      print('{} ends with {}'.format(i, 'er'))
   else:
      print('deleting {} , does not end in er'.format(i))
      my_list.remove(i)

print(my_list)


example making a copy of the list
Code: Select all
>>> my_list = [
...     'test',
...     'tester',
...     'testing',
...     'testify'
... ]
>>> my_list
['test', 'tester', 'testing', 'testify']
>>> b = my_list[:]
>>> b
['test', 'tester', 'testing', 'testify']
>>> b.append('t')
>>> my_list
['test', 'tester', 'testing', 'testify']
>>> b
['test', 'tester', 'testing', 'testify', 't']
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1321
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Stupid loop help por favor

Postby Kebap » Tue May 14, 2013 6:17 pm

I would do something like this
Code: Select all
mylist = ["blah.blah.blah.UnitTests.vbproj",
"blah.blah.blah.UnitTests.vbproj",
"blah.blah.blah.UnitTests.vbproj with sometimes more after the vbproj part",
"blah.blah.blah.UnitTests.vbproj with sometimes more after the vbproj part",
"blah.blah.blah.UnitTests.vbproj with sometimes more after the vbproj part"]

mynewlist = [s for s in mylist if not s.endswith('.UnitTests.vbproj')]
for m in mynewlist:
  print m

Depends on what you really want to do in the end actually. :mrgreen: You did not write much about that. Describe the goal, not the step
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Stupid loop help por favor

Postby bbesase » Tue May 14, 2013 6:50 pm

Code: Select all
def cut(path):

    test =str(foundfiles)
    newList = [s for s in foundfiles if not test.endswith('UnitTests.vbproj')]
    for m in newList:
        print m

return newList


This is my function now, it works except it doesnt get rid of the ones with more than 'UnitTests.vbproj' it keeps them
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: Stupid loop help por favor

Postby Kebap » Tue May 14, 2013 8:09 pm

Correct. As expected, I did not solve your whole problem, because I don't know your whole problem. I have looked through your post history and you seem to open many short threads for what seems to be all part of the same big problem somehow. You are just a python beginner, I understand this, still you could maybe get more help if you read a short python tutorial and/or learn how to ask smart questions.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Stupid loop help por favor

Postby micseydel » Tue May 14, 2013 8:24 pm

metulburr wrote:
Code: Select all
    for lines in foundfiles[n]:
        if not test.endswith('.UnitTests.vbproj'):
            print test
            n=n+1
    else:
        if test.endswith('.UnitTests.vbproj'):
            del test
            n=n+1


I dont believe i have seen someone use an else clause with the for loop since first learning python. I thought it was weird when i switched from c++ to python and i still think it is weird. I havent seen it since some book i read back when used it for an example. I havent seen anyone use it since and I would never use.

I have used it once, and I was very happy to have it. Here though, you could throw out the "else:" line and just dedent the rest, so there's no good reason to have it.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1132
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Stupid loop help por favor

Postby metulburr » Tue May 14, 2013 9:27 pm

I have used it once, and I was very happy to have it

i would be interested in seeing that snippet of code
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1321
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Stupid loop help por favor

Postby micseydel » Tue May 14, 2013 9:40 pm

I can't find the code, but I know I used that language feature when I needed to detect whether I was breaking in a for loop or exiting because the thing I was iterating over was exhausted. The alternative way to do it is a flag variable. It's one or two more lines, and I think takes longer to read than the else clause if you know what it does.

Kinda funny that GvR thought it was worthwhile though, considering he rejects the idea of a multi-level break, saying it's not something anyone would use often enough....
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1132
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests