On 26/10/14 22:12, Clayton Kirkwood wrote:

!On 25/10/14 23:46, Clayton Kirkwood wrote:
!> __author__ = 'SYSTEM'
!
!You are still setting __author__ which is a bit suspect.
!Leave double underscores to python.

This is something being created when I started with this file.

How are you creating your code? Are you using some kind of IDE?

!> raw_table = ('''
!
!> a: Ask    y: Dividend Yield
!> b: Bid     d: Dividend per Share
!> b2: Ask (Realtime)           r1: Dividend Pay Date
!> b3: Bid (Realtime)            q: Ex-Dividend Date
!...
!> s7: Short Ratio
!> ''')
!> col_position, code, description = 0, [], [] key_name =
!> raw_table.replace('\t','\n')
!
!I assume this is because you don't control the file format? Since
!otherwise you would just use newlines in the file, right?

Correct. I cut and pasted the data. Not going to change (probably) no sense
in putting it in a file

Are you saying the data will stay with your code? If that's the case then reformat the text into a Python data structure and avoid all the parsing that makes up about half your code.

!> for each_line in  key_name.splitlines():
!>      if ':' in each_line:
!>          c, d = each_line.split(':')
!>          code.append(c)
!>          description.append(d.strip())
!>          print( col_position, code[col_position],
!description[col_position])
!>          col_position += 1
!
!You could use enumerate in the for loop and that would set the
!col_position value for you:
!
...

However, I don't think that this solution will work for me, because there
are lines with no :

Ah yes, I didn't notice you only increment the counter inside the if.

!> output_line_len = 120
!> current_output_pos = index = 0
!> description_output_string = code_output_string = ''
!> for description_position, code_position in zip(description, code):
!
!Why not just put the codes and descriptions in tuples when you read them
!in the loop above? Why use zip? In other words where you do
!
! >          c, d = each_line.split(':')
! >          code.append(c)
! >          description.append(d.strip())
!
!Why not just join the pair there:
!
! >          c, d = each_line.split(':')
!            values.append((c,d))
!
!or even just
!
! >          values.append(each_line.split(':'))
!

Uh, because I didn't know about it....:<)))

!It seems as if you are splitting the values into two lists only to zip
!those lists together again in the next loop?
!
...

This has led to a problem, however. How do I determine if a value is in one
of the tuples:
Key in [a, word], [key, word]

I'm not sure exactly what you mean, I'd probably need to see
how you implemented it but, using my example above, you can do

key in values[index]

So if values looks like:

values = [(1,2),(2,3),....]

then the test

if 2 in values[0]:  will be true

and

if 2 in values[1]:  will also be true.

If you need to test the specific tuple item you could do:

if 2 == values[0][0]:  will be false because you only test 1st item

and

if 2 == values[0][1]:  will be true coz you test the 2nd item.

If you wanted to extract all the tuples containing 2 you could use a comprehension like:

value2 = [tup for tup in values if 2 in tup]


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to