We help IT Professionals succeed at work.

python - user input - set

ittechlab
ittechlab asked
on
310 Views
Last Modified: 2017-03-13
i want to write a python code where user input a number and i want to insert in between the right place.

for example user input 35, i want to insert between 30 and 40.

a = int(input("Enter a number: "))
s1 = set([10,20,30,40,50])
Comment
Watch Question

CERTIFIED EXPERT

Commented:
This is not possible if you want to use the set type. Set does not preserve the order of arguments. Using a list in the set([10, 20, 30, 40, 50]) is just a convention. You can also use a tuple (immutable), but it would look syntactically slightly confusing -- like set((10, 20, 30, 40, 50)) -- because of the doubled parentheses. Or, at least, it it would be more error prone -- even though the error would be revealed very soon.

So, you may want to use the list without converting it to the set type.

Assuming that the list is sorted, you can traverse the list in a loop and search for the index of the leftmost value that is greater (or greater-or-equal) than the inserted value. Then you can use the list.insert() method to insert it.

Unless it is a homework (to write such loop), it is better to use the standard Python module bisect to search for the position. The module even implements the handy function bisect.insort_left(a, x, lo=0, hi=len(a)), that does exactly what you may want. (Here a is the list used as an array, x is the inserted value. The other two arguments are optional.)
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
ittechlabLinux Support

Author

Commented:
I tried using list.

l1 = [10,20,30,40,50]
index = 0
n=15

for e in l1:
    if e < n:
        index += 1
    else:
        break

print l1.index(e)

l1.insert(l1.index(e),n)

print l1
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions