Posts

Showing posts with the label dictionary

Converting a .csv.gz to .csv in Python 2.7

Converting a .csv.gz to .csv in Python 2.7 I have read the documentation and a few additional posts on SO and other various places, but I can't quite figure out this concept: When you call csvFilename = gzip.open(filename, 'rb') then reader = csv.reader(open(csvFilename)) , is that reader not a valid csv file? csvFilename = gzip.open(filename, 'rb') reader = csv.reader(open(csvFilename)) reader I am trying to solve the problem outlined below, and am getting a coercing to Unicode: need string or buffer, GzipFile found error on line 41 and 7 (highlighted below), leading me to believe that the gzip.open and csv.reader do not work as I had previously thought. coercing to Unicode: need string or buffer, GzipFile found Problem I am trying to solve I am trying to take a results.csv.gz and convert it to a results.csv so that I can turn the results.csv into a python dictionary and then combine it with another python dictionary. results.csv.gz results.csv results.csv Fi...

Append list based on another element in list and remove lists that contained the items

Append list based on another element in list and remove lists that contained the items Let's say I have two lists like this: list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] I want to combine the items together by their holder (i.e 'Robert') only when they are in separate lists. Ie in the end list_all should contain: list_all = [[['some_name','something_else'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] What is a fast and effective way of doing it? I've tried in different ways but I'm looking for something more elegant, more simplistic. Thank you Is keeping the results in this list format required for some reason? I would have thought a dict of lists with names as keys may make more sense – PerlPingu ...

filter out item from ordered dict

filter out item from ordered dict For the following ordered dictionary, how can I print just the 1)'Price' and its value 2) rank it in descending order with its corresponding 'room_id' [OrderedDict([('room_id', '1133718'), ('survey_id', '1280'), ('host_id', '6219420'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK03'), ('reviews', '9'), ('overall_satisfaction', '4.5'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), ('latitude', '1.293354'), ('longitude', '103.769226'), ('location', '0101000020E6100000E84EB0FF3AF159...

Find numbers which begins with a given number in a Map in Java

Find numbers which begins with a given number in a Map in Java I'd like to count all keys in a HashMap which begin with a given number. The size of each key is not always the same. Example: given number(long): long l = 9988776655 find the keys (long) which begin with that number like: 9988776655xxxxxxxxxxxxxxx in which x stands for any integer. How do I approach this problem? Since the length of the keys is not always the same I cannot do it with multiple modulo operations. (or can I?) String.valueOf(veryLongNumber).startsWith(String.valueOf(smallerNumber)) – Lino Jun 29 at 11:10 String.valueOf(veryLongNumber).startsWith(String.valueOf(smallerNumber)) Thanks for the quick answer! – notsosmart.nk Jun 29 at 11:13 ...

Python lamda function in dict

Python lamda function in dict This woking code gives me the 5 most relevant documents for a topic out of my corpus. most_relevant_docs = sorted(bow_corpus, reverse=True, key=lambda doc: abs(dict(doc).get(topic_number, 0.0))) print most_relevant_docs[ :5] But since the corpus is not readable by human I want to zip an index to the corpus so I can recover the depending documents. corpus_ids = range(0,len(corpus)) most_relevant_docs = sorted(zip(corpus_ids, bow_corpus), reverse=True, key=lambda my_id, doc : abs(dict(doc).get(topic_number, 0.0))) print most_relevant_docs[ :5] Where do I have to adapt the lamda function so it returns the id together with the document? Can you mock up some data so we can visualize what you are trying to achieve? Of course, as it stands, we can't run any of your code. – jpp Jun 29 at 9:36 ...

Stream grouping by sum of determinate objects

Stream grouping by sum of determinate objects I have a Request class like this : public class Request { String name,destName; int nSeats; //Example : requestOne,Paris,3 ... } I want to group the request in a Map |Integer,List of String| where the keys are the sum of the request with the same destName and the values are the destinations' names. Here is my code: public TreeMap<Integer, List<String>> destinationsPerNSeats() { return requests. stream(). collect(Collectors.groupingBy(Request::getnSeats, TreeMap::new, Collectors.mapping(Request::getDestName, Collectors.toList()))). } Input : TreeMap<Integer, List<String>> map = mgr.destinationsPerNSeats(); print(map); Output : {4=[Paris], 3=[London, Berlin, Berlin], 2=[Paris]} Output expected : {6=[Berlin, Paris], 3=[London]} How can I solve this? Thanks! What did you use as Input? – Glains 8 mins ago ...

Removing duplicates from list of dictionaries with multiple key/value pairs by comparing on some of the values [on hold]

Removing duplicates from list of dictionaries with multiple key/value pairs by comparing on some of the values [on hold] having some trouble getting this done. I would go for fully functional approach as a nested for loops but that easily gets out of hand (n^2). I have a list of dictionaries (think phonebook even if it isn't) with each dict having this kind of key,values: {'mat':'name_of_the_material', 'tex': [list_of_textures], 'geo':[list_of_geo]} The goal is to reduce the list of dictionaries and match them on first two keys ('mat','tex') and combine the third list when de-duplicating. So mat , tex wouldn't repeat, and geo list would be a merge of N items from the original list of dict. mat tex geo Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this que...

Regex or wildcard in dictionary.TryGetValue

Regex or wildcard in dictionary.TryGetValue I have a similar problem like mentioned in this Link, fetching data from Dictionary using partial key and my key DataType is string . Dictionary DataType string This is how my dictionary looks Key Values GUID1+GUID2+GUID3 1, 2, 3 GUID1+GUID2+GUID3 4, 5, 6 GUID1+GUID2+GUID3 7, 8, 9 But the solution provided is fetching data from Dictionary using an extension method with linq in Dictionary . I just want to extract data from Dictionary using TryGetValue passing Regex or wildcard expression. Dictionary Dictionary Dictionary TryGetValue Regex Note that you'll lose any performance gains offered by a dictionary by doing this. The reason is that the dictionary first finds items which match the key's hash code, and then performs equality comparisons on each item key to find the one that is an exact match. If you use ...