Python Forum
deleting certain rows from multidimensional list
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting certain rows from multidimensional list
#2
Your sample data:

csv_string='''type;latitude;longitude;speed
T;37.547575;15.143184;0.963040
T;37.547569;15.143185;0.833400
T;37.547565;15.143186;0.351880
T;37.547561;15.143194;0.629680
T;37.547560;15.143205;1.129720
T;37.547561;15.143222;1.259360
T;37.547562;15.143236;1.129720'''
The order of steps in your program:
  1. Read a line
  2. Convert data of the line
  3. Make decision if data is in boundaries
  4. If data is in boundaries -> yield data


Just as an example, don't use this code:
import csv
from functools import partial
import io


def in_boundaries(lat, lon, lat_min, lat_max, lon_min, lon_max):
    return lat_min < lat < lat_max and lon_min < lon < lon_max 

# your boundaries
boundaries = {
    'lat_min': 37.547560,
    'lat_max': 37.547570,
    'lon_min': 15.143184,
    'lon_max': 15.143186,
    }

# functional approach, using keyword unpacking
my_boundaries = partial(in_boundaries, **boundaries)


csv_string = '''type;latitude;longitude;speed
T;37.547575;15.143184;0.963040
T;37.547569;15.143185;0.833400
T;37.547565;15.143186;0.351880
T;37.547561;15.143194;0.629680
T;37.547560;15.143205;1.129720
T;37.547561;15.143222;1.259360
T;37.547562;15.143236;1.129720'''

file_like_csv = io.StringIO(csv_string)

reader = csv.reader(file_like_csv, delimiter=';')
header = next(reader)
#print(header)

for row in reader:
    # the part where you split your row into columns
    try:
        type, latitude, longitude, speed = row
    except ValueError:
        print('Invalid column length:', row)
        continue
    try:
        # convert lat, lon to float
        latitude, longitude = map(float, [latitude, longitude])
    except ValueError:
        print('Wrong value in:', row)
        continue
    # now decide if your data is in your defined boundary
    if my_boundaries(latitude, longitude):
        print(row)
        # here you should do something with your valid data
    else:
        # you don't need this.
        # this is just for control
        # print('Not in boundary:', row)
        pass
At the end it's very easy. Make a function which decides if your data is in your boundaries and do something with it.
No need to make this strange nested iteration.

What you need to know:
  • Exception handling
  • argument unpacking
  • keyword argument unpacking, if you're working with dicts. Not necessary.
  • functions
  • use of csv module
  • knowledge of iterables, csv.reader is an iterator
  • comparison with more than one value (min_val < val < max_val)
  • logical operations: and, or, not
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: deleting certain rows from multidimensional list - by DeaD_EyE - Nov-02-2017, 04:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting rows based on cell value in Excel azizrasul 11 2,760 Oct-19-2022, 02:38 AM
Last Post: azizrasul
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,652 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  deleting select items from a list Skaperen 13 4,675 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,149 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,254 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Deleting employee from list SephMon 3 3,322 Jan-05-2021, 04:15 AM
Last Post: deanhystad
  Counting Element in Multidimensional List quest_ 1 2,156 Nov-25-2020, 10:00 PM
Last Post: quest_
  Choose an element from multidimensional array quest_ 2 2,679 Nov-25-2020, 12:59 AM
Last Post: quest_
  Jelp with a multidimensional loop Formationgrowthhacking 1 1,879 Jan-27-2020, 10:05 PM
Last Post: micseydel
  Sort MULTIDIMENSIONAL Dictionary mirinda 2 4,945 Apr-05-2019, 12:08 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020