create dictionary from list - in sequence
By : Nanosoft
Date : March 29 2020, 07:55 AM
it helps some times Dictionaries don't have any order, use collections.OrderedDict if you want the order to be preserved. And instead of using indices use an iterator. code :
>>> from collections import OrderedDict
>>> lis = ['a', 1, 'b', 2, 'c', 3, 'd', 4]
>>> it = iter(lis)
>>> OrderedDict((k, next(it)) for k in it)
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
|
How to create list based on elements from dictionary?
By : James
Date : March 29 2020, 07:55 AM
With these it helps I have this list of dictionaries: , You can pass a generator to sorted: code :
>>> codes = sorted( d['code'] for d in L )
>>> codes
['AF', 'AM', 'UE']
codes = sorted([ d['code'] for d in L ])
|
R -- Create Variable in List of Data Frames Based on a Sequence
By : user2850740
Date : March 29 2020, 07:55 AM
help you fix your problem If you want to iterate over two lists/vectors simultaneously, you can either use mapply() or Map. Here's code using the latter code :
Map(function(x,z) cbind(x,z=z), ldf, 1:4)
|
How to create a list based on same value of a dictionary key
By : Adam Ward
Date : March 29 2020, 07:55 AM
I hope this helps . Iterate over group to sort out mins and maxs to separate keys of the dictionary: code :
def get_temp(temp):
return temp['date']
lst = []
for key, group in itertools.groupby(data, get_temp):
groups = list(group)
d = {}
d['date'] = key
d['temp_min'] = [x['temp_min'] for x in groups]
d['temp_max'] = [x['temp_max'] for x in groups]
lst.append(d)
print(lst)
|
Create a dictionary based on same key pairs from dictionary inside a list
By : Ashish Wadekar
Date : March 29 2020, 07:55 AM
like below fixes the issue defaultdict You can use collections.defaultdict with iteration. Given an input list of dictionaries L: code :
from collections import defaultdict
d = defaultdict(list)
for i in L:
d[i['c1'].strip()+'#'+i['c2']].append(i['keywords'])
print(d)
defaultdict(list,
{'Cars#Class': ['muv', 'hatchback', 'suv', 'sedan', 'coupe'],
'Cars#FuelType': ['electric', 'diesel', 'cng', 'petrol']})
|