简体   繁体   中英

Python- Get the first and last item of Sequence

I got this sorted list of expired OSM Tilenames from a CSV File:

15,17485,11075
15,17485,11076
15,17485,11077
15,17485,11078
15,17485,11079
15,17485,11080
15,17486,11068
15,17486,11069
15,17486,11070
15,17486,11071
15,17486,11072
15,17486,11073
15,17486,11074
15,17486,11075
15,17486,11076
15,17486,11077
15,17486,11078
15,17486,11079
15,17486,11080
15,17487,11068
15,17487,11069
15,17487,11070
15,17487,11071
15,17487,11072
15,17487,11073
15,17487,11074
15,17487,11075
15,17487,11076
15,17487,11077
15,17487,11078
15,17487,11079

I want to get the first and Last item of each sequence, in the third column and corresponding entry in the second column, to create a Bounding Box for rendering with mapnik. I don't want to use mod_tile.

I got no Problems with extracting from the second column:

for x_idx, row in enumerate(zoom_15):
    this_Xelement = row
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)]
    X = int(next_Xelement[1]) - int(this_Xelement[1])
    x_start = 0
    x_end = 0
    y_end = 0
    y_start = int(this_Xelement[2])
    if X == 0:
        continue
    elif X == 1:
        x_start = int(this_Xelement[1])
        x_end = int(next_Xelement[1])
    elif X < 0:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1
    elif X > 1:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1

    print (x_start, x_end)
    print "++++++++++++++++"

creating some output like:

在此处输入图片说明

But i can't get the third column iterated right, to get the right coordinates of the BB. Im'working on Python 2.7

Update:

I want to get of everey sequence the first and last entry in it. so out of this:

15,17485,11075
15,17485,11076
15,17485,11077
15,17485,11078
15,17485,11079
15,17485,11080

i want to get:

17485,11075
17485,11080

I'm not sure if I fully understand your question, but I think you can do something like this:

firstRow = None
lastRow = None
with open('csvfile.csv','r') as file:
   for line in file:
      line = line.strip()
      if len(line)==0: 
          continue # ignore empty lines, in case they exist
      if firstRow is None:
          firstRow = line.strip() # will only be updated once (in the first row)
      lastRow = line.strip() # will be replaced every time (and only the last row will survive, in the end)


for line in [firstRow,lastRow]:
      items = line.split(',')
      second_item = int(items[1])
      third_item = int(items[2])
      print (second_item,third_item)

or using the same strategy in your code:

firstRow = None
lastRow = None
for x_idx, row in enumerate(zoom_15):           
    this_Xelement = row
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)]
    X = int(next_Xelement[1]) - int(this_Xelement[1])
    x_start = 0
    x_end = 0
    y_end = 0
    y_start = int(this_Xelement[2])
    if X == 0:
        continue
    elif X == 1:
        x_start = int(this_Xelement[1])
        x_end = int(next_Xelement[1])
    elif X < 0:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1
    elif X > 1:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1
    if firstRow is None:
        firstRow = (x_start, x_end)
    lastRow = (x_start, x_end)

print firstRow 
print lastRow
print "++++++++++++++++"

I suggest using the pandas package for this, it has a read_csv function, and it has some strong array/table manipulation tools.

Code example:

import pandas as pd

data = pd.read_csv(myCsvFile, header=None)
print(data.loc[:, 1:3]) # get all rows, columns >= 1 and < 3

which results in:

        1      2
0   17485  11075
1   17485  11076
2   17485  11077
3   17485  11078
...
27  17487  11076
28  17487  11077
29  17487  11078
30  17487  11079
for x_idx, row in enumerate(zoom_15):
    this_Xelement = row
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)]
    X = int(next_Xelement[1]) - int(this_Xelement[1])
    if X == 0:
        continue
    elif X == 1:
        x_start = int(this_Xelement[1])
        x_end = int(next_Xelement[1])
        y_end = int(this_Xelement[2]) + 1
        y_start = int(next_Xelement[2])
    elif X < 0:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1
        y_end = int(this_Xelement[2]) + 1
    elif X > 1:
        x_start = int(this_Xelement[1])
        x_end = int(this_Xelement[1]) + 1
        y_end = int(this_Xelement[2]) + 1
        y_start = int(next_Xelement[2])

    print(x_start, y_start)
    print(x_end, y_end)
    print "+++++++++++"

this is creating waht i want:

在此处输入图片说明

got with the help of my wonderful fellow students. Thanks for your help !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM