I'm using Pyshp to extract information from a shapefile. Using the the shapefile.Reader() program, I am able to get the following output:
[('DeletionFlag', 'C', 1, 0), ['Id', 'N', 11, 0], ['Hrapx', 'N', 6, 0], ['Hrapy','N', 6, 0], ['Lat', 'N', 7, 4], ['Lon', 'N', 9, 4], ['Globvalue', 'N', 16, 2]]
How can I extract all of the values for the last index in the list (i.e., 'Globvalue').
This should also be easy to extract the Lat and Lon fields too right?
I'm assuming it is something along the lines of points = shaperecs[:].shape.points[:] but I have been spending hours trying to figure out the exact syntax.
Any help would be great!
I was able to solve this by using basemap (which uses Pyshp):
val = []
s = m.readshapefile('last_500','last_500')
for shapedict in m.last_500_info:
val.append(shapedict['fieldname'])
print val
Related
I have a large database with several columns, i need data from 2 of these.
The end result is to have 2 drop down menus where the first one sets "names" and the second one is the "numbers" values that has been merged into the name. I just need the data available so i can input it into another program.
So a list or dictionary that contains the Unique values of the "names" list, with the numbers from the numbers list appended to them.
# Just a list of random names and numbers for testing
names = [
"Cindi Brookins",
"Cumberband Hamberdund",
"Roger Ramsden",
"Cumberband Hamberdund",
"Lorean Dibble",
"Lorean Dibble",
"Coleen Snider",
"Rey Bains",
"Maxine Rader",
"Cindi Brookins",
"Catharine Vena",
"Lanny Mckennon",
"Berta Urban",
"Rey Bains",
"Roger Ramsden",
"Lanny Mckennon",
"Catharine Vena",
"Berta Urban",
"Maxine Rader",
"Coleen Snider"
]
numbers = [
6,
5,
7,
10,
3,
9,
1,
1,
2,
7,
4,
2,
8,
3,
8,
10,
4,
9,
6,
5
]
So in the above example "Berta Urban" would appear once, but still have the numbers 8 and 9 assigned, "Rey Bains" would have 1 and 3.
I have tried with
mergedlist = dict(zip(names, numbers))
But that only assigns the last of the numbers to the name.
I am not sure if i can make a dictionary with Unique "names" that holds multiple "numbers".
You only get the last number associated with each name because dictionary keys are unique (otherwise they wouldn't be much use). So if you do
mergedlist["Berta Urban"] = 8
and after that
mergedlist["Berta Urban"] = 9
the result will be
{'Berta Urban': 9}
Just as if you did:
berta_urban = 8
berta_urban = 9
In that case you would expect the value of berta_urban to be 9 and not [8,9].
So, as you can see, you need an append not an assignment to your dict entry.
from collections import defaultdict
mergedlist = defaultdict(list)
for (name,number) in zip(names, numbers): mergedlist[name].append(number)
This gives:
{'Coleen Snider': [1, 5],
'Cindi Brookins': [6, 7],
'Cumberband Hamberdund': [5, 10],
'Roger Ramsden': [7, 8],
'Lorean Dibble': [3, 9],
'Rey Bains': [1, 3],
'Maxine Rader': [2, 6],
'Catharine Vena': [4, 4],
'Lanny Mckennon': [2, 10],
'Berta Urban': [8, 9]
}
which is what I think you want. Note that you will get duplicates, as in 'Catharine Vena': [4, 4] and you will also get a list of numbers for each name, even if the list has only one number in it.
You cannot have multiple keys of the same name in a dict, but your dict keys can be unique while holding a list of matching numbers. Something like:
mergedlist = {}
for i, v in enumerate(names):
mergedlist[v] = mergedlist.get(v, []) + [numbers[i]]
print(mergedlist["Berta Urban"]) # prints [8, 9]
Not terribly efficient, tho. In dependence of the datatbase you're using, chances are that the database can get you the results in the form you prefer faster than you post-processing and reconstructing the data.
The user input string follows the pattern:
{ 1, 2, 4, 6, 3 }
{ 2, 5, 8, 0, 3, 45, 5 }
How to convert any one of the given user input to list of integers.
Maybe split can be used as in A.split(', ') but then we get,
A = ['{ 1', '2', '4', '6', '3 }']
but the answer, we want should be,
A = [1, 2, 4, 6, 3]
replace {} to [] and use json module to parse:
>>> import json
>>> s = "{ 1, 2, 4, 6, 3 }"
>>> json.loads(s.replace("{","[").replace("}","]"))
[1, 2, 4, 6, 3]
The easy way would be to strip the parentheses, split on commas and cast the types.
A = [int(x) for x in A.lstrip(' (').rstrip(' )').split(', ')]
The proper way to do this is the same way a compiler would do it: tokenize the input before parsing, using a library like PLY.
(Also, you should make an effort to do your own homework and learn to write proper English.)
You can leverage try and except to accomplish this. Basically you try to convert each character of your string to int, if successful then you append it to a list if Python raises an error -because cannot convert to int- then nothing will be appended. Maybe this is not the shortest code but it is definitely very readable.
s = '{ 1, 2, 4, 6, 3 }'
result = []
for item in s:
try:
result.append(int(item))
except:
pass
print(result)
>>>[1, 2, 4, 6, 3]
I am trying to write a dictionary of lists to a csv file. The solution in:Write dictionary of lists to a CSV file
will trim longer lists. For example if I want to write the following:
d = {"key1": [1, 2, 3], "key2": [4, 5, 6], "key3": [7, 8, 9, 11]}
with open("test.csv", "wb") as outfile:
writer = csv.writer(outfile)
writer.writerow(d.keys())
writer.writerows(zip(*d.values()))
The results is
key3 key2 key1
7 4 1
8 5 2
9 6 3
11 is deleted from key3. Any ideas?
The quick and easy answer is to use itertools.izip_longest instead of zip
import itertools
import csv
d = {"key1": [1, 2, 3], "key2": [4, 5, 6], "key3": [7, 8, 9, 11]}
with open("test.csv", "wb") as outfile:
writer = csv.writer(outfile)
writer.writerow(d.keys())
writer.writerows(itertools.izip_longest(*d.values()))
Say I have a dict of country -> [cities] (potentially an ordered dict):
{'UK': ['Bristol', 'Manchester' 'London', 'Glasgow'],
'France': ['Paris', 'Calais', 'Nice', 'Cannes'],
'Germany': ['Munich', 'Berlin', 'Cologne']
}
The number of keys (countries) is variable: and the number of elements cities in the array, also variable. The resultset comes from a 'search' on city name so, for example, a search on "San%" could potentially meet with 50k results (on a worldwide search)
The data is to be used to populate a select2 widget --- and I'd like to use its paging functionality...
Is there a smart way to slice this such that [3:8] would yield:
{'UK': ['Glasgow'],
'France': ['Paris', 'Calais', 'Nice', 'Cannes'],
'Germany': ['Munich']
}
(apologies for the way this question was posed earlier -- I wasn't sure that the real usage would clarify the issue...)
If I understand your problem correctly, as talked about in the comments, this should do it
from pprint import pprint
def slice_dict(d,a, b):
big_list = []
ret_dict = {}
# Make one big list of all numbers, tagging each number with the key
# of the dict they came from.
for k, v in d.iteritems():
for n in v:
big_list.append({k:n})
# Slice it
sliced = big_list[a:b]
# Put everything back in order
for k, v in d.iteritems():
for subd in sliced:
for subk, subv in subd.iteritems():
if k == subk:
if k in ret_dict:
ret_dict[k].append(subv)
else:
ret_dict[k] = [subv]
return ret_dict
d = {
'a': [1, 2, 3, 4],
'b': [5, 6, 7, 8, 9],
'c': [10, 11, 12, 13, 14]
}
x = slice_dict(d, 3, 11)
pprint(x)
$ python slice.py
{'a': [4], 'b': [5, 6], 'c': [10, 11, 12, 13, 14]}
The output is a little different from your example output, but that's because the dict was not ordered when it was passed to the function. It was a-c-b, that's why b is cut off at 6 and c is not cut off
I am doing the algorithm challenges from HackerRank and one of the problems needs me to accept input in the form of strings of numbers formatted as follows:
3 4
12 14 16
1 2
3 4
5 6
Now, I know how to iterate through the lines and assign them where they need to go, but my issue is with the second line. The others are two two digit numbers so I've been extracting them by just referencing their index in the string. For example, the first line of numbers would be collected with string[0] and string[-1].
The second line, however is of indeterminate length, and may include numbers shorter or longer than three digits. How would I pull those out and assign them to variables? I'm sure there is probably a way to do it with RegEx, but I don't know how to assign multiple matches in one string to multiple variables.
import re
print(re.findall(r"(\d+)",x))
"x" being your line.This will return a list with all the number.
You mean this?
>>> import re
>>> s = """3 4
... 12 14 16
... 1 2
... 3 4
... 5 6"""
>>> m = re.findall(r'\b\d+\b', s, re.M)
>>> m
['3', '4', '12', '14', '16', '1', '2', '3', '4', '5', '6']
Just pickup each value in the final list and assign it to variables.
So if s is your string,
map(int, s.split())
yields a list of integers:
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]
That's basically what skamazin suggested.
Given:
>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6'''
If the lines have meaning, you can do:
>>> [map(int, line.split()) for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6]]
If the lines have no meaning, you just want all the digits, you can do:
>>> map(int, txt.split())
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6]
If your source text has the possibility of strings that will not convert to integers:
>>> txt='''\
... 3 4
... 12 14 16
... 1 2
... 3 4
... 5 6
... text that won't be integers
... 99 100 101'''
You can use a conversion function:
>>> def conv(s):
... try:
... return int(s)
... except ValueError:
... return s
...
>>> [[conv(s) for s in line.split()] for line in txt.splitlines()]
[[3, 4], [12, 14, 16], [1, 2], [3, 4], [5, 6], ['text', 'that', "won't", 'be', 'integers'], [99, 100, 101]]
Or filter out the things that are not digits:
>>> map(int, filter(lambda s: s.isdigit(), txt.split()))
[3, 4, 12, 14, 16, 1, 2, 3, 4, 5, 6, 99, 100, 101]