-4

a list should be altered so in each sub-list all elements except for the date are converted to numerical types (float or int...whichever is appropriate). So the element [’2014-01-29’, ’1119.12’, ’1121.75’, ’1099.42’, ’1106.92’, ’2379900’, ’1106.92’] would become [’2014-01-29’, 1119.12, 1121.75, 1099.42, 1106.92, 2379900, 1106.92] Notes: Do not create another list here. Alter the list that is passed in.

s = ['4/5/2013', '19.7', '20.35', '19.69', '20.3', '521000', '19.02']
['4/4/2013', '19.5', '19.85', '19.45', '19.85', '527500', '18.6']
['4/3/2013', '19.46', '19.56', '19.36', '19.55', '307800', '18.32']
['4/2/2013', '19.18', '19.52', '19.16', '19.52', '400200', '18.29']
['4/1/2013', '19.08', '19.25', '19.08', '19.21', '168100', '18']
['3/28/2013', '18.9', '19.25', '18.86', '19.19', '331100', '17.98']
['3/27/2013', '18.91', '19.03', '18.75', '19', '341600', '17.8']
['3/26/2013', '18.78', '19.1', '18.6', '19.02', '637900', '17.82']
['3/25/2013', '19', '19.15', '18.38', '18.9', '1244300', '17.71']
['3/22/2013', '19', '19.34', '18.6', '18.86', '10274900', '17.67']
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • What you have you tried? – user3467349 Feb 04 '15 at 16:50
  • above input shows syntax error. – Avinash Raj Feb 04 '15 at 16:51
  • 2
    The "Notes:..." feels awfully much like you're trying to make us do your homework before giving it a try. I feel that stackoverflow shouldn't encourage people to stop learning how to program, so I think you should reconsider asking it this way. – Marcus Müller Feb 04 '15 at 16:53
  • s = ['4/5/2013', '19.7', '20.35', '19.69', '20.3', '521000', '19.02'] The others dont have a name. – Tenzin Feb 04 '15 at 16:53
  • See here for how you might convert individual elements of the list to string of float: http://stackoverflow.com/questions/379906/parse-string-to-float-or-int – benjwadams Feb 04 '15 at 17:02

2 Answers2

0

Use List-comprehension.

>>> s = ['4/5/2013', '19.7', '20.35', '19.69', '20.3', '521000', '19.02']
>>> [float(i) if not '/' in i else i for i in s]
['4/5/2013', 19.7, 20.35, 19.69, 20.3, 521000.0, 19.02]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Actually the data in your question is a bunch of lists of strings. Here's how to convert them if they were all stored in a list named s:

s = [['4/5/2013', '19.7', '20.35', '19.69', '20.3', '521000', '19.02'],
     ['4/4/2013', '19.5', '19.85', '19.45', '19.85', '527500', '18.6'],
     ['4/3/2013', '19.46', '19.56', '19.36', '19.55', '307800', '18.32'],
     ['4/2/2013', '19.18', '19.52', '19.16', '19.52', '400200', '18.29'],
     ['4/1/2013', '19.08', '19.25', '19.08', '19.21', '168100', '18'],
     ['3/28/2013', '18.9', '19.25', '18.86', '19.19', '331100', '17.98'],
     ['3/27/2013', '18.91', '19.03', '18.75', '19', '341600', '17.8'],
     ['3/26/2013', '18.78', '19.1', '18.6', '19.02', '637900', '17.82'],
     ['3/25/2013', '19', '19.15', '18.38', '18.9', '1244300', '17.71'],
     ['3/22/2013', '19', '19.34', '18.6', '18.86', '10274900', '17.67']]

for lst in s:
    for i, substring in enumerate(lst):
        for kind in (int, float):
            try:
                lst[i] = kind(substring)
                break  # succeeded
            except ValueError:
                pass  # keep trying (or give-up)
    print(lst)

Output:

['4/5/2013', 19.7, 20.35, 19.69, 20.3, 521000, 19.02]
['4/4/2013', 19.5, 19.85, 19.45, 19.85, 527500, 18.6]
['4/3/2013', 19.46, 19.56, 19.36, 19.55, 307800, 18.32]
['4/2/2013', 19.18, 19.52, 19.16, 19.52, 400200, 18.29]
['4/1/2013', 19.08, 19.25, 19.08, 19.21, 168100, 18]
['3/28/2013', 18.9, 19.25, 18.86, 19.19, 331100, 17.98]
['3/27/2013', 18.91, 19.03, 18.75, 19, 341600, 17.8]
['3/26/2013', 18.78, 19.1, 18.6, 19.02, 637900, 17.82]
['3/25/2013', 19, 19.15, 18.38, 18.9, 1244300, 17.71]
['3/22/2013', 19, 19.34, 18.6, 18.86, 10274900, 17.67]
martineau
  • 119,623
  • 25
  • 170
  • 301