I am trying to find the highest daily high within daily kline data from binance. I am able to API call the list of lists with this code.
client.get_historical_klines('maticbtc', Client.KLINE_INTERVAL_1DAY, "2 days ago UTC")
The output is a list of daily historical prices in 'open' 'high' 'low' 'close' 'volume' format.
[[1599264000000,
'0.00000185',
'0.00000192',
'0.00000171',
'0.00000177',
'208963036.00000000',
1599350399999,
'377.04825679',
14595,
'82785887.00000000',
'150.17277108',
'0'],
[1599350400000,
'0.00000177',
'0.00000185',
'0.00000170',
'0.00000182',
'114643846.00000000',
1599436799999,
'204.99814224',
9620,
'55503278.00000000',
'99.62131279',
'0']]
I would like to find the highest 'high' value in this list. I am currently able to reference a single daily 'high' value using this code:
client.get_historical_klines('maticbtc', Client.KLINE_INTERVAL_1DAY, "30 days ago UTC")[0][2]
output:
0.00000192
Thank you for your suggestions!
I would like to find the highest 'high' value in this list.
The example you present is not a list, it is a list of lists:
data30days = [
[ ... ],
[ ... ],
...
]
In general the "highest" value is nothing else than the maximum. Therefore the code finding the maximums in such a list of lists would be:
maxima30days = [ max(x) for x in data30days ]
totalMaximum = max(maxima30days)
But there is one thing that is odd: The return data of your API. You do not receive a list of numeric values but a record of data of mixed type. Luckily the documentation of binance provides the information which value is the value you are looking for: It seems to be the third. (See: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#klinecandlestick-data) Why this value is returned as string is unclear to me.
Therefore your code would be:
maxima30days = [ float(x[2]) for x in data30days ]
totalMaximum = max(maxima30days)
One little detail: The next time you ask a question please provide information which module you're using. In other situations this piece of information might be very essential! (Luckily here it is not.)
Please have in mind that I am not able to test the code above as unfortunately you did not provide a working example I could build upon. Therefore please test it yourself. Feel free to add a comment to my answer if you encounter any errors, I'll then try to resolve any further issues if there are any.
Assuming that you have your data as above in a variable called values,
>>> values
[[1599264000000, '0.00000185', '0.00000192', '0.00000171', '0.00000177', '208963036.00000000', 1599350399999, '377.04825679', 14595, '82785887.00000000', '150.17277108', '0'], [1599350400000, '0.00000177', '0.00000185', '0.00000170', '0.00000182', '114643846.00000000', 1599436799999, '204.99814224', 9620, '55503278.00000000', '99.62131279', '0']]
if you want the maximum value of the third element in each sublist of values (converted to float because you don't want to do a string comparison), you can do for example:
>>> max(float(lst[2]) for lst in values)
1.92e-06
Related
before you all tell me to search for stuff, let me tell you "I HAVE".
I have searched quite a bit. Forums, educational/training sites, stackexchange and all the regulars. I haven't been able to get what I need. I am not running to SE at the first sign of trouble either. I have registered on SE quite a while ago and this is my first question.
I have found some replies(suggestions, not solutions) and some ANSWERS(sort of) too. However, the most useful thing I have found is https://stackoverflow.com/a/6181978/15065496
I do know what dictionaries are, I know how to use them.
I have tried the answer above too, but it isn't exactly what I want.
I am a mechanical engineer learning python to solve some engineering problems that I am having. Please read till the end to get a better idea of what I am trying to achieve.
I need to create a list of lists with names. I have been able to create a list of lists, which looks like this:
list_xyz = []
for i in range(1,5,1):
temp_array_name = "list_" + str(i)
list_xyz.append(temp_array_name)
>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']
Now, I want to create a new list out of each of the elements.
Basically, what I want is to create lists called list_1, list_2, list_3, list_4.
I want to automate the task of doing
list_1 = []
list_2 = []
list_3 = []
list_4 = []
what I have already tried are:
for i in list_xyz:
i = list()
*this gives:*
>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']
*I thought good*. Then I typed:
>>> list_1
*I was expecting the following *
[]
*Instead, I got:*
Traceback (most recent call last):
File "<pyshell#93>", line 1, in <module>
list_1
NameError: name 'list_1' is not defined
*Then I tried the following and understood what had happened.*
>>> i
[]
*So then, I tried:*
for i in range(0,len(list_xyz),1):
list_xyz[i] = list()
*this gave:*
>>> list_xyz
[[], [], [], []]
Now, technically I could use the elements of list_xyz, but calling them would be a pain.
If I need the third element inside the second element of list_xyz, then I would have to say
list_xyz[1][2]
Instead, if they were named, I could just say list_2[2] and could get the element I was looking for.
I have also tried using dictionaries, as suggested by https://stackoverflow.com/users/455276/the-wolf in https://stackoverflow.com/a/6181978/15065496.
What I have tried is:
>>> dict1 = {}
>>> list_pqr = ['list_1', 'list_2', 'list_3', 'list_4']
>>> for i in range(0,len(list_pqr),1):
dict1[list_pqr[i]] = list()
>>> dict1
{'list_3': [], 'list_2': [], 'list_1': [], 'list_4': []}
This is close to what I want but isn't exactly what I want. Please read the following to understand exactly what I want.
The reason I need this particular list this exact way is:
I am trying to calculate some temperatures at different times at different positions.
I want to create a dictionary with times as keys and a list of temperatures at various locations as the values.
and another dictionary with locations as keys and list temperatures at different times as the values.
What I want is:
dict1 = {time1: temps_at_time_1
time2: temps_at_time_2
time3: temps_at_time_3
time4: temps_at_time_4
time5: temps_at_time_5
}
dict2 = {loc1: temps_at_times_L1
loc2: temps_at_times_L2
loc3: temps_at_times_L3
loc4: temps_at_times_L4
loc5: temps_at_times_L5
}
where temps_at_times_L1 is the list of temperatures at location 1 at various times.
where temp_at_time_1 is the list of temperatures at Time 1 at various locations.
let's say there are 25 locations and 100 timesteps, then:
len(temps_at_times_L1) = 100
len(temp_at_time_1) = 25
now, if temps_at_times_L1 and temp_at_time_1 are variables that I could call, It would make it very easy for me to plot the temperatures etc.
Basically, I want the same thing that I have achieved with dictionaries after using the wolf's suggestion, but the lists created there should have names that I could call so that when calculating temperatures, I could just update the lists by calling the append() method.
If I am calculating timestep1, I wanna be able to just say:
while(timestep == 1):
for i in temperature_location_array:
temps_at_time_1.append(i)
Even better would be if i was able to do the following:
for a in range(startTime, endTime, stepSize):
while(timestep == a):
for i in temperature_location_array:
temps_at_time_a.append(i) or vars("temps_at_time_" + str(a)).append(i)
I'm pretty sure the last line will not work and I will have to rack my brains again for that. I have tried reading about how vars() works but it is giving me a headache.
If you guys can't help me, and/or tell me that the last line of my code is stooooopid, then I'll just have to make do with the basic dictionary and ridiculously complicated(for my level anyway) indexing and calling and assigning directly to the values dictionaries.
I have previously faced the same need whilst editing a spreadsheet using openpyxl, but was unable to create lists from variable names inside another list. I gave up and used another method that got the job done. However, this time I would like to know how to do it: if at all it can be done.
Thank you all for the trouble of reading through my long winded explanation. I did this so people who reply will understand exactly what I need and will not give suggestions that would not serve my purpose. I will read all suggestions and take all useful ones into account though. I am not trying to offend/insult anyone.
Sorry for the negativity but appending numbers to variables is the exact opposite what any programmer should be doing - nested_lists[1][2] is the correct way of handling nested lists, nested_list_1[2] is not. Do not write data into the names of variables, just don't, it won't end well and globals() is terrible misuse here.
What you need is some proper structure that will give meaning to your data. I would recommend pandas library. It's like Excel for Python.
Let's take the following example of 5 measurements in two locations foo and bar at various times.
# Assuming the raw data is in format:
# (time, temperature, location)
import pandas as pd
data=pd.DataFrame({
(1,12, 'foo'),
(2,12,'bar'),
(10,23,'foo'),
(3,12,'foo'),
(3,15,'bar')
},columns=['time','temperature','location'])
print(data)
yields:
time temperature location
0 1 12 foo
1 2 12 bar
2 3 15 bar
3 10 23 foo
4 3 12 foo
I cannot enumerate all the things you can do with a dataframe but it can be sorted, filtered, mapped, indexed... see the documentation or search for answers here.
You might be interested in e.g. filtering data by location data[data['location']=='foo']
time temperature location
0 1 12 foo
3 10 23 foo
4 3 12 foo
or setting the dataframe's index to time column and indexing using it:
new_data = data.set_index('time')
print(new_data.loc[3])
temperature location
time
3 15 bar
3 12 foo
As several commenters are mentioned before me you actually can create variables programatically, but is a very ugly way of solving a problem.
Anyway, you can add variables to the global namespace with globals() as follows:
>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']
>>> for n in list_xyz:
... globals()[n] = list()
...
>>> list_1
[]
>>> list_2
[]
>>> list_3
[]
>>> list_4
[]
>>>
I have a dict of terms (words) and scores assigned to them, like:
{'goose': 12.34521, 'egg': 8.54021}
I have a task to sort them by score, and when scores are equal then lexicographically by key, then simply print top 10 terms in format:
term1, term2, ..., term10
This is what I've done:
# term_idf is dict as explained above
term_idf_sorted = sorted(term_idf_dict.items(), key=functools.cmp_to_key(cmp_term_score))
terms_sorted = list(map(lambda p: p[0], term_idf_sorted[:n_top_terms]))
print(", ".join(terms_sorted))
where compare function is
def cmp_term_score(term_score_1, term_score_2):
if term_score_1[1] == term_score_2[1]:
return term_score_1[0].casefold() < term_score_2[0].casefold()
else:
return term_score_2[1] - term_score_1[1]
When i create dict based on some chunk of text and then print a sorted version, I get something like:
[('parish', 7.427144133408616), ('saar', 4.406719247264253), ('saaremaa', 4.406719247264253), ('jõe', 4.406719247264253), ('villag', 4.208268308540415) ...]
The problem is that 'jõe' should come before 'saar' and 'saaremaa' but when I run app multiple times, sometimes 'jõe' ends up in the middle, and sometimes in the first place, which really confuses me.
I tried to change comparing function but then my other test cases fail due to this lexicographical comparison.
[This second fail occurs on {'egg': 3.05, 'descend': 3.05} where 'egg' ends up printed before 'descend', but 'descend' should be first]
How can I leverage this lexicographical sort (as second priority) to be consistent?
Note. Dict terms are read from a file as utf-8 string.
By default tuples are compared in field order. That is, tuples are sorted by their first fields and in the case of ties the second fields are compared, etc. So, if your challenges is how to sort by score followed by name it may be as simple as leveraging this inherent feature of tuples with one wrinkle: you want the numeric sort to be from high to low, while you want the lexicographical sort to be from low to high. The following example does that albeit in a somewhat tricky way.
Example:
word_scores = [('parish', 7.427144133408616), ('saar', 4.406719247264253), ('saaremaa', 4.406719247264253), ('jõe', 4.406719247264253), ('villag', 4.208268308540415)]
sorted_word_scores = sorted(word_scores, key=lambda ws: (0 - ws[1], ws[0]))
print(sorted_word_scores)
Output:
[('parish', 7.427144133408616), ('jõe', 4.406719247264253), ('saaremaa', 4.406719247264253), ('saar', 4.406719247264253), ('villag', 4.208268308540415)]
I'm new to python and I've been trying to write a program to track the total value of my steam inventory. I've been able to retrieve all my skins and the prices, but the issue is isolating the price output from the steam_community_market.market module output, which is a list with a lot of unnecessary information. I haven't been able to find how to filter only specific strings from the lists. An example of the format of the list I'm trying to filter is:
skins_prices = [
{'Mann Co. Supply Crate Key': {
'success': True,
'lowest_price': '$2.50',
'volume': '6,489',
'median_price': '$2.45'},
'AK-47 | Redline (Field-Tested)': {
'success': True,
'lowest_price': '$15.00',
'volume': '749',
'median_price': '$14.78'}
}
]
I haven't gotten to the part of adding the cost together, because first I need to isolate the "lowest price" for each item, how could I do that so I can use that number to calculate the total value/cost (meaning it needs to be a float)? Please tell me if you need more details or have any questions. Anything helps, thanks in advance.
This is a problem of nested dictionary as identified in the comment. Here's is a sample code to help out:
key = 'lowest_price'
lowest_prices = {}
for k, v in skins_prices[0].items():
for k1, v1 in v.items():
if k1 == key:
lowest_prices[k] = float(v1[1:])
print(lowest_prices)
output (I have removed the $ and converted to float):
{'Mann Co. Supply Crate Key': 2.5, 'AK-47 | Redline (Field-Tested)': 15.0}
Now you can use the new dictionary to manipulate the values. For example getting the sum total is done as below:
>> sum(list(lowest_prices.values()))
>> 17.5
HTH.
I think you need:
low_prices = []
# as your list has only 1 component we are using `[0]` and iterating over it
for _, v in skins_prices[0].items():
low_prices.append(float(v["lowest_price"][1:]))
print(low_prices)
Output:
[2.5, 15.0]
explanation
v["lowest_price"][1:] we are using indexing to ignore the first character from str which is $ so now value will be 2.5 or 15.
On this we are applying float to convert the str into float and appending it to new list which is low_prices
I am trying to get proportion of nouns in my text using the code below and it is giving me an error. I am using a function that calculates the number of nouns in my text and I have the overall word count in a different column.
pos_family = {
'noun' : ['NN','NNS','NNP','NNPS']
}
def check_pos_tag(x, flag):
cnt = 0
try:
for tag,value in x.items():
if tag in pos_family[flag]:
cnt +=value
except:
pass
return cnt
df2['noun_count'] = df2['PoS_Count'].apply(lambda x: check_pos_tag(x, 'noun')/df2['word_count'])
Note: I have used nltk package to get the counts by PoS tags and I have the counts in a dictionary in PoS_Count column in my dataframe.
If I remove "/df2['word_count']" in the first run and get the noun count and include it again and run, it works fine but if I run it for the first time I get the below error.
ValueError: Wrong number of items passed 100, placement implies 1
Any help is greatly appreciated
Thanks in Advance!
As you have guessed, the problem is in the /df2['word_count'] bit.
df2['word_count'] is a pandas series, but you need to use a float or int here, because you are dividing check_pos_tag(x, 'noun') (which is an int) by it.
A possible solution is to extract the corresponding field from the series and use it in your lambda.
However, it would be easier (and arguably faster) to do each operation alone.
Try this:
df2['noun_count'] = df2['PoS_Count'].apply(lambda x: check_pos_tag(x, 'noun')) / df2['word_count']
for line in open('transactions.dat','r'):
item=line.rstrip('\n')
item=item.split(',')
custid=item[2]
amt=item[4]
if custid in cust1:
a=cust1[custid]
b=amt
c=(a)+(b)
print(cust1[custid]+" : "+a+" :"+b+":"+c)
break
else:
cust1[custid]=amt
Output:
85.91 : 85.91 :85.91:85.9185.91
Well above is my code what I want is
when I read from a file I want to add the customer amount with same
id.
Secondly there should not be repetition of customer id in my
dictionary.
so I am trying to add customer amount which is c but it gives me appended string instead of adding the two. You can see in the last part of my output which is value of c. So how do I add the values.
Sample transaction data:
109400182,2016-09-10,119257029,1094,40.29
109400183,2016-09-10,119257029,1094,9.99
377700146,2016-09-10,119257029,3777,49.37
276900142,2016-09-10,135127654,2769,23.31
276900143,2016-09-10,135127654,2769,25.58
You reading strings, instead of floats, from the file. Use this amt=float(item[4]) to convert strings representing numbers to floats, and then print(str(cust1[custid])+" : "+str(a)+" :"+str(b)+":"+str(c)) to print out.
Your code may need lots of refactor, but in a nutshell and if I understand what you are trying to do you could do
c = float(a) + float(b)
and that should work.