Showing summation of all elements in list [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 2 years ago.
Improve this question
I need to add all elements in a list and print that out. Is it possible to make this
x_irp = []
for i in range(1,5):
for r in range(1,7):
for p in range(1,4):
a = "x_{}_{}_{}".format(i,r,p)
x_irp.append(a)
print(x_irp)
['x_1_1_1', 'x_1_1_2', 'x_1_1_3', 'x_1_2_1', 'x_1_2_2', 'x_1_2_3', 'x_1_3_1', 'x_1_3_2', 'x_1_3_3', 'x_1_4_1', 'x_1_4_2', 'x_1_4_3', 'x_1_5_1', 'x_1_5_2', 'x_1_5_3', 'x_1_6_1', 'x_1_6_2', 'x_1_6_3', 'x_2_1_1', 'x_2_1_2', 'x_2_1_3', 'x_2_2_1', 'x_2_2_2', 'x_2_2_3', 'x_2_3_1', 'x_2_3_2', 'x_2_3_3', 'x_2_4_1', 'x_2_4_2', 'x_2_4_3', 'x_2_5_1', 'x_2_5_2', 'x_2_5_3', 'x_2_6_1', 'x_2_6_2', 'x_2_6_3', 'x_3_1_1', 'x_3_1_2', 'x_3_1_3', 'x_3_2_1', 'x_3_2_2', 'x_3_2_3', 'x_3_3_1', 'x_3_3_2', 'x_3_3_3', 'x_3_4_1', 'x_3_4_2', 'x_3_4_3', 'x_3_5_1', 'x_3_5_2', 'x_3_5_3', 'x_3_6_1', 'x_3_6_2', 'x_3_6_3', 'x_4_1_1', 'x_4_1_2', 'x_4_1_3', 'x_4_2_1', 'x_4_2_2', 'x_4_2_3', 'x_4_3_1', 'x_4_3_2', 'x_4_3_3', 'x_4_4_1', 'x_4_4_2', 'x_4_4_3', 'x_4_5_1', 'x_4_5_2', 'x_4_5_3', 'x_4_6_1', 'x_4_6_2', 'x_4_6_3']
I must print this list in a form as x_1_1_1 + x_1_1_2 + x_1_1_3....
It has to continue until the last element of list.

How about using
print(" + ".join(x_irp))

Related

Convert Function Output into Dataframe in Python [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 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

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

Convert string to dictionary with counts of variables [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 5 years ago.
Improve this question
This is a snippet of the output:
{...,"resultMap":
{..."SEARCH_RESULTS":
[{..."resultList":[
{"userClientId":"1"","preferenceValues":["48","51","94"],"MyDate":"7/26/2017 8:30:00 AM"},
{"userClientId":"2","preferenceValues":["42","11","84"],"MyDate":"7/26/2017 9:40:00 AM"},
{"userClientId":"3","preferenceValues":["4","16","24"],"MyDate":"7/26/2017 4:20:00 PM"},
{"userClientId":"4","preferenceValues":["7","2","94"],"MyDate":"7/27/2017 8:00:00 AM"},
{"userClientId":"1","preferenceValues":["48","22","94"],"MyDate":"7/27/2017 1:50:00 PM"},
{"userClientId":"2","preferenceValues":["42","11"],"MyDate":"7/27/2017 2:00:00 PM"},
{"userClientId":"3","preferenceValues":["4","24"],"MyDate":"7/27/2017 6:15:00 PM"},
{"userClientId":"4","preferenceValues":"7","MyDate":"7/27/2017 9:30:00 PM"}]
}]
}
}
I am looking to get a variable pageIdCount that is in dictionary format, where the key is page_id and the values are a counts of occurrences of page_id, by user_id. So for userId 1 it should look like:
{"userClientId":"1","preferenceValues":{48:2, 51:1, 94:2, 22:1}}
Note that when there is only 1 variable inside preferenceValues- there are no brackets. There is also a field "preferenceValue" where there are no brackets no matter what and it is identical to "preferenceValues" otherwise.
Is that possible?
In Python 2.7, I specify user, password and url and then I have the following:
req = requests.post(url = url, auth=(user, password))
ans = req.json()
print ["resultMap"]["SEARCH_RESULTS"][0]["resultList"]
Any help is greatly appreciated.
your_data # this is your data
final_data = {}
for line in yourdata:
uid = line["userId"]
pids = line["PageId"]
if uid not in final_data :
final_data[uid] = {}
for pid in pids :
pid = int(pid)
if pid not in final_data[uid]:
final_data[uid][pid]=0
final_data[uid][pid] += 1
res = [{"userId":uid,"PageIDCount":pids} for uid,pids in final_data.items()]
I suppose you are beginning, if so, the most tricky part of this code will probably be the last line, it uses list comprehension. here is a good lesson about it.

Tell me how to replace python data, How can I read the a.txt file and make it in the following format? [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 5 years ago.
Improve this question
Tell me how to replace python data
a.txt
abcd.com 0.0
* 6.6999306E7
asdf.com 1.50744025E8
asfd.df.com 1.93139033E8
fdsa.com 9.07938122E8
bank.com 2.638989462E9
fire.com 4.151822166E9
ms.com 7.026079907E9
How can I read the a.txt file and make it in the following format?
Output result :
['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907]
file = open('a.txt', 'r')
l = []
for line in file:
l.append( line.split())
Then if you want the second part to be integer, you can use list comprehension:
l = [ [i[0], int(float(i[1]))] for i in l]
output
[['abcd.com', 0],
['*', 66999306],
['asdf.com', 150744025],
['asfd.df.com', 193139033],
['fdsa.com', 907938122],
['bank.com', 2638989462],
['fire.com', 4151822166],
['ms.com', 7026079907] ]

Categories