Posts

Showing posts with the label dictionary

Python: DIvide items of a dictionary by a value that depends on the first key

Image
Clash Royale CLAN TAG #URR8PPP Python: DIvide items of a dictionary by a value that depends on the first key I believe that this questions is very similar to Divide each python dictionary by total value, however I am not able to deal with multidimentional key. So I have this first dictionary: x={'SBGL': 0.2631678018921199, 'SBAR': 0.00017329658914466182, 'SBKP': 0.05787906885949929, 'SBRJ': 0.01686853063415596, 'SBFZ': 0.002151268003175112, 'SBCH': 0.0, 'SBRF': 0.0010995369794006128, 'SBFI': 0.0, 'SBGR': 0.3555667545433087, 'SBCT': 0.12645173241811486, 'SBFL': 0.0020536641771051302, 'SBSP': 0.12206752790423671, 'SBSV': 0.002883296698699977, 'SBMG': 0.0, 'SBCA': 0.0, 'SBPA': 0.024485612897250063, 'SBPS': 0.0006125138064595806, 'SBCF': 0.005023609170377438, 'SBMO': 0.0020686035382382908, 'SBBR': 0.01744718188871371, 'SBJV'...

TypeError: Cannot read property 'map' of undefined ReactJS API calls

Image
Clash Royale CLAN TAG #URR8PPP TypeError: Cannot read property 'map' of undefined ReactJS API calls I have recently been trying to play with APIs in React.js, I believed the below code would have worked, based on a tutorial from https://reactjs.org/docs/faq-ajax.html, However when I run this code I keep getting TypeError: Cannot read property 'map' of undefined Below is my code, this is a component called DOTA and is exported to my App.js import React from 'react'; const API_KEY ="some-api-key"; const DEFAULT_QUERY = 'redux'; class DOTA extends React.Component { constructor(props) { super(props); this.state = { error: null, isLoaded: false, info: , }; } componentDidMount() { fetch(API_KEY + DEFAULT_QUERY) .then(response => response.json(console.log(response))) .then((result) => { console.log(result) this.setState({ isLoaded: true, info: result.i...

Read all words from a given text file and print a count for each

Image
Clash Royale CLAN TAG #URR8PPP Read all words from a given text file and print a count for each Test.txt contains the following sentence(How much wood would a woodchuck chuck if a woodchuck could chuck wood.) This program is supposed to read all words from a given text file (until eof) and print out a count for each word. The word should be processed case-insensitive (all capitals), punctuation should be removed and the output should be sorted by frequency. However I've come to a simple problem where it's counting lines and not the words, help a brother out. Make a translation table for getting rid of non-word characters dropChars = "!@#$%ˆ& ()_+-={}|\:;"’<>,.?/1234567890" dropDict = dict([(c, '') for c in dropChars]) dropTable = str.maketrans(dropDict) Read a file and build the table. f = open("Test.txt") testList=list() lineNum = 0 table = {} # dictionary: words -> set of line numbers for line in f: testList.append(line) for ...

Polynomials and dictionaries

Image
Clash Royale CLAN TAG #URR8PPP Polynomials and dictionaries i have this exercise about polynomials and dictionaries which i did (see below) but i am sure there is a better and easier way to solve it (for question 2 and 3). can anyone show me another way to approach question 2 or 3? Thanks. here is the exercice: In this exercise we want to work with polynomials of any degree. Each polynomial can be represented by a dictionary, whose keys correspond to the powers of x, and the values ​​to the coefficients. For example, to represent the polynomial x ^ 6 + 3 * x ^ 2, we can use the dictionary: {6: 1, 2: 3} 1. Write a function evaluer(p, x) that takes a polynomial p and a number x into arguments, and returns the value of polynomial at point x. Execution example: evaluer({3: 1, 1: 2, 0: -1}, 2) OUT: 11 2. Write a function somme_polynomes(p1, p2) which takes two polynomials (dictionaries) into arguments and which returns a new dictionary representing the sum of the two polynomials p1 and p2...

How to access dictionary when index is datetime?

Image
Clash Royale CLAN TAG #URR8PPP How to access dictionary when index is datetime? I have multiple csv-files each containing two columns. The first contains the timestamp and the second column contains a measurement value. Lets assume the csv-file looks like this: Date,Temperature (Celsius) 2018-07-24T20:45:31,28.86 2018-07-24T20:35:31,29.06 2018-07-24T20:25:31,29.19 2018-07-24T20:15:32,29.31 2018-07-24T20:05:31,29.48 2018-07-24T19:55:31,29.58 2018-07-24T19:45:31,29.82 2018-07-24T19:35:32,30.32 2018-07-24T19:25:31,31.00 and I import it like this: df = pd.read_csv(csv-file, sep=',', header = 0, usecols=[0, 1], parse_dates=[0], infer_datetime_format=True) dd = df.set_index('Date').T.to_dict() I thought this would be convenient later, when I would search for the same key (same timestamp) in multiple files in order to merge them. However I cannot access the dictionary. The keys are recognised as timestamp. dd.keys() dict_keys([Timestamp('2018-07-24 20:45:31'), Timestam...

Simultaneously sum two keys in list of dicts by multiple items

Image
Clash Royale CLAN TAG #URR8PPP Simultaneously sum two keys in list of dicts by multiple items I want to sum two different variables in one function, but I want these to be summed based on multiple other items. If I have the following list of dicts x: x=[{'id':1, 'var1':'a', 'var2':'left', 'var3':0.1, 'var4':1}, {'id':2, 'var1':'a', 'var2':'right', 'var3':0.1, 'var4':1}, {'id':2, 'var1':'a', 'var2':'right', 'var3':0.2, 'var4':3}, {'id':4, 'var1':'b', 'var2':'left', 'var3':0.4, 'var4':4}, {'id':5, 'var1':'b', 'var2':'right', 'var3':0.1, 'var4':5}, {'id':5, 'var1':'b', 'var2':'right', 'var3':0.4, 'var4':2}] Then i can use the following function...

how can I retrieve individual values from a multi value key in Swift 4/json dictionary?

Image
Clash Royale CLAN TAG #URR8PPP how can I retrieve individual values from a multi value key in Swift 4/json dictionary? There are many many examples of retrieving the value of a key, and I can do that. In this case, the key has multiple values. The key is "mining", followed by 6 values. The same key:value is repeated 30 times, with different values for the keys of course. What I can't seem to solve is how to return the values. Especially the first one, 1532825277682891390, for example, which appears to be a sub-key with key/value pairs related to that. The todo is: ["mining": { 1532825277682891390 = { command = mining; siteid = "Platform Mining"; ts = 1532825270557; user1 = 73276; value = 1; }; 1532826406100318457 = { command = mining; siteid = "Platform Mining"; ts = 1532826375712; user1 = 73276; value = 1; }; 1532827074955562013 = { command = mining; siteid = "Platform Mining...

How to convert List of Dictionary to simple 2D list in python to perform the specific task?

Image
Clash Royale CLAN TAG #URR8PPP How to convert List of Dictionary to simple 2D list in python to perform the specific task? I have a list which looks something like this : myList = [{'email_address': 'abc@gmail.com', 'status': 'subscribed'}, {'email_address': 'def@live.com', 'status': 'subscribed'}, {'email_address': 'ghi@gmail.in', 'status': 'pending'}, {'email_address': 'jkl@gmail.com', 'status': 'subscribed'}, {'email_address': 'mno@gmail.com', 'status': 'pending'}, {'email_address': 'pqr@yahoo.com', 'status': 'pending'}, {'email_address': 'stu@gmail.com', 'status': 'pending'}, {'email_address': 'vwx@y.com', 'status': 'pending'}, {'email_address': 'yz@z.com', 'status': 'pending'}] I want to convert th...

Ansible - joining two nested lists without the same key

Image
Clash Royale CLAN TAG #URR8PPP Ansible - joining two nested lists without the same key I am currently trying to combine two different dictionaries together. The thing that ties them together is the login field, but this is not the "key" in both dicts. --- - hosts: localhost gather_facts: no vars: userlist: tom123: guid: "tom" sally: guid: "sally" userteamlist: tom: teams: "group4, group6, group5" sally: teams: "group1, group2, group3" How do I get these two lists to merge based on the "guid"? The "guid" is the key in one list (userteamlist) and a "value" (guid) in the other list (userlist). I have used this Stackoverflow link for joining two dicts with the same key, but have not figured out to do this with key+subelement. Thoughts? Apologies all, I am looking strictly for an Ansible solution to this problem. ...

Tuples, dictionary, and helper function problems in Python

Image
Clash Royale CLAN TAG #URR8PPP Tuples, dictionary, and helper function problems in Python So I had an assignment where I took a list of tuples as an argument such as, [('A', 3), ('C', 4), ('B', 2), ('P', 4)] and I had to compute the overall gpa which is basically ((letter-grade-number * number of credits) / number of credits) . So I had to use a dictionary and my body was composed of 3 parts. The first used a helper function that appended the unweighted grades to a list and used a counter to keep track of all the credits that were taken, the second part was a conditional that returned -1 if all the lists were not considered for grading and the last part finally summed all the values in the appended list and divided it by the total number of credits taken and it works perfectly. ((letter-grade-number * number of credits) / number of credits) -1 Now, my next assignment takes a string as an argument such as: 'Jane A 3 A- 3 F 1#Bob C+ 2 D+ 3 B+ 3#Chris ...