dictionary

Python - How to Create Dictionary from CSV data file using column headings

回眸只為那壹抹淺笑 提交于 2021-01-27 06:28:14
问题 I am trying to create a function that accepts the name of a .csv data file and a list of strings representing column headings in that file and return a dict object with each key being a column heading and the corresponding value being a numpy array of the values in that column of the data file. My code right now: def columndata(filename, columns): d = dict() for col in columns: with open(filename) as filein: reader = csv.reader(filein) for row in reader: if col in row: d.append(row) return d

Check that Python dicts have same shape and keys

﹥>﹥吖頭↗ 提交于 2021-01-27 04:40:08
问题 For single layer dicts like x = {'a': 1, 'b': 2} the problem is easy and answered on SO (Pythonic way to check if two dictionaries have the identical set of keys?) but what about nested dicts? For example, y = {'a': {'c': 3}, 'b': {'d': 4}} has keys 'a' and 'b' but I want to compare its shape to another nested dict structure like z = {'a': {'c': 5}, 'b': {'d': 6}} which has the same shape and keys (different values is fine) as y . w = {'a': {'c': 3}, 'b': {'e': 4}} would have keys 'a' and 'b'

Concise Ruby hash equivalent of Python dict.get()

十年热恋 提交于 2021-01-26 23:34:01
问题 In know that I can manipulate a Ruby default Hash value like this: h={a:1, b:2, c:3} h[:x] # => nil h.default = 5 h[:x] # => 5 h.default = 8 h[:y] # => 8 but this gets quite tedious when doing it repeatedly for multiple values with different defaults. It also could get dangerous if the hash is passed to other methods which want their own defaults for certain (potentially missing) keys. In Python, I used to d={'a':1, 'b':2, 'c':3} d.get('x', 5) # => 5 d.get('y', 8) # => 8 which doesn't have

Unmarshal nested XML elements with JAXB

徘徊边缘 提交于 2021-01-24 11:22:57
问题 I'm starting with JAXB and im trying to read the following xml to map it to a classe: <element id="0"> <type>1</type> <color>0</color> <size>1</size> <location> <absolute> <absolute-item>top</absolute-item> <absolute-item>left</absolute-item> </absolute> <relative> <right>0</right> <left>0</left> </relative> </location> </element> My porblem comes when I try to map the nested elements like abslute, wich can contain any number of elements. I'm trying this right now: public class Element {

Unmarshal nested XML elements with JAXB

不羁岁月 提交于 2021-01-24 11:20:47
问题 I'm starting with JAXB and im trying to read the following xml to map it to a classe: <element id="0"> <type>1</type> <color>0</color> <size>1</size> <location> <absolute> <absolute-item>top</absolute-item> <absolute-item>left</absolute-item> </absolute> <relative> <right>0</right> <left>0</left> </relative> </location> </element> My porblem comes when I try to map the nested elements like abslute, wich can contain any number of elements. I'm trying this right now: public class Element {

looping through list of dictionaries python

无人久伴 提交于 2021-01-24 09:50:47
问题 I'm trying to access a dictionary within a list and cannot seem to get my for loop to get the key, then the value. I've placed an image herein so that its easy for me to explain. so you can see, I would like to navigate to currency = AUD and assign the balance value to a variable, call it aud_balance for curr in result_bal_qr: for k in curr: if curr[k] == 'AUD': I cannot seem to get the key AUD . so I'm officially stuck. I've tried to search for dictionary inside of lists etc but no examples

looping through list of dictionaries python

核能气质少年 提交于 2021-01-24 09:49:46
问题 I'm trying to access a dictionary within a list and cannot seem to get my for loop to get the key, then the value. I've placed an image herein so that its easy for me to explain. so you can see, I would like to navigate to currency = AUD and assign the balance value to a variable, call it aud_balance for curr in result_bal_qr: for k in curr: if curr[k] == 'AUD': I cannot seem to get the key AUD . so I'm officially stuck. I've tried to search for dictionary inside of lists etc but no examples

Array Indexing in Python

独自空忆成欢 提交于 2021-01-23 20:34:32
问题 Beginner here, learning python, was wondering something. This gives me the second element: list = [1,2,3,4] list.index(2) 2 But when i tried this: list = [0] * 5 list[2] = [1,2,3,4] list.index[4] I get an error. Is there some way to pull the index of an element from an array, no matter what list it's placed into? I know it's possible with dictionaries: info = {first:1,second:2,third:3} for i in info.values: print i 1 2 3 Is there something like that for lists? 回答1: The index method does not

Array Indexing in Python

风格不统一 提交于 2021-01-23 20:23:00
问题 Beginner here, learning python, was wondering something. This gives me the second element: list = [1,2,3,4] list.index(2) 2 But when i tried this: list = [0] * 5 list[2] = [1,2,3,4] list.index[4] I get an error. Is there some way to pull the index of an element from an array, no matter what list it's placed into? I know it's possible with dictionaries: info = {first:1,second:2,third:3} for i in info.values: print i 1 2 3 Is there something like that for lists? 回答1: The index method does not

Array Indexing in Python

ぃ、小莉子 提交于 2021-01-23 20:22:06
问题 Beginner here, learning python, was wondering something. This gives me the second element: list = [1,2,3,4] list.index(2) 2 But when i tried this: list = [0] * 5 list[2] = [1,2,3,4] list.index[4] I get an error. Is there some way to pull the index of an element from an array, no matter what list it's placed into? I know it's possible with dictionaries: info = {first:1,second:2,third:3} for i in info.values: print i 1 2 3 Is there something like that for lists? 回答1: The index method does not