Python dict values to list of integers - python

I'm fairly new to Python. I have a list of dictionaries.
[{"x": "23"}, {"x": "14"}, {"x": "9"}, {"x": "19"}, {"x": "21"}, {"x": "14"}, {"x": "16"}, {"x": "11"}, {"x": "9"}, {"x": "6"}, {"x": "24"}, {"x": "13"}]
I want to extract the values associated with the x key and plot them. To do that the list must look like this, and the values must be integers (or floats).
[23, 14, 9, 19, 21, 14, 16, 11, 9, 6, 24, 13]
My question is: how do I
extract these values, and
convert them to integers?

This can be done using a list comprehension using the dictionary's get method.
>>> l = [{"x": "23"}, {"x": "14"}, {"x": "9"}, {"x": "19"}, {"x": "21"}, {"x": "14"}, {"x": "16"}, {"x": "11"}, {"x": "9"}, {"x": "6"}, {"x": "24"}, {"x": "13"}]
>>> [i.get('x') for i in l]
['23', '14', '9', '19', '21', '14', '16', '11', '9', '6', '24', '13']
If you'd like them as integers, convert them in the list comprehension
>>> [int(i.get('x')) for i in l]
[23, 14, 9, 19, 21, 14, 16, 11, 9, 6, 24, 13]

Use a list comprehension. For each item get "x" and cast to int:
[int(item['x']) for item in mylist]
If you need floats change int to float.

You can use list comprehension
>>> mylist = [{'x': '23'}, {'x': '14'}, {'x': '9'}, {'x': '19'}, {'x': '21'}, {'x': '14'}, {'x': '16'}, {'x': '11'}, {'x': '9'}, {'x': '6'}, {'x': '24'}, {'x': '13'}]
>>> [ int(item.values()[0]) for item in mylist]
[23, 14, 9, 19, 21, 14, 16, 11, 9, 6, 24, 13]
or map
>>> map(lambda x: int(x['x']), mylist)
[23, 14, 9, 19, 21, 14, 16, 11, 9, 6, 24, 13]

Related

Annotations in facet plots plotly

I'm creating a facet plot with plotly, which has two columns and a single row. I also have a list of dict for annotations, which looks like this...
annots = [{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 398000), 'y': 125.5, 'text': '8', 'font': {'color': 'black'}},
{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 545000), 'y': 123.5, 'text': '3', 'font': {'color': 'black'}},
{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 583000), 'y': 120.5, 'text': '9', 'font': {'color': 'black'}}]
I create layout dictionary and pass it to the figure object like this...
layout = dict(showlegend=False, height=HEIGHT, annotations=annots, barmode='overlay', hoverlabel=hoverlabel, legend=legend, margin=margin, xaxis=xaxes1, xaxis2=xaxes2, yaxis=yaxes1, yaxis2=yaxes2)
fig = go.Figure(data=data, layout=layout)
But all the annotations are displayed in the first column. How do I specify which annotations belong to which facet plot ?
It was very easy, I just need to specify xref and yref as keys in the dictionary. So it looks like this...
annots = [{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 398000), 'y': 125.5, 'text': '8', 'xref'='x2', 'font': {'color': 'black'}},
{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 545000), 'y': 123.5, 'text': '3', 'xref'='x2', 'font': {'color': 'black'}},
{'x': datetime.datetime(2021, 5, 5, 14, 4, 47, 583000), 'y': 120.5, 'text': '9', 'xref'='x', 'font': {'color': 'black'}}]

Scatter plot line to out of order data

I am tracking the movements of an avian animal. I have detection points on an xy plot. I want to connect the previous detected point to the next detection, regardless of direction. This will assist with removing extraneous detections.
Data Sample:
Sample input
The goal is to have a line from the previous data point to the next point.
Sample output
Unsuccessful method 1:
plt.figure('Frame',figsize=(16,12))
plt.imshow(frame)
plt.plot(x, y, '-ro', 'd',markersize=2.5, color='orange')
Method 1 output
Unsuccessful method 2:
plt.plot(np.sort(x), y[np.argsort(x)], '-bo', ms = 2)
Method 2 output
I used your sample data and make a plot with method 1 (but with pandas) and the output was as you expected. I don't understand why you have an unsuccessful result.
data = [{'frame': 1, 'x': 5, 'y': 15},
{'frame': 4, 'x': 10, 'y': 15},
{'frame': 5, 'x': 15, 'y': 15},
{'frame': 6, 'x': 20, 'y': 15},
{'frame': 7, 'x': 23, 'y': 20},
{'frame': 8, 'x': 25, 'y': 25},
{'frame': 11, 'x': 20, 'y': 23},
{'frame': 15, 'x': 15, 'y': 20},
{'frame': 18, 'x': 8, 'y': 18},
{'frame': 19, 'x': 8, 'y': 10},
{'frame': 20, 'x': 12, 'y': 7}]
df = pd.DataFrame(data).sort_values('frame')
df.plot(x='x', y='y')

Separating "objects" within a series to be in separate columns in Python

I have some data that I have managed to put into a series in Python there are 369 elements in the series, within each element, there is a further two arrays containing starting x and y co-ordinates and ending x and y co-ordinates. I am looking to restructure this series in a simple data table with 369 entries and 4 columns.
First 10 Elements of the Series is
0 [{'y': 52, 'x': 50}, {'y': 44, 'x': 40}]
1 [{'y': 44, 'x': 40}, {'y': 75, 'x': 33}]
2 [{'y': 75, 'x': 33}, {'y': 76, 'x': 42}]
3 [{'y': 76, 'x': 42}, {'y': 36, 'x': 28}]
4 [{'y': 36, 'x': 28}, {'y': 12, 'x': 34}]
5 [{'y': 12, 'x': 34}, {'y': 30, 'x': 32}]
6 [{'y': 30, 'x': 32}, {'y': 70, 'x': 30}]
7 [{'y': 70, 'x': 30}, {'y': 35, 'x': 28}]
8 [{'y': 35, 'x': 28}, {'y': 23, 'x': 33}]
9 [{'y': 83, 'x': 46}, {'y': 87, 'x': 48}]
Name: list, dtype: object
By Using this, I can access the first element within that series, but ideally I want to be able to access each individual 'y' and 'x' value within these elements
passinglocations[1]
[{'y': 44, 'x': 40}, {'y': 75, 'x': 33}]
I cannot seem to find any further information in which I understand to get this in the usable form I want it
Any Insights?
Thanks
Assuming that your four columns are your y, x, y, x values, this should work:
passinglocations = [
[{'y': 44, 'x': 40}, {'y': 75, 'x': 33}],
[{'y': 23, 'x': 15}, {'y': 25, 'x': 37}]
]
def transform(passinglocations):
return [(loc[0]['y'], loc[0]['x'], loc[1]['y'], loc[1]['x']) for loc in passinglocations]
print(transform(passinglocations))
output:
[(44, 40, 75, 33), (23, 15, 25, 37)]

Add String to list items and then create a dict in python

I have two list and i would like to create dict with each list where key value is a string and then combine those two dicts in one, below are my list :
list_1 : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
list_2 : ['BACKUP_INFO', 'sqlite_sequence', 'BACKUP_INFO_SEARCH', 'BACKUP_INFO_SEARCH_content', 'BACKUP_INFO_SEARCH_segments', 'BACKUP_INFO_SEARCH_segdir', 'BACKUP_INFO_SEARCH_docsize', 'BACKUP_INFO_SEARCH_stat', 'FILE_INFO', 'FILE_INFO_SEARCH', 'FILE_INFO_SEARCH_content', 'FILE_INFO_SEARCH_segments', 'FILE_INFO_SEARCH_segdir', 'FILE_INFO_SEARCH_docsize', 'FILE_INFO_SEARCH_stat']
List_1 should be added with dict key value as 'id'
List_2 should be added with dict key value as 'table'
Then, both the above dicts should be combined into one dict to form something similar to this :
{
"output":
{
"id": 1,
"table" : BACKUP_INFO
}
{
"id": 2,
"table" :sqlite_sequence
}
}
But, i am getting the below output using
table_list_out = dict(zip(list_1, list_2))
return { 'output' : {'id' : list_1, 'table_name' : list_2}}:
{
"output": {
"id": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15
],
"table_name": {
"1": "BACKUP_INFO",
"2": "sqlite_sequence",
"3": "BACKUP_INFO_SEARCH",
"4": "BACKUP_INFO_SEARCH_content",
"5": "BACKUP_INFO_SEARCH_segments",
"6": "BACKUP_INFO_SEARCH_segdir",
"7": "BACKUP_INFO_SEARCH_docsize",
"8": "BACKUP_INFO_SEARCH_stat",
"9": "FILE_INFO",
"10": "FILE_INFO_SEARCH",
"11": "FILE_INFO_SEARCH_content",
"12": "FILE_INFO_SEARCH_segments",
"13": "FILE_INFO_SEARCH_segdir",
"14": "FILE_INFO_SEARCH_docsize",
"15": "FILE_INFO_SEARCH_stat"
}
}
}
You can use a list comprehension:
list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
list_2 = ['BACKUP_INFO', 'sqlite_sequence', 'BACKUP_INFO_SEARCH', 'BACKUP_INFO_SEARCH_content', 'BACKUP_INFO_SEARCH_segments', 'BACKUP_INFO_SEARCH_segdir', 'BACKUP_INFO_SEARCH_docsize', 'BACKUP_INFO_SEARCH_stat', 'FILE_INFO', 'FILE_INFO_SEARCH', 'FILE_INFO_SEARCH_content', 'FILE_INFO_SEARCH_segments', 'FILE_INFO_SEARCH_segdir', 'FILE_INFO_SEARCH_docsize', 'FILE_INFO_SEARCH_stat']
new_dict = {'output':[{'id':a, 'table':b} for a, b in zip(list_1, list_2)]}
Output:
{'output': [{'table': 'BACKUP_INFO', 'id': 1}, {'table': 'sqlite_sequence', 'id': 2}, {'table': 'BACKUP_INFO_SEARCH', 'id': 3}, {'table': 'BACKUP_INFO_SEARCH_content', 'id': 4}, {'table': 'BACKUP_INFO_SEARCH_segments', 'id': 5}, {'table': 'BACKUP_INFO_SEARCH_segdir', 'id': 6}, {'table': 'BACKUP_INFO_SEARCH_docsize', 'id': 7}, {'table': 'BACKUP_INFO_SEARCH_stat', 'id': 8}, {'table': 'FILE_INFO', 'id': 9}, {'table': 'FILE_INFO_SEARCH', 'id': 10}, {'table': 'FILE_INFO_SEARCH_content', 'id': 11}, {'table': 'FILE_INFO_SEARCH_segments', 'id': 12}, {'table': 'FILE_INFO_SEARCH_segdir', 'id': 13}, {'table': 'FILE_INFO_SEARCH_docsize', 'id': 14}, {'table': 'FILE_INFO_SEARCH_stat', 'id': 15}]}
From the looks of things, your desired output is impossible. Notice that there are multiple values corresponding to the key: "output".
What is possible is something like this, where the value corresponding to 'output' is a list of dictionaries.
return {'output': [{'id': x, 'table': y} for x, y in zip(list1, list2)]}
You can just loop through it, I'm sure there's a one-liner but this is rather clear.
output = {"output":{} }
for i in xrange(0, len(list_1)):
output["output"][list_2[i]] = list_1[i]
print output

Difficulty getting the item count for the combinations of list of items from python dictionary

I have below input list of dictionaries
inpdata = {"cat": [{"categories": [{"cid": 27}, {"cid": 66}, {"cid": 29}], "id": 20},
{"categories": [{"cid": 66}], "id": 21},
{"categories": [{"cid": 66}, {"cid": 27}], "id": 22},
{"categories": [{"cid": 66}, {"cid": 27}], "id": 23},
{"categories": [{"cid": 66}, {"cid": 29}, {"cid": 27}], "id": 24}]};
Am trying to get the count of id's for each cid along with the id values, I used below code for that -
allcategories = set( sec['cid'] for record in inpdata['cat'] for sec in record['categories'] )
summarize = lambda record: record['id']
fs_cat = [
{
'cat':cid,
'count':len(matches),
'ids':[ summarize( match ) for match in matches ]
}
for cid in allcategories
for matches in [[
record for record in inpdata['cat'] if cid in [ sec['cid'] for sec in record['categories'] ]
]]
]
print(fs_cat)
This gives the output as -
[{'cat': 66, 'count': 5, 'ids': [20, 21, 22, 23, 24]},
{'cat': 27, 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': 29, 'count': 2, 'ids': [20, 24]}
]
But how can I get the combination of the categories {66,27,29} ?
I tried using below approach for getting the combinations of this input - it gives the combination of items from the list
allcategories = {66,27,29}
for subset in itertools.chain.from_iterable(itertools.combinations(allcategories, n) for n in range(len(allcategories) + 1)):
print(subset)
But I couldn't figure out how can I use this approach to get me the result as below for categories {66,27,29} from the 'inpdata'
result=[{'cat': '66', 'count': 5, 'ids': [20, 21, 22, 23, 24]},
{'cat': '27', 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': '29', 'count': 2, 'ids': [20, 24]},
{'cat': '66&27', 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': '66&29', 'count': 2, 'ids': [20, 24]},
{'cat': '27&29', 'count': 2, 'ids': [20, 24]},
{'cat': '66&27&29', 'count': 2, 'ids': [20, 24]}
]
Could you please suggest on how I can achieve this?
itertools.combinations(1), itertools.combinations(2), ... upto itertools.combinations(n) will give you all combinations of fs_cat (where, n = len(fs_cat))
import itertools
import operator
from functools import reduce
fs_cat = [
{'cat': 66, 'count': 5, 'ids': [20, 21, 22, 23, 24]},
{'cat': 27, 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': 29, 'count': 2, 'ids': [20, 24]},
]
result = []
for n in range(1, len(fs_cat) + 1): # 1, 2, ..., len(fs_cat)
for xs in itertools.combinations(fs_cat, n):
cat = '&'.join(map(str, sorted(x['cat'] for x in xs)))
ids = sorted(reduce(operator.and_, (set(x['ids']) for x in xs)))
result.append({'cat': cat, 'count': len(ids), 'ids': ids})
>>> result
[{'cat': '66', 'count': 5, 'ids': [20, 21, 22, 23, 24]},
{'cat': '27', 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': '29', 'count': 2, 'ids': [20, 24]},
{'cat': '27&66', 'count': 4, 'ids': [20, 22, 23, 24]},
{'cat': '29&66', 'count': 2, 'ids': [20, 24]},
{'cat': '27&29', 'count': 2, 'ids': [20, 24]},
{'cat': '27&29&66', 'count': 2, 'ids': [20, 24]}]

Categories

Resources