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
5 Answers 5
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
-
\$\begingroup\$ I am getting this value as string . Could you provide little bit more input to convert string to dict into this format \$\endgroup\$user765443– user7654432013年08月23日 13:13:06 +00:00Commented 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\$Simon Sagi– Simon Sagi2013年08月23日 13:27:24 +00:00Commented Aug 23, 2013 at 13:27
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'
-
\$\begingroup\$ It could even be condensed down to
print drivers.get(raw_input("Enter a load value\n"), 'Could not find driver value')
\$\endgroup\$200_success– 200_success2014年09月02日 15:05:59 +00:00Commented Sep 2, 2014 at 15:05
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)
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.
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"
li[index].split(':')
twice. Call it once, store the result and than use [0] and [1] on it. \$\endgroup\$