Questions tagged [dictionary]

19727 questions
0

votes
1

answer
29

Views

How can I split an array into specified lines when printing?

New to software development, I'm working through Big Nerd Ranch's book 'Swift Programming'. One of the challenges set by the book is to print an array within a dictionary that contains 15 zip codes and to have them split across three lines in groups of five. The formatting must exactly match how it...
Peter H
1

votes
2

answer
3.9k

Views

Compare the ArrayList values and HashMap values

I have a small query with respect to the comparison of ArrayList value with the HashMap value and extract the key value if they are equal. I am reading two files and storing them in ArrayList and HashMap respectively. I have to compare these values and extract the key from HashMap. For example: Arra...
1

votes
1

answer
1.5k

Views

error: passing ‘const std::map<int, int>’ as ‘this’ argument discards qualifiers [-fpermissive] [duplicate]

This question already has an answer here: C++ “error: passing 'const std::map' as 'this' argument of …” 3 answers Fetching an entry from a const C++ std::map fails to compile on gcc 5.4.0. test_map.cpp: In function ‘int main()’: test_map.cpp:9:24: error: passing ‘const std::map’ as ...
Stéphane Gourichon
1

votes
3

answer
5.7k

Views

Sum dictionary values?

Help: Please write a function called sumDictionaryValues that accepts a single argument: a dictionary variable. The keys of this dictionary will be String variables. The values of this dictionary will each be a list of numbers. Your function should create a new dictionary. The keys of the new dictio...
mads
1

votes
1

answer
16

Views

Convert nested html-table to nested-dictionary in python?

I am writting an application convert the html-table string data receive from website (by calling RESR API) to dictionary format. The problem is that the format of HTML table string is nested HTML table format. After a while doing search on the internet i cannot find the solution for this case. Event...
hieu to van
1

votes
3

answer
419

Views

arrange keys order in a django queryset

I have this Django query where I get some information from different databases, which I want to expose to the front end: specifics = PageData.objects.filter(page_id__in=page_ids).values('page_id'). \ annotate(views=Count('page_id')). \ values('views', 'page__title', 'page_...
Mattia Paterna
1

votes
1

answer
1.6k

Views

labeling networkx node attributes outside of nodes

I am working on a small example node set belonging to two types {'human', 'machine'} and I want to label node attributes in dictionary form outside of each node in networkx graph, such as those shown in nodes c, e, j in the graph below. (I used MS Word to add dictionary-type attributes on the graph....
Chris T.
0

votes
0

answer
6

Views

program not recognizing multiple occurrences of word python (on linux)

I'm making an essay generator by creating a directed graph from sample text where the words are nodes and there are directed edges between a word and any word that follows it. I'm forming the nodes through a dictionary, but the program doesn't seem to read occurrences of a word after the first. tH =...
viperclan
1

votes
1

answer
73

Views

Why is my dictionary in python always ordered? [duplicate]

This question already has an answer here: Are dictionaries ordered in Python 3.6+? 3 answers I know that the dictionary in Python is unordered. But I'm confused why my dict is always ordered when I output them. Like the code following, no matter how I change the keys or the values, it is always pri...
Tao
1

votes
3

answer
48

Views

Max key from other dictionary values function in Python

I have a dictionary stats = {'OK':0, 'SKIP':1, 'EXCEPTION':2, 'WARN':3, 'ERROR':4, 'FATAL':5} I get values from a different program that gives a dictionary like this my_values = {'Web': OK, 'Host': WARN, 'System': FATAL} Now I want to compare value from my_values with keys of stats and get the high...
user3330284
1

votes
2

answer
53

Views

In Python, how to match a string to a dictionary item (like 'Bra*')

I'm a complete novice at Python so please excuse me for asking something stupid. From a textfile a dictionary is made to be used as a pass/block filter. The textfile contains addresses and either a block or allow like '002029568,allow' or '0011*,allow' (without the quotes). The search-input is a st...
John K
1

votes
1

answer
20

Views

Cannot read property 'map' of undefined while fetching VANILLA JS

When I launch splitArray(people, 5) and displayContent(0) functions one after another in console, they work perfectly fine, but when I add them with .then() into my fetch method, I get an Uncaught TypeError: Cannot read property 'map' of undefined error. I'm trying to make my first app that displays...
Tomasz Czechowski
1

votes
3

answer
91

Views

Create dictionary of dicts from a single list - Python3

Python 3.6.5/3.7.1 on Linux Struggling to create a dictionary with dictionaries as values. I want to create a dictionary from a list date & time data (ultimately to create charts with bokeh). This must have been asked before, but I can't find a set of search terms that returns a result that clarifie...
CodeRedacted
1

votes
2

answer
36

Views

Using Filter and Map to Modify Merged Array

I have the following code, which takes 2 arrays of objects in JavaScript and compares them and merges the contents, returning all objects from the 1st array, plus all objects from the 2nd that have an id that's not in the 1st. But for those items that come from the 2nd array, I want to modify its en...
J Woodchuck
1

votes
1

answer
74

Views

Why does dictionary take less time than set in python?

Both dictionary and sets are implemented as hash tables in Python and have an insertion time and lookup time of O(1). I am writing a program to count if the string consists of all unique characters and I am using a set to keep a track of all the characters seen so far. What I observe is if I use a d...
siddharth
1

votes
1

answer
58

Views

How to check whether a dictionary is nested - python

DON'T FORGET, SEE MY SELF-ANSWER BELOW Let's say i have a dictionary called d: d = {'a': {1: (1,2,3), 2: (4,5,6)},'b': {1: (3,2,1), 2: (6,5,4)}} As you can see, it is a nested dictionary, how would i detect if it is? Here are some examples: d = {'a':{1:(1,2,3),2:(4,5,6)},'b':{1:(3,2,1),2:(6,5,4)}} d...
U9-Forward
1

votes
1

answer
53

Views

Sorting dictionary by the closest value

I have a dictionary of the keys and values, that values are from 0.0 to 1.0 like this the one below: {'item1': 0.88, 'item2': 0.4, 'item3': 0.49, 'item4': 0.75, 'item5': 0.32, 'item6': 0.9} I had to sort it by the value closest to 0.5. I know how to sort dictionary ascending or descending from the g...
Miłosz Synecki
1

votes
1

answer
27

Views

Remove prefix quotes from a list of strings

So I have a list that includes some prefix single quotes that I really don't need. I tried to convert them with map(). The list looks like this: ['{'id':'browser','top':5000}', '{'id':'amo_ef_id','top':5000}'] I want it to look like this: [{'id':'browser','top':5000}, {'id':'amo_ef_id','top':5000}]
PyRar
1

votes
4

answer
43

Views

comparing list with a dictionary index and returning dictionary values based on original list order

I have a list that has 'index' values that I want to compare to a dictionary index. I want the dictionary values to be returned in the same order as the list. example below: list =['a','b','c','d'...n] dict = {'d':'fourth' ,'a': 'first','b':'second','c':'third'...n} The issue is, when I execute the...
Joey
1

votes
2

answer
50

Views

Loading the dictionary values in to the row values of a dataframe in pandas

I have a python dictionary {1:cat, 2:dog, 3:sheep, 4:foo, 5:bar, 6:fish, 7:lion, 8:shark, 9:zebra, 10:snake} Also I have pandas dataframe as following df: ID col1 col2 col2 col4 18938 1 Nan 5 Nan 17839 Nan 2 Nan 8 72902 3 5 9 Nan 78298...
Abalan Musk
1

votes
1

answer
46

Views

How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items

I have a Dictionary L and I want to find in it all keys such that the corresponding value, a List, has tuples such that Item1 and Item2 are equal to some given values 'toto' and 'tata'. I tried like below: var res = L.Where(x => x.Value.ToList().Where(y => y.Item1 == 'toto' && (string) y.Item2 == '...
11house
1

votes
5

answer
69

Views

Combining Python dictionary into a string csv

I have a Dictionary here: dict = {'AB': 1, 'AE': '0', 'CC': '3'} I am trying to get that dictionary into the exact string format: AB1,AE0,CC3 I have currently been trying this: string = ','.join(('{},{}'.format(*i) for i in dict.items())) But my output has been this: AB,1,AE,0,CC,3 Which is just off...
Evan Graves
1

votes
1

answer
36

Views

How to cumulatively update a dictionary with values from other dictionaries?

I have a master dictionary to keep the word frequency of the entire corpus and individual dictionaries to keep the word frequency for each text file. I loop over each file, generate the WF of each file, and sequentially update the master dictionary. My code is as follows. Is there a shortcut? Thank...
Victor Wang
1

votes
2

answer
49

Views

Convert python dictionary to dataframe with dict values(list) as columns and 1,0 if that column is in dict list

I want to create a dataframe from a dictionary which is of the format Dictionary_ = {'Key1': ['a', 'b', 'c', 'd'],'Key2': ['d', 'f'],'Key3': ['a', 'c', 'm', 'n']} I am using df = pd.DataFrame.from_dict(Dictionary_, orient ='index') But it creates its own columns till max length of values and put v...
vishal sharma
1

votes
2

answer
44

Views

Combine dictionary keys according to bins

I have a dictionary, myDict = {1: 10, 1.1: 10, 2: 15, 2.1: 20} but instead of only 4 key-value pairs, it has thousands of them and some are very close together like the key 1 and the key 1.1 in my example sometimes up to machine epsilon. Is there a simple procedure how I can bin the keys together w...
ste
1

votes
3

answer
44

Views

Key, values under one header in .INI file

I have this code in Python which, given a dictionary, it writes the key:value fields in a config.ini file. The problem is that it keeps writing the header for each field. import configparser myDict = {'hello': 'world', 'hi': 'space'} def createConfig(myDict): config = configparser.ConfigParser() # t...
Link
1

votes
3

answer
84

Views

Dictionary inside List

Imagine that I have this list: a=[ {'user':'John','password':'123'} , {'user':'Mike','password':'12345'} ,{'user':'Peter','password':'qwerty'}] user=input('insert your username') password=input('insert your password') Now I want to know if the user and password typed are in the previous list, how ca...
alladin
1

votes
2

answer
59

Views

Question about function pointers, map and templates

I'm learning about function pointers and maps. I have to use function pointers and maps to write a replication of switch-statements without using if or switch. I want to write a function execute() that take two arguments 'a' and 'b' and do an operation with the appropriate operation character. #inc...
Pemi Ng
1

votes
4

answer
55

Views

Can I simplify my code so that I am not writing out every letter of the alphabet individually?

I am writing a code that loops through the Gettysburg Address and counts the number of occurrences of each letter. The letter is then stored as a key in the dictionary, and the value for each key is the total number of occurrences of that specific value. The Gettysburg Address was split up into thre...
Jaxon123
1

votes
1

answer
47

Views

Dictionary of Logic Functions

I want to create a dictionary in c++ where I am supposed to keep a reference number and a logic function related to it, I thought could do like this sample: mapfunc = { {1 = !}, {2 = &&}, {3 = ||} }; Someone have an ideia how I could implement this?
Rita Carolina
1

votes
3

answer
70

Views

How to remove duplicates from list of dicts?

I have a list of dictionaries in python as follows: [{'category': 'software', 'name': 'irssi', 'version': '1.2.0'}, {'category': 'software', 'name': 'irssi', 'version': '1.1.2'}, {'category': 'software', 'name': 'hexchat', 'version': '2.14.2'}] (parsing some data txt file) What I wanna do: If catego...
Lorem
1

votes
2

answer
55

Views

Find key, value pair in dict according to second value of tuples using itemgetter() method

I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':(17, 205), 'orange':(1, 91)} I would like to find key, value pair according to max value of the second element in the tuple so in this case it should return: banana':(9, 407) I know I can get this result using l...
halny
1

votes
3

answer
38

Views

Python csv(Two columns: key/value) to Dictionary

Is it possible to read data from a csv file into a dictionary, such that the first column is the key and the second column. E.g. I have a csv file code msg 123456 Lorem ipsum dolor sit amet, consectetur adipiscing elit 345981 sed do eiusmo ut labore, et dolore magna aliqua; 459827 ullamco, lab...
XdansLaFoule
1

votes
3

answer
54

Views

Simple way to create JavaScript dictionary from Array of Arrays

I have a list of lists, each inner list has 2 items. I want to transform it into a dictionary. const have = [['a', 1], ['b', 2]] const want = {'a': 1, 'b': 2} In python I would do >>> dict([['a', 1], ['b', 2]]) {'a': 1, 'b': 2} What is the easiest way (1-liner) to achieve this in JavaScript? The ea...
Andrei Cioara
1

votes
2

answer
55

Views

Python statements not executing sequentially?

I have the following three lists: d1 = [{'Names': ['Chris','Sam','Max'], 'Color': 'Blue'},{'Names': ['Albert','Robert','Julie'], 'Color': 'Red'}] d2 = [{'SomeNames': ['April','John','Erin'], 'Color': 'Green'},{'SomeNames': ['Phillip','Nate','Wendy'], 'Color': 'Yellow'}] d3 = [{'Color': 'Pink'}, {'C...
TroggleDorf
1

votes
2

answer
68

Views

When to check if item already exists in C++ set/map?

I have a general question regarding set, unordered_set, map, unordered_map in C++ STL. I often see code that checks if an item is already in a set/map before attempting to alter its value. I was wondering, when is it advisable to manually check if an item already exists in the set/map before attempt...
Timothy Huang
0

votes
0

answer
23

Views

How to access a function using an object which is stored inside a dictionary in python?

I have created a class which has an object named entity inside it. The objects has a set of attributes and functions inside. In a different python file, I have created a dictionary of entity objects. The key is 'id' (which is a variable used while creating the object) and the value is the object it...
user8306074
1

votes
2

answer
2.4k

Views

Convert a sequence of dictionary keys to a set

The following code lists the set of keys found in a dictionary sequence (each dict is basically a row from a database). (I want to convert the keys to a set so I can compare 2 db tables) for seqitem in tblseq do let keyset = seqitem.Keys |> Set.ofSeq // works correctly printfn '>>> List: %A; Ite...
BrendanC
1

votes
1

answer
259

Views

Posting multiple values with one parameter with httpclienthandler in c#

I am trying to send a form to a website with HTTP Client. The Website then returns a list with data based on my request. The process itself is working fine but I have to do 3 requests wheras one is enough when I do it by hand in the browser. In the browser I have multiple checkboxes to apply Filters...
julian bechtold
1

votes
0

answer
117

Views

Loop through Dictionary in Swift

I have a dictionary which I want to append some items. **This is my dictionary which I want to achieve: ** let parameters: [String: Any] = [ { 'ResultsList': [{ 'UserId': 'b806e283-066f-4081-aafe-1fe216a57c35', 'FriendUserId': '7a2ec150-cdb3-4600-84f8-2dab970bfa0c', 'TransferDate': '2017-11-23', 'U...
Ziyaad

View additional questions