OESF | ELSI | pdaXrom | OpenZaurus | Zaurus Themes | Community Links | Ibiblio

IPB

Welcome Guest ( Log In | Register )

> Loops And Such
bam
post Aug 19 2006, 12:13 AM
Post #1





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



I have this so far:
CODE
for item in dupes[]:
  mydupeitem=item
  for item in filter[]:
     if mydupeitem[2:] = item:
        pass
     else
        write......etc.



now is there anything wrong with this? it wipes out my entire bash history, so it never gets to the write statement. filter[] is just a list of filters(strings) and dupes[] is just a list of strings as well, basically i an 'cleaning' my bash history at logoff, I have a remove duplicates script running and it sorts but I would like to remove any "cd blahblah" and rm's mv's etc. any ideas, I am new to python, so any help would be great.
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
kopsis
post Aug 19 2006, 06:14 PM
Post #2





Group: Members
Posts: 329
Joined: 1-July 04
Member No.: 3,880



QUOTE(bam @ Aug 19 2006, 03:13 AM)
I have this so far:
CODE
for item in dupes[]:
  mydupeitem=item
  for item in filter[]:
     if mydupeitem[2:] = item:
        pass
     else
        write......etc.

*


A few observations:

* Your "if mydupeitem..." line is using an assignment (=) instead of a compare (==).
* You're reusing your loop variable (item) in the inner loop. I'm not sure Python cares (I'm in the habit of not trying such things), but in other languages like C that would cause much breakage. Note: "item" is just a variable that gets set on each loop iteration. You can use any variable name there that you wish.
* I don't understand the empty [] brackets on the "for item ..." lines. They should cause syntax errors.

A more elegant solution (assuming you're using a recent version of Python would be something like:
CODE
def f(x):
   for item in filter:
       if x[2:] = item:
           return false
   return true

filtered_list = filter(f, dupes)


That will run f(x) on every element in dupes and set filtered_list equal to the list of those for which f(x) returned true (i.e. they did not match any items in the filter list).
Go to the top of the page
 
+Quote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 20th August 2014 - 10:31 AM