I have a set with time and each row either has a value of true or false and I want to print out the first row of true, then skip until it becomes false and print that alternating between those through the whole file
this is just an example of the set that I have the true values represent if the speed and distance to an object is less than a given number
What I want it to print
any ideas on how to do this in python
I've taken your data and converted it in to a list of Python dict structures. You could easily use a list of lists ([ [..], [..], ... ]) but a dict makes it easier to imagine when starting out
data = [
{'id': 1, "time": "10/03/2022 01:20", "bool": True},
{'id': 2, "time": "10/03/2022 01:20", "bool": True},
{'id': 3, "time": "10/03/2022 01:20", "bool": True},
{'id': 4, "time": "10/03/2022 01:20", "bool": False},
{'id': 5, "time": "10/03/2022 01:20", "bool": False},
{'id': 6, "time": "10/03/2022 01:20", "bool": False},
{'id': 7, "time": "10/03/2022 15:13", "bool": True},
{'id': 8, "time": "10/03/2022 15:13", "bool": True},
{'id': 9, "time": "10/03/2022 15:13", "bool": False},
{'id': 10, "time": "10/03/2022 15:13", "bool": True}
]
current_bool=None
for row in data:
if current_bool != row['bool']:
print(row)
current_bool=row['bool']
The output is:
{'id': 1, 'time': '10/03/2022 01:20', 'bool': True}
{'id': 4, 'time': '10/03/2022 01:20', 'bool': False}
{'id': 7, 'time': '10/03/2022 15:13', 'bool': True}
{'id': 9, 'time': '10/03/2022 15:13', 'bool': False}
{'id': 10, 'time': '10/03/2022 15:13', 'bool': True}
Related
dicts = {
'1': {
'name': 'hello1',
'roll': 1,
'artifacts': ['hello1']
},
'2': {
'name': 'hello2',
'roll': 2,
'artifacts': ['hello2']
},
'3': {
'name': 'hello3',
'roll': 3,
'artifacts': ['hello3']
}
}
keys = ['name', 'roll']
new_dict = {k: {key: v[key]} for k, v in dicts.items() for key in keys}
print(new_dict)
Current output:
{'1': {'roll': 1}, '2': {'roll': 2}, '3': {'roll': 3}}
I wanted to have the key name as well into new_dict.
I am able to do it manually, just trying to do it in pythonic way.
Try:
dicts = {
"1": {"name": "hello1", "roll": 1, "artifacts": ["hello1"]},
"2": {"name": "hello2", "roll": 2, "artifacts": ["hello2"]},
"3": {"name": "hello3", "roll": 3, "artifacts": ["hello3"]},
}
keys = ["name", "roll"]
new_dict = {k: {n: v[n] for n in keys} for k, v in dicts.items()}
print(new_dict)
Prints:
{
"1": {"name": "hello1", "roll": 1},
"2": {"name": "hello2", "roll": 2},
"3": {"name": "hello3", "roll": 3},
}
I have a data like this;
{"result": [{"name": "Mil", "age": 21, "id": 1}, {"name": "Jen", "age": 23, "id": 2}, {"name": "Rosa", "age": 26, "id": 3}]}
How can I get just the 'age' value of each individual from a data like this in Python?
You can access the ages like this:
d = {"result": [{"name": "Mil", "age": 21, "id": 1}, {"name": "Jen", "age": 23, "id": 2}, {"name": "Rosa", "age": 26, "id": 3}]}
ages = [res["age"] for res in d["result"]] # [21, 23, 26]
Indeed, d["result"] is a list of dicts. So, in a more expanded form, it is the same as:
d = {"result": [{"name": "Mil", "age": 21, "id": 1}, {"name": "Jen", "age": 23, "id": 2}, {"name": "Rosa", "age": 26, "id": 3}]}
results = d["result"] # a list
ages = []
for res in results:
age = res["age"]
ages.append(age)
print(ages) # [21, 23, 26]
You can this code:
dic={"result": [{"name": "Mil", "age": 21, "id": 1}, {"name": "Jen", "age": 23, "id": 2}, {"name": "Rosa", "age": 26, "id": 3}]}
lst = dic["result"]
age_lst =[]
for i in range(len(lst)):
age =lst[i]["age"]
age_lst = [age]+age_lst
print(age_lst)
for my internship, I need to build a nested object using parent IDs I don't want children attribute array.
I have an array of object with id and parent id and I use npm flatnest to do it. This works for a one-level hierarchy but the code must be adapted for a multi-hierarchy level.
I don't know how to adapt that to multi-hierarchy level.
This is my array of Object
var fn = require("flatnest");
const flat =
[
{ "id": 1, "name": 'Restaurants', 'parentId': 0},
{ "id": 2, "name": 'family restaurant', 'parentId': 1, 'value':'Excellent'},
{ "id": 3, "name": 'Sun restaurant', 'parentId': 1,'value':""},
{ "id": 4, "name": 'Sun restaurant 1', 'parentId': 3, 'value':'Good'},
{ "id": 5, "name": 'Sun restaurant 2', 'parentId': 3, 'value':"bad"},
{ "id": 6, "name": 'Hotels', 'parentId': 0,'value':""},
{ "id": 7, "name": 'Space Hotel', 'parentId': 6,'value':""},
{ "id": 8, "name": 'Sun Hotel', 'parentId': 7,'value':'Nice'},
{ "id": 9, "name": 'Moon Hotel', 'parentId': 7,'value':""},
{ "id": 10, "name": 'Moon Hotel 1', 'parentId': 9, 'value':"Excellent"},
{ "id": 11, "name": 'Moon Hotel 2', 'parentId': 9, 'value':"Worst"},
];
To use nested function of npm flatnest, I have to flat my array of Object (const flat)
My code to flat :
var transform={};
for(var i=0;i<flat.length;i++)
{
if(typeof flat[parseInt(i)+1] !== 'undefined' )
{
if(flat[i].id==flat[i+1].parentId)
{
var t = flat[i].name;
transform[t.concat(".").concat(flat[i+1].name)]=flat[i+1].value;
}else{
transform[t.concat(".").concat(flat[i+1].name)]=flat[i+1].value;
}
}
}
console.log(transform)
var nested = fn.nest(transform)
console.log(nested)
I expect the output of console.log(transform) to be
{ 'Restaurants.family restaurant':'Excellent',
'Restaurants.Sun restaurant.Sun restaurant 1': 'Good',
'Restaurants.Sun restaurant.Sun restaurant 2': 'bad',
'Hotels.Space Hotel.Sun Hotel': 'Nice',
'Hotels.Space Hotel.Moon Hotel.Moon Hotel 1': 'Excellent',
'Hotels.Space Hotel.Moon Hotel.Moon Hotel 2' : 'Worst'}
Then by using nested function :
var nested = fn.nest(transform)
console.log(nested)
The output must be exactly like that :
"Restaurants":{
"family restaurant":"Excellent",
"Sun restaurant":{
"Sun restaurant 1":"Good",
"Sun restaurant 2":"bad"
}
},
"Hotels":{
"Space Hotel":{
"Sun Hotel":"Nice",
"Moon Hotel":{
"Moon Hotel 1":"Excellent",
"Moon Hotel 2":"Worst"
}
}
}
}
but the actual output of console.log(transform) is :
{'Restaurants.family restaurant':'Excellent',
'Restaurant.Sun restaurant':'',
'Sun restaurant.Sun restaurant 1':'Good',
'Sun restaurant.Sun restaurant 2':'bad',
'Sun restaurant.Hotels':'',
'Hotels.Space Hotel':'',
'Space Hotel.Sun Hotel':'Nice'
'Space Hotel.Moon Hotel':'',
'Moon Hotel.Moon Hotel 1':'Excellent',
'Moon Hotel.Moon Hotel 2': 'Worst'}
I'm not using flatnest. But the below code works for me. Please check and let me know if it doesn't work for any scenario.
const flat = [{
"id": 1,
"name": 'Restaurants',
'parentId': 0
},
{
"id": 2,
"name": 'family restaurant',
'parentId': 1,
'value': 'Excellent'
},
{
"id": 3,
"name": 'Sun restaurant',
'parentId': 1,
'value': ""
},
{
"id": 4,
"name": 'Sun restaurant 1',
'parentId': 3,
'value': 'Good'
},
{
"id": 5,
"name": 'Sun restaurant 2',
'parentId': 3,
'value': "bad"
},
{
"id": 6,
"name": 'Hotels',
'parentId': 0,
'value': ""
},
{
"id": 7,
"name": 'Space Hotel',
'parentId': 6,
'value': ""
},
{
"id": 8,
"name": 'Sun Hotel',
'parentId': 7,
'value': 'Nice'
},
{
"id": 9,
"name": 'Moon Hotel',
'parentId': 7,
'value': ""
},
{
"id": 10,
"name": 'Moon Hotel 1',
'parentId': 9,
'value': "Excellent"
},
{
"id": 11,
"name": 'Moon Hotel 2',
'parentId': 9,
'value': "Worst"
},
];
const map = new Map();
const result = flat.reduce((acc, curr) => {
let val = {}
if (curr.parentId == 0)
acc[curr.name] = val;
else {
if (map.get(curr.parentId)) {
if (curr.value != '')
val = curr.value;
map.get(curr.parentId)[curr.name] = val;
}
}
map.set(curr.id, val);
return acc;
}, {});
console.log(JSON.stringify(result));
I have this list
result = [{"name": "A", "score": 35, "other_details": ""},
{"name": "A", "score": 60,"other_details": ""},
{"name": "B", "score": 45, "other_details": ""},
{"name": "B", "score": 34, "other_details": ""},
{"name":"C", "score": 65, "other_details": ""}]
Now, I want to get the whole dictionary on the basis of maximum score for each name.
My expected output is:
[{"name": "A", "score": 60,"other_details": ""}]
[{"name": "B", "score": 45, "other_details": ""}]
[{"name":"C", "score": 65, "other_details": ""}]
Using itertools.groupby
Ex:
from itertools import groupby
result = [{"name": "A", "score": 35, "other_details": ""},
{"name": "A", "score": 60,"other_details": ""},
{"name": "B", "score": 45, "other_details": ""},
{"name": "B", "score": 34, "other_details": ""},
{"name":"C", "score": 65, "other_details": ""}]
data_result = [max(list(v), key=lambda x: x["score"]) for k, v in groupby(sorted(result, key=lambda x: x["name"]), lambda x: x["name"])]
print(data_result)
Output:
[{'name': 'A', 'other_details': '', 'score': 60},
{'name': 'B', 'other_details': '', 'score': 45},
{'name': 'C', 'other_details': '', 'score': 65}]
If we have the following documents in elasticsearch:
[
{'name': 'John', 'time': '2013-01-01 12:01:00'},
{'name': 'John', 'time': '2013-01-01 12:02:00'},
{'name': 'John', 'time': '2013-01-01 12:03:00'},
{'name': 'John', 'time': '2013-01-01 12:04:00'},
{'name': 'Harry', 'time': '2013-01-01 12:05:00'},
{'name': 'Fred', 'time': '2013-01-01 12:06:00'},
{'name': 'Fred', 'time': '2013-01-01 12:07:00'}
]
And we facet over the 'name' field, we'll get something like this:
"facets": {
"count_per_name": {
"_type": "terms",
"missing": 0,
"total": 7,
"other": 0,
"terms": [
{
"term": "John",
"count": 4
},
{
"term": "Fred",
"count": 2
},
{
"term": "Harry",
"count": 1
}
]
}
}
My question is this: is it possible to perform a faceting query in elasticsearch whereby those documents with the name "John" count as "half" documents? This would lead to John's count falling from 4 to 2, but Fred's and Harry's remaining the same:
"facets": {
"count_per_name": {
"_type": "terms",
"missing": 0,
"total": 5,
"other": 0,
"terms": [
{
"term": "John",
"count": 2
},
{
"term": "Fred",
"count": 2
},
{
"term": "Harry",
"count": 1
}
]
}
}
you could play with http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html
and specify value_script, where return 0.5 for John and 1 for other guys, and operate with SUM facet result. Though this approach is performance affected