2
\$\begingroup\$

This works fine but I feel it can be better optimized.

#!/usr/bin/env py
s = "1:5.9,1.5:7,2:10,4:18,8:40"
load_value = raw_input("Enter a load value\n")
li = s.split(',')
driver = None
for index,item in enumerate(li):
 if load_value == li[index].split(':')[1]:
 driver = li[index].split(':')[0]
 print driver
if not driver:
 print "Could not find driver value"

Output:

Enter a load value
5.9
1
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Aug 23, 2013 at 12:50
\$\endgroup\$
3
  • \$\begingroup\$ do not call li[index].split(':') twice. Call it once, store the result and than use [0] and [1] on it. \$\endgroup\$ Commented Aug 23, 2013 at 13:19
  • \$\begingroup\$ Don't use "#!/usr/bin/env py"; the name of the executable should be "python". \$\endgroup\$ Commented Aug 23, 2013 at 13:36
  • 2
    \$\begingroup\$ What is this code supposed to do? \$\endgroup\$ Commented Aug 23, 2013 at 16:11

5 Answers 5

3
\$\begingroup\$

Maybe you can use dict instead of str to store the driver-load values. If the driver is the key, you can search easily.

#!/usr/bin/env py
s = {'5.9':'1', '7':'1.5', '10':'2', '18':'4', '40':'8'}
load_value = raw_input("Enter a load value\n")
driver = s.get(load_value)
if driver is None:
 print "Could not find driver value"
else:
 print driver
answered Aug 23, 2013 at 13:06
\$\endgroup\$
2
  • \$\begingroup\$ I am getting this value as string . Could you provide little bit more input to convert string to dict into this format \$\endgroup\$ Commented Aug 23, 2013 at 13:13
  • \$\begingroup\$ These two lines takes the s string and creates the dict from it. pairs = dict([pair.split(':') for pair in s.split(',')]) new_dict = dict((v,k) for k,v in pairs.iteritems()) \$\endgroup\$ Commented Aug 23, 2013 at 13:27
2
\$\begingroup\$

Python is exception friendly. You can get cleaner code like this:

#!/usr/bin/python
drivers = {'5.9':'1', '7':'1.5', '10':'2', '18':'4', '40':8}
load_value = raw_input("Enter a load value\n")
try:
 print drivers[load_value]
except KeyError:
 print 'Could not find driver value'
answered Aug 23, 2013 at 17:04
\$\endgroup\$
1
  • \$\begingroup\$ It could even be condensed down to print drivers.get(raw_input("Enter a load value\n"), 'Could not find driver value') \$\endgroup\$ Commented Sep 2, 2014 at 15:05
2
\$\begingroup\$

My suggestion is to convert to a proper inverted map (easier to find the elements you want and faster)

#!/usr/bin/env py 
data = "1:5.9,1.5:7,2:10,4:18,8:40"
load_value = raw_input("Enter a load value\n")
driver_map = dict((t.split(':')[1],t.split(':')[0]) for t in data.split(','))
driver = driver_map[load_value]
if driver :
 print driver
else :
 print "Could not find driver value"

I considered that you can't change the format of your input (since no much information is provided regarding your code input/output)

answered Sep 2, 2014 at 14:47
\$\endgroup\$
1
\$\begingroup\$

I agree with Simon that store the data in a dictionary is a better way to structure the data. That being said, I do have some comments on the code you posted.

  • You don't need to use enumerate(). You are using it to get the index of the current item and just using the index to access the item from the list. Since you aren't using index for anything else, you should just use a basic for loop.

Ex:

for item in li:
 print li
  • You are also performing the split() operation twice instead of storing the result in a variable that can be accessed.
answered Aug 23, 2013 at 13:13
\$\endgroup\$
1
\$\begingroup\$

How about doing all splitting beforehand, and replacing driver with a simple bool:

s = "1:5.9,1.5:7,2:10,4:18,8:40"
load_value = raw_input("Enter a load value\n")
li = [v.split(':') for v in s.split(',')]
found = False
for index,item in enumerate(li):
 if load_value == li[index][1]:
 print li[index][0]
 found = True
if not found:
 print "Could not find driver value"
answered Aug 23, 2013 at 16:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.