Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have written the following program in Python:
s = []
for e in random_key:
s = str(e)
print(s)
where the list random_key is
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
The output of the program is
1111011010110011
1011000110011100
0011011001100010
0000011100100001
1111111010000100
0110110101100011
1011100011000101
1011101011100010
1101101101001010
1000011110110000
which is not correct. How can I fix the code?
If I am able to read your thoughts (not sure about that ..). Would you like them to 10 based numbers?
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
numbers = [int(x, 2) for x in random_key]
print(numbers)
output
[16101, 1531, 14772, 34130, 13007, 56179, 50171, 2100, 24229, 39265]
Do you mean this?
s = list()
for e in random_key:
s.append(str(e))
print(s)
Returns:
['0011111011100101', '0000010111111011', '0011100110110100', '1000010101010010', '0011001011001111', '1101101101110011', '1100001111111011', '0000100000110100', '0101111010100101', '1001100101100001']
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am using nsepython library for data extraction. I was extracting lot size for particular symbol from function nse_get_fno_lot_sizes of nsepython library.
I want the output of function nse_get_fno_lot_sizes in dataframe format instead of current format.
Complete Code - https://github.com/aeron7/nsepython/blob/master/nsepython/rahu.py
nse_get_fno_lot_sizes function-
def nse_get_fno_lot_sizes(symbol="all",mode="list"):
url="https://archives.nseindia.com/content/fo/fo_mktlots.csv"
if(mode=="list"):
s=requests.get(url).text
res_dict = {}
for line in s.split('\n'):
if line != '' and re.search(',', line) and (line.casefold().find('symbol') == -1):
(code, name) = [x.strip() for x in line.split(',')[1:3]]
res_dict[code] = int(name)
if(symbol=="all"):
return res_dict
if(symbol!=""):
return res_dict[symbol.upper()]
if(mode=="pandas"):
payload = pd.read_csv(url)
if(symbol=="all"):
return payload
else:
payload = payload[(payload.iloc[:, 1] == symbol.upper())]
return payload
My Code-
print(nse_get_fno_lot_sizes("adaniports","pandas"))
print(type(nse_get_fno_lot_sizes))
Output-
UNDERLYING SYMBOL JUL-22 AUG-22 SEP-22 DEC-22 MAR-23 JUN-23 DEC-23 JUN-24 DEC-24 JUN-25 DEC-25 JUN-26 DEC-26 JUN-27
8 ADANI PORT & SEZ LTD ADANIPORTS 1250 1250 1250
<class 'function'>
I want to convert the output getting from the nse_get_fno_lot_sizes function as dataframe.
Documentation- https://aeron7.github.io/nsepython/documentation/nsetools.html#top-losers-gainers
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create and use a dynamic variable in Python for the first time.
for i in range(0,len(test_data)):
globals()["test_list_{}".format(test_data[i])]=[]
globals()["test_calculation_{}".format(test_data[i])]=0
First, I created test_list_number and test_calculation_number as global variables.
Then I want to use this in for and use it for calculations.
The code I wrote here was made by simplifying the code I'm going to use.
How do I change the numbers in the two for statements below?
1
--------------Below is a code example. -------------
import pandas as pd
import numpy as np
X=list(np.random.random(100)*100)
Y=list(np.random.random(100)*100)
test_data= [2,5,7,8]
test_dict={(i,j):np.hypot(X[i]-X[j],Y[i]-Y[j]) for i in range(0,100) for j in range(0,100)}
test_df_data2={
'index' : [1,2,3],
'data1' : [3,5,6],
'data2' : [2,5,6]
}
test_df_data5={
'index' : [1,2,3],
'data1' : [8,3,1],
'data2' : [3,2,7]
}
test_df_2 =pd.DataFrame(test_df_data2)
test_df_5 =pd.DataFrame(test_df_data5)
for i in range(0,len(test_data)):
globals()["test_list_{}".format(test_data[i])]=[]
globals()["test_calculation_{}".format(test_data[i])]=0
for i in range(0, len(test_df_2 ) ):
test_list_2 .append((test_df_2 .data1[i],test_df_2 .data2 [i]))
for i in range(len(test_list_2 )):
test_calculation_2 = test_calculation_2 + test_dict[test_list_2 [i] ]
print( test_calculation_2)
Short answer, do not do this!
It is widely accepted to be a bad practice (see for example). You have a high risk of doing something difficult to debug, to overwrite existing variables, or to use the wrong namespace. In addition there are eventual pitfalls and lack of robust methods to do this.
Use a container instead.
You will have a shorter, cleaner, and easier to debug code.
Dictionaries are ideal for this:
# initialize
my_dfs = {}
# computation
for i in …:
my_dfs[f'df_computation_{i}'] = …
# access
my_dfs['df_computation_42']
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to understanding what is happening with my code but I couldn't obtain a solution.
Here is my code:
multipolygon = ((
(-58.89198482 -13.38147202, -58.89189251 -13.38147271, -58.89189321 -13.38156309, -58.89198552 -13.3815624,
-58.89207783 -13.38156171, -58.89217014 -13.38156102, -58.89226245 -13.38156032, -58.89235476 -13.38155963,
-58.89244706 -13.38155894, -58.89253937 -13.38155825, -58.89263168 -13.38155756, -58.89263097 -13.38146719,
-58.89253867 -13.38146788, -58.89244636 -13.38146857, -58.89235405 -13.38146926,
-58.89226174 -13.38146995, -58.89216943 -13.38147064, -58.89207712 -13.38147133,
-58.89198482 -13.38147202)
),
(
(-58.89484849 -13.38172171, -58.89484919 -13.38181209, -58.8949415 -13.38181139,
-58.89503381 -13.3818107, -58.89512612 -13.38181001, -58.89521843 -13.38180932,
-58.89521772 -13.38171894, -58.89531003 -13.38171825, -58.89540234 -13.38171756,
-58.89540163 -13.38162718, -58.89540092 -13.38153681, -58.89540022 -13.38144644,
-58.89530791 -13.38144713, -58.8952156 -13.38144782, -58.89512329 -13.38144851,
-58.89503098 -13.3814492, -58.89493868 -13.3814499, -58.89484637 -13.38145059,
-58.89484707 -13.38154096, -58.89484778 -13.38163134, -58.89484849 -13.38172171)
)
)
for poly in multipolygon:
print(poly)
The problem is when I print my multipolygon, the values are changing.
Here is the output:
(-72.27345684, -72.27336522, -72.27345629999999, -72.27354792, -72.27363954, -72.27373116, -72.27382277, -72.27391439, -72.274006, -72.27409762, -72.27418924, -72.27409816, -72.27400655, -72.27391493, -72.27382331, -72.27373169, -72.27364007, -72.27354845, -72.27345684)
(-72.27657020000001, -72.27666128, -72.27675289, -72.27684451, -72.27693613, -72.27702775, -72.27693666, -72.27702828, -72.2771199, -72.27702881, -72.27693773, -72.27684666, -72.27675504, -72.27666342, -72.2765718, -72.27648018000001, -72.27638858, -72.27629696, -72.27638802999999, -72.27647912, -72.27657020000001)
Why this is happening?
It looks like you're missing commas in the tuples which causes some values to be subtracted
From your code:
-58.89484849 -13.38172171
I'm assuming you mean for it to be -58.89484849, -13.38172171?
otherwise -58.89484849 -13.38172171 = 72.2765702 which matches the printed values
It does math since this is what you have inside your data structure
-58.89198482 -13.38147202 = -72.27345684
What is happening is that you are subtracting (-58.89189251 -13.38147271) before printing
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm crawling and parsing the JSON data I'm getting from RiotGames LoL API using Python (2.X) and I'm running into an odd error.
I'm loading in the json data and reading the data attr by attr, which goes great until I hit a certain attr which is clearly in the object I'm trying to extract it from but makes Python throw a KeyError as can be seen in the screenshot below.
Here is the codesnippet where the error takes place. As you can see I print the object (for debugging purposes) and then parse all attr, which works fine but throws a KeyError for unknown reasons at the attr 'doubleKills'. Hope you guys can help ^^
def parseJSON(self, jsonDump):
matchDetailDict = dict()
jsonobj = json.loads(jsonDump)
matchId = jsonobj['matchId']
tmpMatch = Match()
tmpMatch.matchID = matchId
tmpMatch.creationDate = jsonobj['matchCreation']
tmpMatch.matchDuration = jsonobj['matchDuration']
for participant, participantId in zip(jsonobj['participants'], jsonobj['participantIdentities']):
stats = participant['stats']
print stats
tmpStats = MatchPlayerStats()
tmpStats.playerID = participantId['player']['summonerId']
tmpStats.matchID = matchId
tmpStats.winner = stats['winner']
tmpStats.kills = stats['kills']
tmpStats.deaths = stats['deaths']
tmpStats.assists = stats['assists']
tmpStats.kda = (tmpStats.kills + tmpStats.assists)*1.0/max(tmpStats.deaths, 0.5)
tmpStats.visionWardsBoughtInGame = stats['visionWardsBoughtInGame']
tmpStats.sightWardsBoughtInGame = stats['sightWardsBoughtInGame']
tmpStats.championID = participant['championId']
tmpStats.doubleKills = participant['doubleKills'] #KeyError here!
tmpStats.firstBloodAssist = participant['firstBloodAssist']
tmpStats.firstBloodKill = participant['firstBloodKill']
tmpStats.killingSprees = participant['killingSprees']
tmpStats.wardsKilled = participant['wardsKilled']
tmpStats.wardsPlaced = participant['wardsPlaced']
tmpStats.unrealKills = participant['unrealKills']
matchDetailDict[tmpStats.playerID] = tmpStats
tmpMatch.playerStats = matchDetailDict
return tmpMatch
It looks as though the JSON on the Terminal you posted is from stats, but you are trying to use the key on participant.
print 'doubleKills' in stats.keys()
Should evaluate to True
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used this code to generate xyz coordinates.
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i = i + 1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(x[0]-3.5,x[0]+3.5), uniform(y[1]-3.5,y[1]+3.5), uniform(z[2]-3.5,z[2]+3.5))
print i, '\t', x1,y1,z1
When i run this program its showing error.
when i run this program with hole numbers it work.
how to resolve????
Your x, y, z are float, not list. So your can't have x[0] and such. You will get a no attribute or typeerror for that.
You're incorrectly using x,y,z in the x1,y1,z1 line. See what I've done below;
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i=i+1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(uniqcoord[0]-3.5,uniqcoord[0]+3.5), uniform(uniqcoord[1]-3.5,uniqcoord[1]+3.5), uniform(uniqcoord[2]-3.5,uniqcoord[2]+3.5))
print i, '\t', x1,y1,z1
x[0] does not exist, but uniqcoord[0] = x because uniqcoord = [x,y,z], and so on.
Result:
1 5.86941266341 29.4004245806 67.1323961576
2 6.38143060206 29.7045813689 69.4867869245
3 5.55280335095 29.9472835241 63.7388152633
4 10.5607637875 26.6269381673 69.5256503601
5 7.29826364813 28.5740308696 65.2122276564
6 8.24134391937 30.880058802 69.8445734597
7 10.246919304 27.9240839326 64.9480054046
8 8.26957559527 28.5700768795 63.996117793
9 5.88677020227 30.0621250245 63.7431176092
10 8.98100830174 27.3378753286 63.1329446911
I think this is what you are looking for.