defaultdict

TypeError: first argument must be callable

♀尐吖头ヾ 提交于 2020-04-12 07:59:13
问题 fs = codecs.open('grammar_new.txt', encoding='utf-8') unidata=[] d={} fr=codecs.open('rule.txt', 'w') for line in fs: line_data=line.split() for i in range(0,len(line_data)): unidata.append(line_data[i]) d = defaultdict(unidata) while executing this code will generate error as d = defaultdict(unidata) TypeError: first argument must be callable..I want to store duplicate keys in dictionary 回答1: The first argument to defaultdict must be callable . You've passed an instance of list which isn't

defaultdict constant_factory doesn't behave as expected

断了今生、忘了曾经 提交于 2020-01-05 05:57:10
问题 I'm willing to use defaultdict with an ad hoc default_factory which suits better my purpose. The default_factory would be [0,0] . I've implemented a constant_factory function: def constant_factory(value): return itertools.repeat(value).next Then when I try to use it, my defaultdict has an unexpected behavior (at least a behavior I didn't expect). Here is an example: >>>import itertools >>>from collections import defaultdict >>>dictio=defaultdict(constant_factory([0,0])) >>>for i in xrange(10)

Retrieving total number of words with 2 or more letters in a document using python

六月ゝ 毕业季﹏ 提交于 2020-01-03 06:47:17
问题 I have a small Python script that calculates the top 10 most frequent words, 10 most infrequent words and the total number of words in a .txt document. According to the assignment, a word is defined as 2 letters or more. I have the 10 most frequent and the 10 most infrequent words printing fine, however when I attempt to print the total number of words in the document it prints the total number of all the words, including the single letter words (such as "a"). How can I get the total number

Create and lookup 2D dictionary with multiple keys per value

天涯浪子 提交于 2020-01-02 04:53:32
问题 I think I want to make a 2D dictionary with multiple keys per value. I know how to make a 2D dictionary using defaultdict : from collections import defaultdict 2d_dict = defaultdict(dict) 2d_dict['canned_food']['spam'] = 'delicious' And I know that using regular dictionaries you can make with multiple keys like: dictionary={('food','canned_food'):spam} But I want to do something like lookup by tuple-of-keys: 2d_dict[('canned_food','food')]['spam'] = 'delicious' In the first dimension of

Why can't I create a default, ordered dict by inheriting OrderedDict and defaultdict?

拜拜、爱过 提交于 2020-01-01 08:27:01
问题 My first attempt to combine the features of two dictionaries in the collections module was to create a class that inherits them: from collections import OrderedDict, defaultdict class DefaultOrderedDict(defaultdict, OrderedDict): def __init__(self, default_factory=None, *a, **kw): super().__init__(default_factory, *a, **kw) However, I cannot assign an item to this dictionary: d = DefaultOrderedDict(lambda: 0) d['a'] = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module>

How to read two lines from a file and create dynamics keys in a for-loop, a follow-up

久未见 提交于 2020-01-01 04:43:22
问题 This question follows the problem in question: How to read two lines from a file and create dynamics keys in a for-loop? But, the nature of the problem has evolved to certain complexity that I want to address. Below is the structure of my data separated by space. chr pos M1 M2 Mk Mg1 F1_hybrid F1_PG F1_block S1 Sk1 S2 Sj 2 16229767 T/T T/T T/T G/T C|T 1|0 726 . T/C T/C T/C 2 16229783 C/C C/C C/C A/C G|C 0|1 726 G/C G/C G/C C|G 2 16229992 A/A A/A A/A G/A G|A 1|0 726 A/A A/A A/A A|G 2 16230007

Surprising results with Python timeit: Counter() vs defaultdict() vs dict()

為{幸葍}努か 提交于 2019-12-27 17:56:13
问题 I obtained very surprising results with timeit, can someone tell me if I am doing something wrong ? I am using Python 2.7. This is the contents of file speedtest_init.py: import random to_count = [random.randint(0, 100) for r in range(60)] These are the contents of speedtest.py: __author__ = 'BlueTrin' import timeit def test_init1(): print(timeit.timeit('import speedtest_init')) def test_counter1(): s = """\ d = defaultdict(int); for i in speedtest_init.to_count: d[i] += 1 """ print(timeit

create matrix using python

Deadly 提交于 2019-12-25 04:28:09
问题 I have 6 text files (each corresponds to a specific sample) and each file looks like this: Gene_ID Gene_Name Strand Start End Length Coverage FPKM TPM ENSMUSG00000102735 Gm7369 + 4610471 4611406 936 0 0 0 ENSMUSG00000025900 Rp1 - 4290846 4409241 10926 0 0 0 ENSMUSG00000104123 Gm37483 - 4363346 4364829 1484 0 0 0 ENSMUSG00000102175 Gm6119 - 4692219 4693424 1206 0.328358 0.015815 0.008621 I want to collect all the elements from 1 & 2 column in one file and corresponding tpm values(9th column)

How to order by key (alphabetically) in defaultdict(list) for an inverted index

人走茶凉 提交于 2019-12-24 19:29:08
问题 I have an inverted index. It consists of my word dictionary and the posting list of documents in which the terms appear. What I simply want is to sort my dictionary alphabetically. This is how it looks right now (example): self.index = defaultdict(<type 'list'>, { 'all': [['d03', array('I', [32L, 40L)], ['d07', array('I', [32L, 40L, 47L])], ['d05', array('I', [32L, 40L, 47L])]], 'just': [['d03', array('I', [11L])], ['d07', array('I', [11L])], ['d05', array('I', [11L])], ['d08', array('I',

Writing defaultdict(list) to file

余生颓废 提交于 2019-12-23 18:16:51
问题 Previously asked a question Using defaultdict to parse multi delimiter file While I do get the desired output based on the code, I am struggling to write it to a file as a table in this form count pos _pos _neg 31022550 31022550 31022550 31022550 ids: for key, rows in ids.iteritems(): for row in rows: print '{}\t{}'.format(key, row) 31022550 {'count': '0', 'base': '=', 'pos': '20', '_neg': '0', '_pos': '0'} 31022550 {'count': '2', 'base': 'A', 'pos': '20', '_neg': '0', '_pos': '2'} 31022550 {