I want to create a dynamic variable and use it [closed] - python

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']

Related

Binary numbers to list [closed]

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']

Convert string structure in another [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file with this pattern :
[account.invoice.set_num]
job_size = 0
trans_size = 100
[commission.invoice_second.create_full]
j_size = 0
[commission.invoice_principal.finalize]
j_size = 12
in_directory = /to/the/directory
I want to transform this pattern to a text like :
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE = 0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE = 100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_IN_DIRECTORY=/to/the/directory
I try to do that in Bash unix or in Python.
I don't konw what is the best/easiest way to do that.
It's quite feasible with config.ConfigParser features:
from configparser import ConfigParser
config = ConfigParser()
config.read('yourfile')
config_lines = ''
for section in config.sections():
s_key = section.replace('.', '_') # transformed section key
for k, v in config.items(section):
config_lines += f'{s_key}_{k}'.upper() + f'={v}\n'
print(config_lines)
The output:
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE=0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE=100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_IN_DIRECTORY=/to/the/directory

Get complete source code from a website generated on clicking a button [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The webpage for which I want the source code contains some products.
When I simply load it, it gives me only the source code of the first few products.
What I need is the complete source code after clicking the Show 140-250 items which is generated several times and get the code when there is no something like Show x - y items anymore. The id of the button being generated is see-more-products.
I have tried urllib, requests modules, which don't do the job as expected.
Link to that particular page is in comments.
Any help is appreciated. Thanks :)
There are a few different ways, to get you started
import requests
from bs4 import BeautifulSoup
for i in range(0, 3339, 48):
r = requests.get("http://www.snapdeal.com/acors/json/product/get/search/175/{}/48".format(i))
print([a["href"] for a in BeautifulSoup(r.content).select("div.product-desc-rating.title-section-expand a")])
Which will gives you all the phone links, a sample of which are:
['http://www.snapdeal.com/product/micromax-canvas-spark-2-4/658765871888', 'http://www.snapdeal.com/product/alpha-cod/639184667095', 'http://www.snapdeal.com/product/apple-iphone-5s-16-gb/1204769399', 'http://www.snapdeal.com/product/micromax-canvas-a1-aq4502-8/630310793485', 'http://www.snapdeal.com/product/infocus-m370-8gb/685769734109', 'http://www.snapdeal.com/product/lenovo-a1000-8gb-black/634810874852', 'http://www.snapdeal.com/product/infocus-m350-16gb/667944684160', 'http://www.snapdeal.com/product/motorola-moto-x-force-32gb/677683041597', 'http://www.snapdeal.com/product/infocus-m812i-16gb-silver/672166899922', 'http://www.snapdeal.com/product/xolo-black-1x-32gb/633488491545', 'http://www.snapdeal.com/product/meizu-m2-m578h-4g-16gb/632749055332', 'http://www.snapdeal.com/product/iphone-6s-16gb/663413326062', 'http://www.snapdeal.com/product/apple-iphone-6-16-gb/1270529654', 'http://www.snapdeal.com/product/apple-iphone-5s-16-gb/347830397', 'http://www.snapdeal.com/product/micromax-s301-4gb-black/668491526095', 'http://www.snapdeal.com/product/infocus-m260-zl2-8gb/619518605458', 'http://www.snapdeal.com/product/micromax-juice-2-aq5001-dual/625046032091', 'http://www.snapdeal.com/product/swipe-konnect-51-8gb-sand/626439596420', 'http://www.snapdeal.com/product/lenovo-a6000-black/670500124351', 'http://www.snapdeal.com/product/redmi-2-prime-16-gb/683138068892', 'http://www.snapdeal.com/product/asus-zenfone-go-8gb/646618265597', 'http://www.snapdeal.com/product/karbonn-aura-8gb-3g/627156357326', 'http://www.snapdeal.com/product/panasonic-eluga-turbo-32gb-marine/644981636428', 'http://www.snapdeal.com/product/asus-zenfone-2-laser-ze550kl/633082651487', 'http://www.snapdeal.com/product/redmi-2-8-gb-grey/628016512272', 'http://www.snapdeal.com/product/infocus-m680-16gb-4g/649492687203', 'http://www.snapdeal.com/product/lava-iris-atom-8gb-white/665532304471', 'http://www.snapdeal.com/product/micromax-x073-black/632400291797', 'http://www.snapdeal.com/product/intex-aqua-3g-pro-4gb/661217697971', 'http://www.snapdeal.com/product/micromax-x605-black/663717815370', 'http://www.snapdeal.com/product/micromax-x088-red/550645299', 'http://www.snapdeal.com/product/micromax-unite-3-q372/639750556852', 'http://www.snapdeal.com/product/micromax-canvas-selfie-2-q340/673685061115', 'http://www.snapdeal.com/product/samsung-j7-16gb-espresso-brown/661359071561', 'http://www.snapdeal.com/product/hpl-a50-white-dual-core/752819001', 'http://www.snapdeal.com/product/intex-intex-aqua-power-hd/677955090549', 'http://www.snapdeal.com/product/samsung-galaxy-core-2-4gb/95934411', 'http://www.snapdeal.com/product/samsung-galaxy-j2-8gb/637947695307', 'http://www.snapdeal.com/product/micromax-bolt-x088/1252626810', 'http://www.snapdeal.com/product/lenovo-a7000-turbo-16gb-matte/676482356343', 'http://www.snapdeal.com/product/nokia-105-dual-sim-black/625877378012', 'http://www.snapdeal.com/product/lenovo-vibe-x2ap/619606468183', 'http://www.snapdeal.com/product/xolo-hive-8x1000i-16-gb/620631303689', 'http://www.snapdeal.com/product/panasonic-p41-black-android-dual/1202820682', 'http://www.snapdeal.com/product/asus-zenfone-2-laser-ze500kl/686932521097', 'http://www.snapdeal.com/product/samsung-galaxy-grand-max/647437791381', 'http://www.snapdeal.com/product/panasonic-p55-novo-8-gb/630374183875', 'http://www.snapdeal.com/product/samsung-tizen-z1/214936653']
['http://www.snapdeal.com/product/micromax-canvas-juice-3-q392/675338203115', 'http://www.snapdeal.com/product/micromax-x853-256-mb-gray/678836590088', 'http://www.snapdeal.com/product/iball-prince-2-black/683878915532', 'http://www.snapdeal.com/product/intex-cloud-swift-4g-16gb/651487709065', 'http://www.snapdeal.com/product/htc-desire-820-white/2096877771', 'http://www.snapdeal.com/product/celkon-2gb-star-16gb/668257971604', 'http://www.snapdeal.com/product/lava-iris-atom-8gb-black/671938280548', 'http://www.snapdeal.com/product/asus-zenfone-max-zc550kl-16gb/659567821492', 'http://www.snapdeal.com/product/micromax-x084/1189906548', 'http://www.snapdeal.com/product/infocus-bingo-21-m430-8gb/685620250128', 'http://www.snapdeal.com/product/xolo-black-4g-16gb-with/670501820959', 'http://www.snapdeal.com/product/nokia-130-dual-sim-black/1238158129', 'http://www.snapdeal.com/product/phicomm-e670-energy-2-16gb/642536836073', 'http://www.snapdeal.com/product/lenovo-a7000-8-gbwhite/660909822842', 'http://www.snapdeal.com/product/infocus-m260-8gb-yellow-3g/658001307243', 'http://www.snapdeal.com/product/gionee-m5l-32gb-golden-4g/664586124833', 'http://www.snapdeal.com/product/spice-gaming-mobile-x2-red/673912591179', 'http://www.snapdeal.com/product/lenovo-lenovo-a1000-8gb-white/656199391476', 'http://www.snapdeal.com/product/xolo-cube-50-8gb-black/681508479062', 'http://www.snapdeal.com/product/microsoft-lumia-540-8-gb/641216010217', 'http://www.snapdeal.com/product/infocus-m810-16gb-gold-4g/683493537817', 'http://www.snapdeal.com/product/samsung-guru-music-2-sm/672045180352', 'http://www.snapdeal.com/product/samsung-guru-music-2-duos/197236301', 'http://www.snapdeal.com/product/intex-cloud-cube-8gb-gray/678043979304', 'http://www.snapdeal.com/product/samsung-tizen-z3-8gb/638555381978', 'http://www.snapdeal.com/product/micromax-canvas-spark/634407752835', 'http://www.snapdeal.com/product/moto-e-2nd-gen-3g/627496057276', 'http://www.snapdeal.com/product/intex-cloud-breeze-8gb-grey/671476050074', 'http://www.snapdeal.com/product/samsung-j5-8gb-espresso-brown/659785514599', 'http://www.snapdeal.com/product/lg-google-nexus-5-16/848745269', 'http://www.snapdeal.com/product/mi4i-16gb/654856488809', 'http://www.snapdeal.com/product/micromax-joy-1850-35-kb/673275470330', 'http://www.snapdeal.com/product/phicomm-energy-653-4g-8gb/621455892516', 'http://www.snapdeal.com/product/panasonic-eluga-icon-16gb/650547748150', 'http://www.snapdeal.com/product/lenovo-vibe-x2ap-32gb-white/660083187849', 'http://www.snapdeal.com/product/xolo-prime/683763827810', 'http://www.snapdeal.com/product/meizu-mx5-silver-black/618643364985', 'http://www.snapdeal.com/product/karbonn-titanium-s205-2gb-16gb/662465421207', 'http://www.snapdeal.com/product/micromax-canvas-nitro-4g-e455/667663492017', 'http://www.snapdeal.com/product/i-kall-k88-gray/659450396588', 'http://www.snapdeal.com/product/karbonn-mach-two-titanium-s360/653888615715', 'http://www.snapdeal.com/product/asus-zenfone-2-laser-55/681651791446', 'http://www.snapdeal.com/product/i-kall-k55-fruity-orange/621445557898', 'http://www.snapdeal.com/product/yu-yunique-8gb/621238835100', 'http://www.snapdeal.com/product/htc-desire-326g-dual-sim/673825142381', 'http://www.snapdeal.com/product/micromax-h375-below-256-mb/626813860848', 'http://www.snapdeal.com/product/intex-intex-aqua-star-2/677150759819', 'http://www.snapdeal.com/product/micromax-canvas-nitro-2/674813358269']
['http://www.snapdeal.com/product/karbonn-s-15-plus-8gb/678547994280', 'http://www.snapdeal.com/product/huawei-honor-bee-8gb-3g/623753457685', 'http://www.snapdeal.com/product/intex-cloud-m6-16-gb/652438065304', 'http://www.snapdeal.com/product/karbonn-s310/2046781351', 'http://www.snapdeal.com/product/htc-desire-826/673132800168', 'http://www.snapdeal.com/product/oneplus-one-64gb/683926070447', 'http://www.snapdeal.com/product/samsung-galaxy-grand-prime-4g/682950239423', 'http://www.snapdeal.com/product/panasonic-panasonic-eluga-switch-32gb/651502528631', 'http://www.snapdeal.com/product/infocus-f120-dual-sim-gsm/647132622326', 'http://www.snapdeal.com/product/samsung-j5-8gb-white/625632131578', 'http://www.snapdeal.com/product/micromax-canvas-juice-3-q394/647451126738', 'http://www.snapdeal.com/product/micromax-x610-black/677067716681', 'http://www.snapdeal.com/product/videocon-v1393-dual-sim-mobile/519151652', 'http://www.snapdeal.com/product/nokia-105/1397084', 'http://www.snapdeal.com/product/micromax-canvas-blaze-4g-q400/635578549162', 'http://www.snapdeal.com/product/lenovo-a-6000-shot-16gb/652479804547', 'http://www.snapdeal.com/product/micromax-x597-black/666857291079', 'http://www.snapdeal.com/product/lenovo-a2010-16gb-black/641561931090', 'http://www.snapdeal.com/product/micromax-x597-blue/635774497484', 'http://www.snapdeal.com/product/lenovo-a1000-8gb-white-3g/682465160925', 'http://www.snapdeal.com/product/oorie-gsm-4gb-gray/644950735714', 'http://www.snapdeal.com/product/videocon-v1429w-black/659876102140', 'http://www.snapdeal.com/product/wham-q4-8gb-black/683618702751', 'http://www.snapdeal.com/product/lenovo-s60-8gb-white/682858759745', 'http://www.snapdeal.com/product/lenovo-a6000-white-8gb-white/687038221568', 'http://www.snapdeal.com/product/k2-air-8gb-black/662606076269', 'http://www.snapdeal.com/product/infocus-m680-16gb-4g/623852692656', 'http://www.snapdeal.com/product/micromax-x707-gray/675166544662', 'http://www.snapdeal.com/product/micromax-bolt-a-24-champagne/318667695', 'http://www.snapdeal.com/product/htc-desire-620-g/2140463474', 'http://www.snapdeal.com/product/whitecherry-k9-gray/634412911071', 'http://www.snapdeal.com/product/intex-neo-v-plus-black/49904988', 'http://www.snapdeal.com/product/intex-aqua-power-hd-mobile/639191044460', 'http://www.snapdeal.com/product/i-kall-k88-black/624856889024', 'http://www.snapdeal.com/product/apple-iphone-6-64-gb/131798351', 'http://www.snapdeal.com/product/infocus-f135-dual-sim-gsm/662122536540', 'http://www.snapdeal.com/product/apple-iphone-5s-32-gb/936530455', 'http://www.snapdeal.com/product/micromax-bolt-q338-8gb-black/633509849227', 'http://www.snapdeal.com/product/micromax-x084-black/676316493911', 'http://www.snapdeal.com/product/infocus-f110-dual-sim-gsm/668147567472', 'http://www.snapdeal.com/product/kenxinda-watch-mobile-dual-sim/149800122', 'http://www.snapdeal.com/product/panasonic-eluga-s/1270286256', 'http://www.snapdeal.com/product/swipe-virtue-16gb-white-3g/633490528209', 'http://www.snapdeal.com/product/micromax-canvas-play-q355-dual/663710099854', 'http://www.snapdeal.com/product/samsung-tizen-z1-4gb-gold/683743955020', 'http://www.snapdeal.com/product/asus-zenfone-go-45-zc451tg/632893288226', 'http://www.snapdeal.com/product/karbonn-a6-turbo/677649317861', 'http://www.snapdeal.com/product/karbonn-titanium-delight-s22-blackgrey/679556902984']
You might have to go tweak this a little bit but this should get close to getting all the links you want:
import requests
from bs4 import BeautifulSoup
def get_all_links():
r = requests.get("http://www.snapdeal.com/acors/json/product/get/search/175/0/48")
total = int((BeautifulSoup(r.content).select("div.jsNumberFound.hidden")[0].text))
yield from (a["href"] for a in BeautifulSoup(r.content).select("div.product-desc-rating.title-section-expand a"))
print("There are {} total items".format(total))
for pge in range(50, total+1, 48):
r = requests.get("http://www.snapdeal.com/acors/json/product/get/search/175/{}/48".format(pge))
yield from (a["href"] for a in BeautifulSoup(r.content).select("div.product-desc-rating.title-section-expand a"))
There are 3340 total items
http://www.snapdeal.com/product/micromax-canvas-spark/634407752835
http://www.snapdeal.com/product/iphone-6s-16gb/663413326062
http://www.snapdeal.com/product/apple-iphone-5s-16-gb/1204769399
http://www.snapdeal.com/product/moto-e-2nd-gen-3g/627496057276
http://www.snapdeal.com/product/redmi-2-prime-16-gb/683138068892
http://www.snapdeal.com/product/micromax-juice-2-aq5001-dual/625046032091
http://www.snapdeal.com/product/alpha-cod/639184667095
http://www.snapdeal.com/product/intex-cloud-breeze-8gb-grey/671476050074
http://www.snapdeal.com/product/micromax-canvas-spark-2-4/658765871888
http://www.snapdeal.com/product/infocus-m370-8gb/685769734109
http://www.snapdeal.com/product/infocus-m350-16gb/667944684160
http://www.snapdeal.com/product/lg-google-nexus-5-16/848745269
http://www.snapdeal.com/product/lenovo-a6000-black/670500124351
http://www.snapdeal.com/product/apple-iphone-6-16-gb/1270529654
http://www.snapdeal.com/product/meizu-m2-m578h-4g-16gb/632749055332
http://www.snapdeal.com/product/micromax-canvas-a1-aq4502-8/630310793485
http://www.snapdeal.com/product/apple-iphone-5s-16-gb/347830397
http://www.snapdeal.com/product/infocus-bingo-21-m430-8gb/685620250128
http://www.snapdeal.com/product/samsung-j7-16gb-espresso-brown/661359071561
http://www.snapdeal.com/product/apple-iphone-6-64-gb/131798351
http://www.snapdeal.com/product/panasonic-p55-novo-8-gb/630374183875
http://www.snapdeal.com/product/lenovo-a1000-8gb-black/634810874852
http://www.snapdeal.com/product/infocus-m260-zl2-8gb/619518605458
http://www.snapdeal.com/product/asus-zenfone-2-laser-ze500kl/686932521097
http://www.snapdeal.com/product/meizu-mx5-silver-black/618643364985
http://www.snapdeal.com/product/moto-e-2nd-gen-4g/682444504031
http://www.snapdeal.com/product/xolo-black-1x-32gb/633488491545
http://www.snapdeal.com/product/panasonic-eluga-turbo-32gb-marine/644981636428
http://www.snapdeal.com/product/mi4i-16gb/654856488809
http://www.snapdeal.com/product/samsung-j5-8gb-espresso-brown/659785514599
http://www.snapdeal.com/product/intex-cloud-swift-4g-16gb/651487709065
http://www.snapdeal.com/product/lenovo-vibe-x2ap-32gb-white/660083187849
http://www.snapdeal.com/product/panasonic-eluga-icon-16gb/650547748150
http://www.snapdeal.com/product/micromax-s301-4gb-black/668491526095
http://www.snapdeal.com/product/apple-iphone-5s-32-gb/936530455
http://www.snapdeal.com/product/lenovo-a7000-turbo-16gb-matte/676482356343
http://www.snapdeal.com/product/samsung-tizen-z3-8gb/638555381978
http://www.snapdeal.com/product/oneplus-one-64gb/683926070447
http://www.snapdeal.com/product/asus-zenfone-2-laser-55/681651791446
http://www.snapdeal.com/product/htc-desire-826/673132800168
http://www.snapdeal.com/product/gionee-m5l-32gb-golden-4g/664586124833
http://www.snapdeal.com/product/intex-intex-aqua-power-hd/677955090549
http://www.snapdeal.com/product/samsung-a7-2016-16gb-4g/632787468738
http://www.snapdeal.com/product/micromax-unite-3-q372/639750556852

Python JSON KeyError for key that is not missing in object being parsed [closed]

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

how to variate a parameter in an equation python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am solving the following equation:
wf=1110
wt=647
wp=119000
c=300000000
e0=10983849
e1 =e0-(wp**2/(w*2+wt))
And I want "w" as a variable like from 1 to 1000 or whatever I want in a rising scale.
after I want a plot of e1 vs w
does anyone know how to do that?
One way to do this:
def solve_equation(start, end, equation):
for x in xrange(start, end):
yield equation(x)
def my_equation(x):
wt=647
wp=119000
e0=10983849
e1 =e0-(wp**2/(w*2+wt))
return e1
print solve_equation(0, 100, my_equation)
print solve_equation(500, 1000, my_equation)
This splits the solving of the equation for a range from the equation itself.
You could also look into numpy, if you are going to be doing this on a large scale. Then you would create a numpy array of the values for w and calculate them as a vector. This would boil your code down to something like this:
from numpy import arange
wt=647
wp=119000
e0=10983849
w = arange(0, 100)
e = e0-(wp**2/(w*2+wt))
This would set w to array([0, 1, 2, ..., 98, 99]) and e to:
array([-10903322, -10835873, -10768839, -10702215, -10635998, -10570184,
-10504770, -10439751, -10375125, -10310887, -10247035, -10183565,
-10120472, -10057755, -9995410, -9933433, -9871821, -9810570,
-9749679, -9689143, -9628960, -9569126, -9509638, -9450494,
-9391690, -9333224, -9275092, -9217292, -9159820, -9102675,
-9045853, -8989352, -8933169, -8877301, -8821745, -8766499,
-8711561, -8656927, -8602596, -8548564, -8494830, -8441391,
-8388244, -8335387, -8282817, -8230533, -8178532, -8126812,
-8075370, -8024204, -7973312, -7922693, -7872342, -7822259,
-7772442, -7722888, -7673595, -7624560, -7575784, -7527262,
-7478993, -7430975, -7383206, -7335685, -7288409, -7241376,
-7194584, -7148033, -7101719, -7055641, -7009797, -6964186,
-6918805, -6873654, -6828729, -6784030, -6739555, -6695302,
-6651269, -6607455, -6563858, -6520477, -6477310, -6434355,
-6391611, -6349076, -6306749, -6264628, -6222712, -6180999,
-6139488, -6098177, -6057065, -6016151, -5975432, -5934908,
-5894577, -5854438, -5814490, -5774730])
Using the excellent IPython Notebook with pylab, you can then just do:
plot(w, e)
and have a nice graph showing the result:
Plotting up to 10000 will result in a graph like this:

Categories