Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Remove items from a list

Reply
Thread Tools

Remove items from a list

 
 
Stan Cook
Guest
Posts: n/a
 
      09-08-2004
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....

signed
.. . . . . at the end of my rope....!


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date: 9/1/04
 
Reply With Quote
 
 
 
 
Dan Perl
Guest
Posts: n/a
 
      09-08-2004
What is the content of _dbases? How do you create that list? If I
understand correctly, it is a list of file names that you may have gotten
with os.listdir( ).

And I want to make sure I understand the problem. Are you trying to remove
the names from the list or are you trying to remove the files themselves?
Just making sure that it's not the latter...

Can you put more in your example, something that I may be able to run and
see the results?

Dan

"Stan Cook" <> wrote in message
news:ysv%c.32876$...
I was trying to take a list of files in a directory and remove all but the
".dbf" files. I used the following to try to remove the items, but they
would not remove. Any help would be greatly appreciated.

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....

signed
.. . . . . at the end of my rope....!


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date: 9/1/04


 
Reply With Quote
 
 
 
 
Stan Cook
Guest
Posts: n/a
 
      09-08-2004
Yes, I used the listdir. The list is a list of files in the
directory. I want to filter everything out but the ".dbf"
files.


"Dan Perl" <> wrote in message
news:KOv%c.104$ gers.com...
: What is the content of _dbases? How do you create that
list? If I
: understand correctly, it is a list of file names that you
may have gotten
: with os.listdir( ).
:
: And I want to make sure I understand the problem. Are you
trying to remove
: the names from the list or are you trying to remove the
files themselves?
: Just making sure that it's not the latter...
:
: Can you put more in your example, something that I may be
able to run and
: see the results?
:
: Dan
:
: "Stan Cook" <> wrote in message
: news:ysv%c.32876$...
: I was trying to take a list of files in a directory and
remove all but the
: ".dbf" files. I used the following to try to remove the
items, but they
: would not remove. Any help would be greatly appreciated.
:
: x = 0
: for each in _dbases:
: if each[-4:] <> ".dbf":
: del each # also tried: del
_dbases[x]
: x = x + 1
:
: I must be doing something wrong, but it acts as though it
is....
:
: signed
: . . . . . at the end of my rope....!
:
:
: ---
: Outgoing mail is certified Virus Free.
: Checked by AVG anti-virus system (http://www.grisoft.com).
: Version: 6.0.749 / Virus Database: 501 - Release Date:
9/1/04
:
:


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date:
9/1/04


 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      09-08-2004
"Stan Cook" <> wrote in message
news:yWv%c.33126$...
> Yes, I used the listdir. The list is a list of files in the
> directory. I want to filter everything out but the ".dbf"
> files.
>


You said the answer yourself - "I want to _filter_ everything out but the
".dbf" files."

Use filter built-in, and use str's endswith() method in place of [-4:] list
slicing.

dirlist = [ "a.txt", "b.txt", "c.dbf", "d.txt", "e.dbf" ]
isdbf = lambda x : x.endswith(".dbf")
print filter( isdbf, dirlist )

gives:

['c.dbf', 'e.dbf']


-- Paul


 
Reply With Quote
 
Leif K-Brooks
Guest
Posts: n/a
 
      09-08-2004
Stan Cook wrote:
> I was trying to take a list of files in a directory and remove all but the ".dbf" files.


Assuming the variable "files" is the list of files:

files = [fname for fname in files if fname.endswith('.dbf')]
 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      09-08-2004
Paul McGuire <._bogus_.com> wrote:
> "Stan Cook" <> wrote in message
> news:yWv%c.33126$...
> > Yes, I used the listdir. The list is a list of files in the
> > directory. I want to filter everything out but the ".dbf"
> > files.
> >

>
> You said the answer yourself - "I want to _filter_ everything out but
> the ".dbf" files."
>
> Use filter built-in, and use str's endswith() method in place of [-4:]
> list slicing.
>
> dirlist = [ "a.txt", "b.txt", "c.dbf", "d.txt", "e.dbf" ]
> isdbf = lambda x : x.endswith(".dbf")
> print filter( isdbf, dirlist )
>
> gives:
>
> ['c.dbf', 'e.dbf']


Off topic... but if OP is interested in shell solution comparable to
above, then there are two I can offer:

dirlist=( a.txt b.txt c.dbf d.txt e.dbf )

1. echo ${dirlist[*]|/*.dbf}

2. func () {
[[ $1 == *.dbf ]]
}
arrayfilter func dirlist

3. for i in ${dirlist[*]}; do
[[ $1 == *.dbf ]] && echo $i
done

The first is shell version of Python's list comprehension, the second is
shell version of Python's filter(), and the third is standard loop
solution.

Ref:
http://freshmeat.net/projects/bashdiff/
help '${var|'
help arrayfilter

--
William Park <>
Open Geometry Consulting, Toronto, Canada
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      09-08-2004
"Stan Cook" <> writes:

> for each in _dbases:
> if each[-4:] <> ".dbf":


List comprehension to the rescue!

_dbases = [each for each in _dbases if each[-4:] == ".dbf"]
 
Reply With Quote
 
Egbert Bouwman
Guest
Posts: n/a
 
      09-08-2004
On Wed, Sep 08, 2004 at 03:59:26AM +0000, Stan Cook wrote:
> I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.
>
> x = 0
> for each in _dbases:
> if each[-4:] <> ".dbf":
> del each # also tried: del _dbases[x]
> x = x + 1
>
> I must be doing something wrong, but it acts as though it is....
>

The answers you received don't tell you what you are doing wrong.
If you replace 'del each' with 'print each' it works,
so it seems that you can not delete elements of a list you are
looping over. But I would like to know more about it as well.
egbert
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
 
Reply With Quote
 
Mel Wilson
Guest
Posts: n/a
 
      09-08-2004
In article <mailman.3029.1094638477.5135.python->,
Egbert Bouwman <> wrote:
>On Wed, Sep 08, 2004 at 03:59:26AM +0000, Stan Cook wrote:
>> I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.
>>
>> x = 0
>> for each in _dbases:
>> if each[-4:] <> ".dbf":
>> del each # also tried: del _dbases[x]
>> x = x + 1
>>
>> I must be doing something wrong, but it acts as though it is....
>>

>The answers you received don't tell you what you are doing wrong.
>If you replace 'del each' with 'print each' it works,
>so it seems that you can not delete elements of a list you are
>looping over. But I would like to know more about it as well.


One use of `del` is to remove a name from a namespace,
and that's what it's doing here: removing the name 'each'.

A paraphrase of what's going on is:

for i in xrange (len (_dbases)):
each = _dbases[i]
if each[-4:] <> ".dbf":
del each

and we happily throw away the name 'each' without touching
the item in the list.

The way to remove items from a list is (untested code):

for i in xrange (len (a_list)-1, -1, -1):
if i_want_to_remove (a_list[i]):
del a_list[i]

Going through the list backwards means that deleting an item
doesn't change the index numbers of items we've yet to
process. `del a_list[i]` removes from the list the
reference to the object that was the i'th item in the list
(under the hood, a Python list is implemented as an array of
references.)

This is one reason list comprehensions became popular so
fast.

Regards. Mel.
 
Reply With Quote
 
John Lenton
Guest
Posts: n/a
 
      09-08-2004
On Wed, Sep 08, 2004 at 03:59:26AM +0000, Stan Cook wrote:
> I was trying to take a list of files in a directory and remove all
> but the ".dbf" files. I used the following to try to remove the
> items, but they would not remove. Any help would be greatly
> appreciated.


any reason you aren't doing glob.glob('*.dbf')?

--
John Lenton () -- Random fortune:
Has everybody got HALVAH spread all over their ANKLES?? ... Now, it's
time to "HAVE A NAGEELA"!!

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBPwbygPqu395ykGsRAk+FAKCNb59yaVFmbjQp5kcfYu a4VubgUwCdFIcC
th0XRO992Xf3EUoW/vMkSD4=
=F7+O
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to retrieve listbox items on postback , items moved usingjavascript between 2 list boxes (source and target ) divya ASP .Net 1 05-28-2008 05:27 AM
Is there any way to append some items to List box, without retrieving all items through AJAX? Anjan Bhowmik ASP .Net 1 02-14-2008 09:02 PM
Remove Items from List Box Lit ASP .Net 2 07-27-2007 03:22 PM
How to remove items from add/remove list please Caractucus Potts Computer Support 5 07-03-2005 10:31 PM
grouping items among a list according to items subtag value Gilles Kuhn XML 0 09-15-2003 12:01 PM



Advertisments