dictionary

Python3, building nested dictionary

一曲冷凌霜 提交于 2021-01-29 17:37:21
问题 I'm new with Python, coming from Perl where I've used something like this very often: #!/usr/bin/env perl my %d; my $a = 12; $b = 14; $c = 16; $d{$a}{$b}{$c} = 42; print($d{$a}{$b}{$c}); # 42 How can fill a dictionary in python e.g. in a loop If I try this with Python, I've got a KeyError a = 12 b = 14 c = 16 my_dict[a][b][c] = 42 # KeyError Is this the only way? if a in my_dict: if b in my_dict[a]: my_dict[a][b][c] = 42 else: my_dict[a][b] = {} my_dict[a][b][c] = 42 else: my_dict[a] = {} my

Yfinance IndexError: list index out of range

白昼怎懂夜的黑 提交于 2021-01-29 17:25:55
问题 I wrote the code below and it is running. When the loop run a fourth time, it gives an error. It gives "IndexError: list index out of range". How do I fix this error? import yfinance as yf dow_list = ['AAPL', 'AXP', 'BA', 'CAT', 'CSCO', 'CVX', 'DIS', 'DOW', 'GS', 'HD', 'IBM', 'INTC', 'JNJ', 'JPM', 'KO', 'MCD', 'MMM', 'MRK', 'MSFT', 'NKE', 'PFE', 'PG', 'RTX', 'TRV', 'UNH', 'V', 'VZ', 'WBA', 'WMT', 'XOM'] rows = [] for ticker in dow_list: stk_container = yf.Ticker(ticker) stk_info = stk

Python - How to sort a dictionary based on its value as well it key

拥有回忆 提交于 2021-01-29 17:01:30
问题 I have a dictionary as :- { (1, 1): 16, (1, 2): 16, (1, 3): 16, (1, 4): 16, (2, 1): 24, (2, 2): 24, (2, 3): 24, (2, 4): 24 } ...i want it to be ordered as per tuple key's second field as well as dictionary's value field... so result dictionary should be :- { (2, 1): 24, (2, 2): 24, (2, 3): 24, (2, 4): 24, (1, 1): 16, (1, 2): 16, (1, 3): 16, (1, 4): 16 } ... i tried sorting it with value alone but it messes the keys tuple order.... yes so i assigned it to a list afterwards by doing ... list =

Partitioning large dictionary (400k+) for finding words in words in Python 3

依然范特西╮ 提交于 2021-01-29 16:19:38
问题 in my neural network, I want to normalize words of a text. It means words get converted to infinitives and gender independent. Example: "Unternehmensgruppenführer" (german) gets 3 words: unternehmen gruppe fahren. This requires a lot of "startsWith" processing. I built a table that gives me word=>infinitive and loads it into a sorted(!) dictionary of length 400k. However, the processing of that dictionary by finding words in words got very slow. I optimized that with an implementation of

Mapping between two arrays of observable classes

回眸只為那壹抹淺笑 提交于 2021-01-29 12:54:46
问题 I have two given classes SgFormsBase and QuestionBase with slightly different member names, and I want to translate observable[] of one to the other. import { of, Observable } from 'rxjs' import { map} from 'rxjs/operators' class SgFormsBase{ constructor( public id: number, public survey: string ){} } class QuestionBase{ constructor( public qid: number, public qsurvey: string ){} } const a = new SgFormsBase(11, 'Endo') const b = new SgFormsBase(12, 'Kolo') const sg = of([a, b] ) function

parse pyspark column values into new columns

谁说我不能喝 提交于 2021-01-29 11:31:40
问题 I have a pyspark dataframe like the example df below. It has 3 columns in it organization_id, id, query_builder. The query_builder column contains a string that's similar to a nested dict. I would like to parse the query_builder field into separate columns for the field, operator, and value. I've supplied an example desired output below. If need be I could convert the pyspark dataframe to a pandas dataframe to make it easier. Does anyone have suggestions or recognize the type of data in the

Save changed textbutton state back to dictionary value list index 0

非 Y 不嫁゛ 提交于 2021-01-29 10:11:58
问题 Good Day. I have managed to load a dictionary from a text file with this code: def loadDictFile(data_folder): critDict = json.loads(open(data_folder+'critDict3.txt').read()) print('json_data is', critDict) return critDict And I can create a tkinter UI with checkboxes that are on or off according to the dictionary values using the following code: def makeUI(data_folder, critDict): top = Tk() varList=[] for key, val in critDict.items(): myVar = IntVar()#create intVar for this checkbutton

Why is argument missing in chained Map operator

六眼飞鱼酱① 提交于 2021-01-29 10:11:32
问题 I'm getting to know hyperledger composer angular apps. This piece of code that yeoman generator produces puzzle me. The add method executes return this.http.post and it returns a response object, that is chained to a map operator to convert to json output. But all references I could find to map operator show that its argument must be specified, like an arrow function (v)=> {some instructions to perform on v here}); , or as an anonymous function (f(v){some instructions here}) ; I've seem the

EXCEL VBA: Class Module Variables value location depending on if function

六眼飞鱼酱① 提交于 2021-01-29 09:58:09
问题 I have created a dictionary to combine several bits of data for one event. For example, NameA can have four entries depending on the value in column 5. It can be A, B, C, D. However i would like to display all this information from A, B, C, D on one row in a table for NameA as the end product. So i have created a dictionary with a class module to show this, with the class listing all possible variables to display in final output. Depending on the entries (A, B, C, D) the value in column 1 can

TypeError: '>' not supported between instances of 'dict' and 'dict'

左心房为你撑大大i 提交于 2021-01-29 09:55:01
问题 I'm working with dictionaries and I have the following error '>' not supported between instances of 'dict' and 'dict' I know that there are some problems with dictionaries in Python 2.7 and 3.x version. print("number of nodes %d" % G.number_of_nodes()) print("number of edges %d" % G.number_of_edges()) print("Graph is connected?: %s" % nx.is_connected(G)) print("Number of connected components: %s" % nx.number_connected_components(G)) print("Size of connected componnents: %s" % [len(cc) for cc