Printing multiple value with specific width in format - python

I want to print this with a specific width so that they are aligned when printed.
for i in range(len(valuelist)):
print("{0:<5} {0:<5} {0:<5}".format(valuelist[i],averagelist[i],averagesquared[i]))
Somehow, it only shows the valuelist[i] to all 3 columns. The averagelist[i], and averagesquared[i] is not getting shown. All 3 of those will only show valuelist[i].
It works properly when I print them like this.
for i in range(len(valuelist)):
print("{} {} {}".format(valuelist[i],averagelist[i],averagesquared[i]))
It properly works just like how I intended it to be.
I can fix it by putting 2 more prints but I want it to be in a single line.

That is because you are not using the syntax correctly, it should be:
for i in range(len(valuelist)):
print("{0:<5} {1:<5} {2:<5}".format(valuelist[i],averagelist[i],averagesquared[i]))
This is the correct way to format it, as the numbers inside the {} refer to the index of the arguments in the format function. If they are all zero instead like in your code, they will all call on the same argument, which is the first one every time.

Related

How to use each element in the list to check and locate the matching value from a data set?

I want to retrieve values from a data set that matches a certain value. The ".loc" method is working fine if I give one value at a time. But when trying to get the value from a list nothing is happening.
The below script work fine.
df.loc[df.domains=="IN"]
The below script is not. I want to use each item from the list to match and get the desired data frame from the data set
list=[""AE","AU","BE","BR","CN","DE","EG","ES","FR","IN","IT","JP","MX","NL","PL","SE","SG","UK"]
for i in list:
a=f'"{i}"'
print(a)
df.loc[df.domains==a]
Instead of a=f'"{i}"' try a = list[i]
You need to access the list in position i in order to get the location you desire.
Also I noticed that in list you have extra " in the beginning. It might give you a syntax error

Finding a set of values

I have a curve_hex generator, it uses two coordinates, I'm looking for only one coordinate. The script searches for one coordinate value easily, but if I specify several coordinates for the search, then it does not find anything.
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238'
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
if str(curve_hex).find(wanted)!=-1:
print (curve_hex[str(curve_hex).find(wanted):str(curve_hex).find(wanted)+len(wanted)])
else:
print ('not')
One value is found normally. But if I add several values, the script writes an error
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252'
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
if str(curve_hex).find(wanted)!=-1:
print (curve_hex[str(curve_hex).find(wanted):str(curve_hex).find(wanted)+len(wanted)])
else:
print ('not')
Tell me how to do it right. What am I doing wrong. I have just started learning python.
Very exciting that you are learning python, I would also suggest that you might want to spend some time in the stackoverflow section explaining how to ask a question because I am not 100% sure what you are trying to achieve with your code.
From my understanding, if you are happy with your if else conditions, and your only problem is that you can’t add multiple values to wanted. I would convert wanted to a list that includes all your wanted values, and loop over those.
Something like this:
wanted = ['586..{copy the whole value here}', '108...{copy the value here}']
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
for value in wanted:
if str(curve_hex).find(value)!=-1:
print (curve_hex[str(curve_hex).find(value):str(curve_hex).find(value)+len(value)])
else:
print ('not')
Edit: formatting and typos
You are misleadiong some basic concepts:
Difinitions like this result in diferent types
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238'
type(wanted) # string
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252'
type(wanted) # tuple
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
type(wanted) # string
So you should choose a type first. Tuple is the best case.
wanted=('58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252')
curve_hex=('58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252')
for i in wanted:
for x,j in enumerate(curve_hex):
if i in j:
print(f'{i} in {curve_hex} at position {x}')

How to take multiple inputs from user in a single line & add them together

Pretty much I'm trying to make a GPA calculator. I don't want anyone to just do the whole thing for me because I'm trying to figure out how to get 8 different values from the user in one line and add them together into one value. Most of the answers I've found online only talk about adding 2 values together so it's not of very much use to me...
I've tried using the ".split" function but really that's about it I'm new to python and dont have the background knowledge to really try much else.
No code, just need help with this problem
The expected result is to ask the user to put in 8 different grades between 0 and 100, then add them together into one value to later be divided.
If the GPAs come in in this format:
'3.3 3.6 2.7'
then you can read it in like this:
gpas = input('Please enter the GPAs in one line separated by spaces').split(' ')
and then you can loop through them (since split() returns a list), convert them to floats, and add them up, like so:
sum = 0
for gpa in gpas:
sum += float(gpa)
From what I read, I see you're getting the user's input as a string, from what you want to get the numbers the user entered and then operate with them, your problem being getting each separate number from the input. I think this other question on SO may help. Once you get each 'word' as an element of the array, you should convert each element to an int, getting the desired result.
Hope this helps!

Input/Output items of the 'work' function in GNU Radio

Is there a way to print (in the terminal or to a file) the input items passed to the work function and the output items produced there? I have written a GNU radio block (in Python) and I need to access the above information.
Any help is appreciated! :)
Assuming you're using a sync_block as block type, your work function will look like this:
def work(self, input_items, output_items):
where input_items is a 2D-array. First axis is the input ports (you might have only one) and second axis is the input items. So, if you just want to print the input items of the first input port in the terminal, you can do something like:
for i in range(len(input_items[0])):
print input_items[0][i]
Since you are producing the output items yourself within the work function, you can print them in the same manner after creating them.
Still, I think you try to solve something with this question that could be solved in another (better) way. Can you specify what you're trying to do with the information gathered by the printed input/output items?

Python FPDP multi_cell just returns a blank

I am using the multi_cell() function from FPDF in Python.
However every time I call it I just get a blank space where the cell should be. If however I call cell() it displays the cell.
Code
See two examples below.
Blank space with:
pdf.multi_cell(25,cell_height,ticket_no,1,0,"J",1)
Cell populated with data with
pdf.cell(25,cell_height,ticket_no,1,0,"L",1)
Anyone got an idea what is going on here?
Assuming PyFPDF, then cell() and multi_cell() take different parameters:
Cell:
https://code.google.com/p/pyfpdf/wiki/Cell
https://code.google.com/p/pyfpdf/source/browse/fpdf/fpdf.py#704
MultiCell:
https://code.google.com/p/pyfpdf/wiki/MultiCell
https://code.google.com/p/pyfpdf/source/browse/fpdf/fpdf.py#797
Multi_cell doesn't take ln, so by keeping that in, you end up shifting some of the parameters right by one, and saying:
align=0, fill="J"
instead of
align="J", fill=1

Categories