Related
I have the json code below and I have a list
i want to do a for loop or if statement which
if label in selected_size:
fsize = id
selected_size[]
in selected size:
[7, 7.5, 4, 4.5]
in json:
removed
print(json_data)
for size in json_data:
if ['label'] in select_size:
fsize = ['id']
print(fsize)
i have no idea on how to do it.
You need to access to list and later to dict, for example:
json_data = [{'id': '91', 'label': '10.5', 'price': '0', 'oldPrice': '0', 'products': ['81278']}, {'id': '150', 'label': '9.5', 'price': '0', 'oldPrice': '0', 'products': ['81276']}, {'id': '28', 'label': '4', 'price': '0', 'oldPrice': '0', 'products': ['81270']}, {'id': '29', 'label': '5', 'price': '0', 'oldPrice': '0', 'products': ['81271']}, {'id': '22', 'label': '8', 'price': '0', 'oldPrice': '0', 'products': ['81274']}, {'id': '23', 'label': '9', 'price': '0', 'oldPrice': '0', 'products': ['81275']}, {'id': '24', 'label': '10', 'price': '0', 'oldPrice': '0', 'products': ['81277']}, {'id': '25', 'label': '11', 'price': '0', 'oldPrice': '0', 'products': ['81279']}, {'id': '26', 'label': '12', 'price': '0', 'oldPrice': '0', 'products': ['81280']}]
fsize = []
select_size = [7, 7.5, 4, 4.5]
[float(i) for i in select_size] #All select_size values to float value
for size in json_data:
if float(size['label']) in select_size: #For compare it i need float(size['label']) for convert to float.
fsize.append(size['id']) #Add to list
print(fsize) #Print all list, i get only 28
I am currently doing an Assignment; however, I got some interesting output which confused me so much.
I am trying to sort the following dictionary:
result = {'A1': '9', 'A2': '14', 'A3': '16', 'A4': '0', 'B1': '53', 'B2': '267', 'B3': '75', 'B4': '22', 'C1': '19', 'C2': '407', 'C3': '171', 'C4': '56', 'C5': '10', 'D3': '47', 'D4': '34', 'D5': '10'}
My sorting code with Python 3 is the following : (only sorted by value)
sortedList = [v for v in sorted(result.values())]
The output is :
['0', '10', '10', '14', '16', '171', '19', '22', '267', '34', '407', '47', '53', '56', '75', '9']
which is not fully sorted. The output is quite strange.
Why it is happened like this?
I have used another dict to test like this:
testD = {'A':'5','B': '9','c': '8','d': '6'}
the output is right :
['5', '6', '8', '9']
Is there something wrong with my result dictionary or is there something I am missing?
Strings will be ordered with a lexical sort. To sort your data numerically, convert the values into integers first.
Strings are compared one character at a time, so '30' < '4' because '3' < '4'. You need to use a key parameter to get the comparison based on the numeric value, not the string characters.
Also, it's redundant to use a list comprehension on something that already returns a list.
sortedList = sorted(result.values(), key=int)
As the value of dictionary are string so there is lexical sorting based on ascii values.
As evident, you need the values to be sorted according to their integer values.
result = {'A1': '9', 'A2': '14', 'A3': '16', 'A4': '0', 'B1': '53', 'B2': '267', 'B3': '75', 'B4': '22', 'C1': '19', 'C2': '407', 'C3': '171', 'C4': '56', 'C5': '10', 'D3': '47', 'D4': '34', 'D5': '10'}
As mentioned in the comments by #AChampion, you can pass the sort value type by using key something like this :
sortedList = sorted(result.values(), key = int)
print(sortedList)
Or you can do something like this :
result_ints = dict((k,int(v)) for k,v in result.items())
sortedList = [str(v) for v in sorted(result_ints.values())]
print(sortedList)
Both of the above code snippets will result in :
['0', '9', '10', '10', '14', '16', '19', '22', '34', '47', '53', '56', '75', '171', '267', '407']
You can try this :
result = [{'A1': '9', 'A2': '14', 'A3': '16', 'A4': '0', 'B1': '53', 'B2': '267', 'B3': '75', 'B4': '22', 'C1': '19', 'C2': '407', 'C3': '171', 'C4': '56', 'C5': '10', 'D3': '47', 'D4': '34', 'D5': '10'}]
lst=[]
for item in result:
for key in item.keys():
lst.append(int(item.get(key)))
sortedList = [v for v in sorted(lst)]
print(sortedList)
Output: [0, 9, 10, 10, 14, 16, 19, 22, 34, 47, 53, 56, 75, 171, 267,
407]
I have data that looks like this:
The columns are Name, ID, Dev ID, Date
('Anthony', '1', '10', '4/3/2017')
('Anthony', '1', '11', '5/2/2017')
('Anthony', '1', '13', '12/30/2017
('Anthony', '1', '15', '8/20/2017'
('Anthony', '4', '17', '2/3/2018')
('Anthony', '4', '18', '3/28/2017'
('Bob', '1', '111', '4/3/2017')
('Bob', '1', '200', '5/2/2017')
('Bob', '1', '113', '12/30/2017')
('Bob', '1', '115', '8/20/2017')
('Bob', '4', '117', '2/3/2018')
('Bob', '4', '118', '3/28/2017')
I'm trying to find unique Name's and ID's and then compare any dates and return only the one furthest in the future.
Ideally I want output that looks like:
('Anthony', '1', '12/30/2017')
('Anthony', '4', '2/3/2018')
('Bob', '1', '12/30/2017')
('Bob', '4', '2/3/2018')
I'm struggling because I have multiple keys and I can't figure out how to make it work. Any ideas?
Edit: This is only a sample I have 30ish people names and 10 unique id's. So i'm looking to make a For loop to figure this out.
You can use itertools.groupby combined with max to get output similar to what you're looking for.
import itertools
from datetime import datetime
data = [('Anthony', '1', '10', '4/3/2017'),
('Anthony', '1', '11', '5/2/2017'),
('Anthony', '1', '13', '12/30/2017'),
('Anthony', '1', '15', '8/20/2017'),
('Anthony', '4', '17', '2/3/2018'),
('Anthony', '4', '18', '3/28/2017'),
('Bob', '1', '111', '4/3/2017'),
('Bob', '1', '200', '5/2/2017'),
('Bob', '1', '113', '12/30/2017'),
('Bob', '1', '115', '8/20/2017'),
('Bob', '4', '117', '2/3/2018'),
('Bob', '4', '118', '3/28/2017')]
groups_with_max_date = []
for key, group in itertools.groupby(data, lambda d: (d[0], d[1])):
# convert to datetime and get max of group
group_max = max(group, key=lambda q: datetime.strptime(q[3], '%m/%d/%Y'))
groups_with_max_date.append(group_max)
groups_with_max_date
Gives us:
[('Anthony', '1', '13', '12/30/2017'),
('Anthony', '4', '17', '2/3/2018'),
('Bob', '1', '113', '12/30/2017'),
('Bob', '4', '117', '2/3/2018')]
The solution using datetime object, dict.setdefault(), max and datetime.strptime functions:
import datetime
l = [('Anthony', '1', '10', '4/3/2017'),('Anthony', '1', '11', '5/2/2017'),('Anthony', '1', '13', '12/30/2017'),('Anthony', '1', '15', '8/20/2017'),
('Anthony', '4', '17', '2/3/2018'),('Anthony', '4', '18', '3/28/2017'),('Bob', '1', '111', '4/3/2017'),('Bob', '1', '200', '5/2/2017'),
('Bob', '1', '113', '12/30/2017'),('Bob', '1', '115', '8/20/2017'),('Bob', '4', '117', '2/3/2018'),('Bob', '4', '118', '3/28/2017')]
d = {}
for t in l:
# grouping items by first two values of each tuple(accumulating `date` strings)
d.setdefault(t[0] +'-'+ t[1], []).append(t[3]) # first two values of a tuple are combined to be a "hash" key
# getting max date from the list of `datetime` objects
result = [(*k.split('-'), max(v, key=lambda dt: datetime.datetime.strptime(dt, '%m/%d/%Y'))) for k,v in sorted(d.items())]
print(result)
The output:
[('Anthony', '1', '12/30/2017'), ('Anthony', '4', '2/3/2018'), ('Bob', '1', '12/30/2017'), ('Bob', '4', '2/3/2018')]
old_list = [ ['ID0', 'ID1'], ['4', '8'], ['5', '6'] ]
I want convert list to new list
key = ['id', 'frame', 'length']
new_list = [{'id': 'ID0', 'frame': '4', 'length': '5'}, {'id': 'ID1', 'frame': '8', 'length': '6'}]
Here's a one-line approach:
>>> [{'id':x, 'frame':y, 'length':z} for x,y,z in zip(*old_list)]
[{'length': '5', 'frame': '4', 'id': 'ID0'}, {'length': '6', 'frame': '8', 'id': 'ID1'}]
new_list=[]
for x,y,z in zip(old_list[0],old_list[1], old_list[2]):
dict = {'id' : x, 'frame' : y, 'length': z}
new_list.append(dict)
Basically, I would like to identify whether the missing values in data set are continuously repeated or not. If there are countinously repeated missing values in the data set, I would like to know whether lengths of the each continuously repeated missing value sets are above certian number or not.
For example:
data =['1', '0', '9', '31', '11', '12', 'nan', '10', '44', '53', '12', '66', '99', '3', '2', '6.75833',....., 'nan', 'nan', 'nan', '3', '7', 'nan', 'nan']
In data above, the total number of 'nan' would be 6 and it could be calculated with data.count('nan'). However, what I want to know is how much continuously the missing value can be repeated. For this data, the answer would be 3.
I apologize that I don't show my example code, but I am a very novice in this area and I couldn't have any idea for coding.
Any idea, help or tips would be really appreciated.
This looks like a job for itertools.groupby():
>>> from itertools import groupby
>>> data =['1', '0', '9', '31', '11', '12', 'nan', '10', '44', '53',
'12', '66', '99', '3', '2', '6.75833', 'nan', 'nan', 'nan',
'3', '7', 'nan', 'nan']
>>> [len(list(group)) for key, group in groupby(data) if key == 'nan']
[1, 3, 2]
Note if your code actually has real NaNs instead of strings, the if key == 'nan'equality test should be replaced with math.isnan(key).
Or you can try this one, which is faster:
grouped_L = [sum(1 for i in group) for k,group in groupby(L)]
Using pyrle for speed. In this solution I replace nan with a number not in the data (-42). This is because nan is a difficult value for rles, as np.nan != np.nan and hence no nans are treated as consecutive.
import numpy as np
data =['1', '0', '9', '31', '11', '12', 'nan', '10', '44', '53', '12', '66', '99', '3', '2', '6.75833', 'nan', 'nan', 'nan', '3', '7', 'nan', 'nan']
arr = np.array([np.float(f) for f in data])
assert not -42 in arr
from pyrle import Rle
r = Rle(arr)
arr[np.isnan(arr)] = -42
is_nan = r.values == -42
np.max(r.runs[is_nan])
# 3