Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python adds an extra half space when reading froma string or list --back to the question

Reply
Thread Tools

python adds an extra half space when reading froma string or list --back to the question

 
 
Joel Goldstick
Guest
Posts: n/a
 
      07-01-2013
I copied the original question so that the rant on the other thread
can continue. Let's keep this thread ontopic


number_drawn=()
def load(lot_number,number_drawn):
first=input("enter first lot: ")
last=input("enter last lot: ")
for lot_number in range(first,last):
line_out=str(lot_number)
for count in range(1,5):
number_drawn=raw_input("number: ")
line_out=line_out+(number_drawn)
print line_out
finale_line.append(line_out)
finale_line2=finale_line

load(lot_number,number_drawn)


print finale_line
print(" "*4),
for n in range(1,41):
print n, #this is to produce a line of numbers to compare to
output#
for a in finale_line:
print"\n",
print a[0]," ",
space_count=1
for b in range(1,5):
if int(a[b])<10:
print(" "*(int(a[b])-space_count)),int(a[b]),
space_count=int(a[b])
else:
print(" "*(a[b]-space_count)),a[b],
space_count=a[b]+1







number_drawn=()
def load(lot_number,number_drawn):
first=input("enter first lot: ")
last=input("enter last lot: ")
for lot_number in range(first,last):
line_out=str(lot_number)
for count in range(1,5):
number_drawn=raw_input("number: ")
line_out=line_out+(number_drawn)
print line_out
finale_line.append(line_out)
finale_line2=finale_line

load(lot_number,number_drawn)


print finale_line
print(" "*4),
for n in range(1,41):
print n, #this is to produce a line of numbers to compare to
output#
for a in finale_line:
print"\n",
print a[0]," ",
space_count=1
for b in range(1,5):
if int(a[b])<10:
print(" "*(int(a[b])-space_count)),int(a[b]),
space_count=int(a[b])
else:
print(" "*(a[b]-space_count)),a[b],
space_count=a[b]+1








this generates
<type 'list'>
enter first lot: 1
enter last lot: 4
number: 2
number: 3
number: 4
number: 5
12345
number: 1
number: 2
number: 3
number: 4
21234
number: 3
number: 4
number: 5
number: 6
33456
['12345', '21234', '33456']
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40
1 2 3 4 5
2 1 2 3 4
3 3 4 5 6
>#as you can see many numbers are between the lines of a normal print#

#I thought this was due to "white space" int he format .So I tried a list
of strings and got the same results.#



--
Joel Goldstick
http://joelgoldstick.com

 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      07-02-2013
On 2/07/2013 5:32 AM, Joel Goldstick wrote:
> I copied the original question so that the rant on the other
> thread can continue. Let's keep this thread ontopic


You've included the same set of code twice. Also, it doesn't run as is,
so you haven't reduced it to a minimal working example for us to test.

Python's print adds a space where there's a comma, try this at the
interactive prompt:

>>> print 'a','b'

a b

I don't think you're taking this into account. There are quite a few
other problems with your code, though:

> number_drawn=()
> def load(lot_number,number_drawn):


You've assigned `number_drawn` to a tuple, which is an immutable type.
Within the `load` function you then do:

> number_drawn=raw_input("number: ")


Which just re-assigns `number_drawn` to the output of the `raw_input`
function _within the function itself_. The value isn't available
outside of the function, if that is what you're intending. You also
haven't defined `lot_number` in your code, and again you re-assign it
within the body of the `load` function:

> for lot_number in range(first,last):


Which has no impact on any global definition. Since you're not actually
passing values into the function you can probably do without both
arguments and just go with:

def load():

> first=input("enter first lot: ")
> last=input("enter last lot: ")


You should never use `input`, it evaluates the expression entered.
Always use `raw_input` unless you absolutely know what you're doing.

> for lot_number in range(first,last):


`range` will start at `first` and finish _at but not including `last`.
If you want 4 lines when you enter first=1 and last=4, then you need to
increment `last` by 1:

for lot_number in range(first,last+1):

> finale_line.append(line_out)


Where is `finale_line` defined? You haven't shown it but my guess is
you've made it an empty list. You're also not returning anything from
the function, which implies you're relying on global scope to hold the
result. This is bad practice. You should start your function with:

finale_line = []

And then end it with:

return finale_line

Which would allow you to replace:

> finale_line2=finale_line
> load(lot_number,number_drawn)


With:

finale_line = load()

> for a in finale_line:
> print"\n",
> print a[0]," ",
> space_count=1
> for b in range(1,5):


This condition will _always_ be true:

> if int(a[b])<10:


Because of the way you're stepping through `finale_line`, you're only
ever looking at a single character, so that value will always be from 0
to 9. You clearly want to allow for double digit numbers, so not storing
them all as a big string would be a good start. You're dealing
with numbers, so hold them as numbers in a list.

Here's a more flexible approach that I think does what you want:

import os

def load_numbers():
first=int(raw_input("enter first lot: "))
last=int(raw_input("enter last lot: "))
finale_line = []
for lot_number in range(first,last+1):
line_out = []
for count in range(1,5):
number_drawn=raw_input("lot %d, number %d: " %
(lot_number, count))
line_out.append(number_drawn)
finale_line.append((lot_number, line_out))
return finale_line

finale_line = load_numbers()

# get space needed from the maximum number entered
largest_number = max(
number for row,numbers in finale_line for number in numbers)
space_count = len(str(largest_number))

# get no. of columns from the first set of numbers
columns = len(finale_line[0][1])
# space to cover the lot numbers
print ' ',
# print the columns
for column in xrange(1, columns+1):
spacing = space_count - len(str(column))
print '%s%d' % (' '*spacing, column),

for lot, numbers in finale_line:
print os.linesep, # use the EOL used by the current
# operating system
print lot,
for number in numbers:
spacing = space_count - len(str(number))
print '%s%d' % (' '*spacing, number),

However, if you want to make your life a _lot_ easier when printing
tabular data, I highly recommend using a library dedicated to just
that, something like:

https://code.google.com/p/prettytable/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: python adds an extra half space when reading from a string or list Mark Lawrence Python 106 07-08-2013 08:19 AM
Re: python adds an extra half space when reading from a string or list Joshua Landau Python 0 06-29-2013 08:05 PM
Re: python adds an extra half space when reading from a string or list Joshua Landau Python 0 06-29-2013 04:05 PM
When did the bool type make it into the language? Christopher Pisz C++ 13 05-05-2013 03:34 PM
Printing and prompting adds a mysterious extra space Christoph Haas Python 4 10-01-2005 11:35 PM



Advertisments