defaultdict

convert a list of delimited strings to a tree/nested dict, using python

↘锁芯ラ 提交于 2019-11-29 02:35:17
I am trying to convert a list of dot-separated strings, e.g. ['one.two.three.four', 'one.six.seven.eight', 'five.nine.ten', 'twelve.zero'] into a tree (nested lists or dicts - anything that is easy to walk through). The real data happens to have 1 to 4 dot-separated parts of different length and has 2200 records in total. My actual goal is to fill in the set of 4 QComboBox'es with this data, in manner that the 1st QComboBox is filled with first set items ['one', 'five', 'twelve'] (no duplicates). Then depending on the chosen item, the 2nd QComboBox is filled with its related items: for 'one'

Is there a standard class for an infinitely nested defaultdict?

瘦欲@ 提交于 2019-11-28 08:33:12
Does anyone know if there's a standard class for an infinitely nestable dictionary in Python? I'm finding myself repeating this pattern: d = defaultdict(lambda: defaultdict(lambda: defaultdict(int))) d['abc']['def']['xyz'] += 1 If I want to add "another layer" (e.g. d['abc']['def']['xyz']['wrt'] ), I have to define another nesting of defaultdicts. To generalize this pattern, I've written a simple class that overrides __getitem__ to automatically create the next nested dictionary. e.g. d = InfiniteDict(('count',0),('total',0)) d['abc']['def']['xyz'].count += 0.24 d['abc']['def']['xyz'].total +=

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

£可爱£侵袭症+ 提交于 2019-11-28 04:16:00
问题 In the following data, I am trying to run a simple markov model. Say I have a data with following structure: pos M1 M2 M3 M4 M5 M6 M7 M8 hybrid_block S1 S2 S3 S4 S5 S6 S7 S8 1 A T T A A G A C A|C C G C T T A G A 2 T G C T G T T G T|A A T A T C A A T 3 C A A C A G T C C|G G A C G C G C G 4 G T G T A T C T G|T C T T T A T C T Block M represents data from one set of catergories, so does block S . The data are the strings which are made by connecting letter along the position line. So, the string

Is collections.defaultdict thread-safe?

☆樱花仙子☆ 提交于 2019-11-27 23:20:28
问题 I have not worked with threading in Python at all and asking this question as a complete stranger. I am wondering if defaultdict is thread-safe. Let me explain it: I have d = defaultdict(list) which creates a list for missing keys by default. Let's say I have multiple threads started doing this at the same time: d['key'].append('value') At the end, I'm supposed to end up with ['value', 'value'] . However, if the defaultdict is not thread-safe, if the thread 1 yields to thread 2 after checking

convert a list of delimited strings to a tree/nested dict, using python

落爺英雄遲暮 提交于 2019-11-27 21:56:16
问题 I am trying to convert a list of dot-separated strings, e.g. ['one.two.three.four', 'one.six.seven.eight', 'five.nine.ten', 'twelve.zero'] into a tree (nested lists or dicts - anything that is easy to walk through). The real data happens to have 1 to 4 dot-separated parts of different length and has 2200 records in total. My actual goal is to fill in the set of 4 QComboBox'es with this data, in manner that the 1st QComboBox is filled with first set items ['one', 'five', 'twelve'] (no

Python defaultdict and lambda

我是研究僧i 提交于 2019-11-27 17:10:01
In someone else's code I read the following two lines: x = defaultdict(lambda: 0) y = defaultdict(lambda: defaultdict(lambda: 0)) As the argument of defaultdict is a default factory, I think the first line means that when I call x[k] for a nonexistent key k (such as a statement like v=x[k]), the key-value pair (k,0) will be automatically added to the dictionary, as if the statement x[k]=0 is first executed. Am I correct? And what about y? It seems that the default factory will create a defaultdict with default 0. But what does that mean concretely? I tried to play around with it in Python

Python: convert defaultdict to dict

我的梦境 提交于 2019-11-27 07:09:54
How can i convert a defaultdict number_to_letter defaultdict(<class 'list'>, {'2': ['a'], '3': ['b'], '1': ['b', 'a']}) to be a common dict? {'2': ['a'], '3': ['b'], '1': ['b', 'a']} You can simply call dict : >>> a defaultdict(<type 'list'>, {'1': ['b', 'a'], '3': ['b'], '2': ['a']}) >>> dict(a) {'1': ['b', 'a'], '3': ['b'], '2': ['a']} but remember that a defaultdict is a dict: >>> isinstance(a, dict) True just with slightly different behaviour, in that when you try access a key which is missing -- which would ordinarily raise a KeyError -- the default_factory is called instead: >>> a

python format string unused named arguments [duplicate]

妖精的绣舞 提交于 2019-11-27 06:48:13
This question already has an answer here: partial string formatting 15 answers Let's say I have: action = '{bond}, {james} {bond}'.format(bond='bond', james='james') this wil output: 'bond, james bond' Next we have: action = '{bond}, {james} {bond}'.format(bond='bond') this will output: KeyError: 'james' Is there some workaround to prevent this error to happen, something like: if keyrror: ignore, leave it alone (but do parse others) compare format string with available named arguments, if missing then add If you are using Python 3.2+, use can use str.format_map() . For bond, bond : >>> from

Is there a standard class for an infinitely nested defaultdict?

走远了吗. 提交于 2019-11-27 02:17:40
问题 Does anyone know if there's a standard class for an infinitely nestable dictionary in Python? I'm finding myself repeating this pattern: d = defaultdict(lambda: defaultdict(lambda: defaultdict(int))) d['abc']['def']['xyz'] += 1 If I want to add "another layer" (e.g. d['abc']['def']['xyz']['wrt'] ), I have to define another nesting of defaultdicts. To generalize this pattern, I've written a simple class that overrides __getitem__ to automatically create the next nested dictionary. e.g. d =

`if key in dict` vs. `try/except` - which is more readable idiom?

非 Y 不嫁゛ 提交于 2019-11-27 00:45:42
I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue on. Which way is better? try: A["blah"] = B["blah"] except KeyError: pass or if "blah" in B: A["blah"] = B["blah"] "Do and ask for forgiveness" vs. "simplicity and explicitness". Which is better and why? Exceptions are not conditionals. The conditional version is clearer. That's natural: this is straightforward flow control, which is what conditionals are