5 Replies - 835 Views - Last Post: 14 July 2012 - 07:46 PM Rate Topic: -----

#1 cannesMajor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-June 12

removing a list from a list

Posted 25 June 2012 - 06:36 PM

I have a list of lists. Each individual list contains (name, latitude, longitude, boundary). I'm trying to figure out a way to remove a user specified list from the list of lists. So, for example, my list of lists contains [(test1, 38.3, 77.4, .2), (test2, 39.5, 77.8, .05)...] If the user wants to remove "test2" how can I identify that list from the list of lists? I have tried the below but it doesn't work:

def fence_remover():
    global fences
    name = input("Which fence do you want to remove? ")
    if name in fences:
        fences.remove(name)


*** Edit ***
Please use code tags when posting code.
Posted Image

This post has been edited by GunnerInc: 25 June 2012 - 06:43 PM
Reason for edit:: Removed broken font tags, added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: removing a list from a list

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: removing a list from a list

Posted 25 June 2012 - 07:45 PM

Just to clear it up, you have a list of tuples...not a list of lists, but now I'm being a little nitpicky.

As for your code, you need to search each tuple for the tester. This is a command line Python program I developed. It worked nicely with me.
>>> fences = [('test1', 38.3, 77.4, .2), ('test2', 39.5, 77.8, .05)]
>>> name = 'test2'
>>> for tup in fences:
        if name in tup:
	    fences.remove(tup))



Let me know if you have any questions.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: removing a list from a list

Posted 25 June 2012 - 09:23 PM

Do NOT modify a list while iterating over it. It might have looked like it worked for your input, but if you change the first line to something like
fences = [('test1', 38.3, 77.4, .2), ('test2', 39.5, 77.8, .05), ('test2', 38.3, 77.4, .2), ('test3', 39.5, 77.8, .05)]
you'll notice that it doesn't actually work correctly. It will leave the second test2-fence in the list because it will skip over it while iterating.

The best way to remove items from a list is to use a list comprehension like this:

fences = [fence for fence in fences if fence[0] != name]


This list comprehensions creates a new list by iterating over the old list and including a fence if and only if its name is different than the name the user entered. Note that I used fence[0] != name instead of not (name in fence) because we know the name will be the first element of the tuple and it helps to be specific about these kinds of things.

PS: It would probably make sense to create a Fence class to represent each fence instead of using a tuple. Then you could write fence.name != name instead of fence[0] != name.

PPS: The fences list should probably be an argument to the function, not a global variable.
Was This Post Helpful? 3
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: removing a list from a list

Posted 26 June 2012 - 02:50 PM

Aye, list comprehensions are a much better way to do that. Thanks, sepp2k for that! I need to think more Pythonically...

Also, I made the assumption of only unique valued names for some strange reason. Otherwise I would not have modified a list while iterating over it. But, that;s the wrong assumption.

Good solution.
Was This Post Helpful? 0
  • +
  • -

#5 cannesMajor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-June 12

Re: removing a list from a list

Posted 14 July 2012 - 06:29 PM

Thanks for the help guys. just FYI, I'm learning python with an attempt to write a program that simulates a GPS data stream, compares the "current position" of a fictitious vehicle, compares the position against named geofences, and reports the results. Later I included some functionality that warns if my "car" travels over a defined speed limit. All of this is in response to my discovery of Telit's GSM/GPS modules. (www.telit.com). Here's what I came up with. The above question was in reference to my second attempt, which is to make the below more modular/object oriented.

#!/usr/local/bin/python
#monitors the current position of the gps unit and reports when the unit
#enters/exits predefined geofences ("zones")

import math
import time
import re    #used for the stopTimer functionality

tracklog = []
fences = []
rmclist = ["$GPRMC,161229.000,A,3857.9760,N,07723.4240,W,0.13,309.62,121111,,*10",
           "$GPRMC,161431.000,A,4557.9810,N,07723.3390,W,20.1,309.62,121111,,*10",
           "$GPRMC,161633.000,A,4558.1200,N,07723.2700,W,21.5,309.62,121111,,*10",
           "$GPRMC,161835.000,A,4558.1280,N,07723.1740,W,24.4,309.62,121111,,*10",
           "$GPRMC,161936.000,A,4558.8400,N,07723.0340,W,24.4,309.62,121111,,*10",
           "$GPRMC,162037.000,A,4557.8420,N,07723.0370,W,0.00,309.62,121111,,*10",
           "$GPRMC,162637.000,A,4557.8420,N,07723.0370,W,0.00,309.62,121111,,*10",
           "$GPRMC,162739.000,A,4557.8620,N,07722.9940,W,14.8,309.62,121111,,*10",
           "$GPRMC,162941.000,A,4557.7420,N,07722.9080,W,27.0,309.62,121111,,*10",
           "$GPRMC,163343.000,A,4557.6500,N,07722.7390,W,32.8,309.62,121111,,*10",
           "$GPRMC,163545.000,A,4557.5360,N,07722.4720,W,38.3,309.62,121111,,*10",
           "$GPRMC,163747.000,A,4557.4420,N,07722.2770,W,37.8,309.62,121111,,*10",
           "$GPRMC,163949.000,A,4557.2840,N,07722.0840,W,0.00,309.62,121111,,*10",
           "$GPRMC,164151.000,A,4557.2330,N,07721.7880,W,18.6,309.62,121111,,*10",
           "$GPRMC,164353.000,A,4557.2500,N,07721.5490,W,27.5,309.62,121111,,*10",
           "$GPRMC,164555.000,A,4557.2720,N,07721.5530,W,0.00,309.62,121111,,*10",
           "$GPRMC,164757.000,A,4557.3420,N,07721.5540,W,0.18,309.62,121111,,*10"]
rlat = 0.0
rlon = 0.0
stat = ""
stopCounter = 0
notifyFlag = 0

data = open("fences.txt")
for each_line in data:
    (name, lat, lon, boundary) = each_line.split(",")
    lat = float(float(lat)/57.29577951)
    lon = float(float(lon)/57.29577951)
    boundary = float(boundary)
    fence = (name, lat, lon, boundary)
    fences.append(fence)
data.close

def gps():
    #formats and parses incoming gps sentences 
    global ctime, rlat, rlon, lat, lon, speed, bearing, tracklog
    nmea = rmclist.pop(0)
    (header, utm, status, lat, NS, lon, EW, speed, bearing, date, magvar, checksum) = nmea.split(",")
    ctime = utm[:2]+":"+ utm[2:4] + ":" + utm[4:6]
    latd = int(lat[:2])
    latm = float(lat[2:9])
    lond = int(lon[:3])
    lonm = float(lon[3:10])
    lat = round((latd + latm/60), 4)
    lon = round((lond + lonm/60), 4)
    rlat,rlon = map(math.radians, [lat, lon])
    #rlon = float(lon/57.29577951)
    speed = int(float(speed)*1.15077945)
    new_entry = (ctime, lat, lon)
    tracklog.append(new_entry)
    return rlat, rlon, ctime, lat, lon, speed, bearing

def status():
    #get current position and compare it with all geofences, reporting results
    global stat, notifyFlag
    for fence in fences:
        name = fence[0]
        glat = fence[1]
        glon = fence[2]
        boundary = fence[3]
        x = (glon-rlon)*math.cos((glat+rlat)/2)
        y = (glat-rlat)
        d = math.sqrt(x*x+y*y) * 3958.756
        if d < boundary:
            stat = name
            notifyFlag += 1
            break
        elif stat == name and d > boundary:
            stat = "leaving", name
            notifyFlag = 0
            break
        else:
            stat = "roaming"
    if stat == name and notifyFlag < 2:
        print "\n", ctime, "300SD is at", name
    elif stat == ("leaving", name):
        print "\n", ctime + " 300SD is leaving", name + "; traveling at", speed, "mph; heading", bearing, "degrees"
        

for i in range(len(rmclist)):
    gps()
    status()
    if speed > 35:
        print "\n", ctime, "300SD is speeding! Currently travelling at", speed, "mph"
    if speed != 0:
        stopCounter = 0
    if speed == 0 and stopCounter == 0:
        stopCounter += 1
        stopTimer = int(re.sub("\D", "", ctime))
        startTime = stopTimer
    if speed == 0 and stopCounter != 0 and (stopTimer - startTime) > 510:
        print "\n", ctime, "300SD has stopped at", lat, "N", lon, "W"
    time.sleep(2)


Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: removing a list from a list

Posted 14 July 2012 - 07:46 PM

Wow, quite the ambitious project! That's great
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1