data1: 2
element: 3
sorted set after adding: ['3', '2']
data2: 4
sorted set after updating: [['4'], '3', '2']
When displaying the sorted set after updating I want the output to be like
sorted set after updating: [ 4, 3, 2]
data1 = input("data1: ").split(",")
element = input("element: ")
l1 = []
for i in data1:
l1.append(i)
l1.insert(-1, element)
print("sorted set after adding:", l1)
data2 = input("data2: ").split(",")
l1.insert(0, data2)
print("sorted set after updating:", l1)
CodePudding user response:
You need to treat data2
like you did with data1
, and also use sort()
to sort your list:
data1=input("data1: ").split(",")
element=input("element: ")
l1=[]
for i in data1:
# you should parse inputs to int
l1.append(int(i))
l1.insert(-1,int(element))
# we sort the list
l1.sort(reverse=True)
print("sorted set after adding:",l1)
# add the second input like you did with the first one
data2=input("data2: ").split(",")
for i in data2:
l1.append(int(i))
l1.sort(reverse=True)
print("sorted set after updating:",l1)
Output:
data1: 1,2,3
element: 5
sorted set after adding: [5, 3, 2, 1]
data2: 4
sorted set after updating: [5, 4, 3, 2, 1]
CodePudding user response:
Code:-
data1=input("data1: ").split(",")
element=input("element: ")
l1=[]
for i in data1:
l1.append(int(i))
l1.insert(-1,int(element))
l1=sorted(l1,reverse=True)
print("sorted set after adding:",l1)
data2=input("data2: ").split(",")
for i in data2:
l1.append(int(i))
l1=sorted(l1,reverse=True)
print("sorted set after updating:",l1)
Output:- Your Testcase:-
data1: 2
element: 3
sorted set after adding: [3, 2]
data2: 4
sorted set after updating: [4, 3, 2]
Another testcase:-
data1: 3,5,1
element: 4
sorted set after adding: [5, 4, 3, 1]
data2: 6,2,8
sorted set after updating: [8, 6, 5, 4, 3, 2, 1]
CodePudding user response:
You use the split() method to take the second input, data2, that returns a list of inputs, and you are adding that list to your original list. That's why you have got [['4'], '3', '2']. Try the following way:
data1=input("data1: ").split(",")
element=input("element: ")
l1=[]
for i in data1:
l1.append(int(i))
l1.insert(-1,int(element))
print("sorted set after adding:",l1)
data2=input("data2: ")
l1.insert(0,int(data2))
print("sorted set after updating:",l1)
Results:
data1: 2
element: 3
sorted set after adding: [3, 2]
data2: 4
sorted set after updating: [4, 3, 2]
You can also go this approach which is more general.
data1=input("data1: ").split(",")
element=input("element: ")
l1=[]
for i in data1:
# you should parse inputs to int
l1.append(int(i))
l1.insert(-1,int(element))
# we sort the list
l1.sort(reverse=True)
print("sorted set after adding:",l1)
# add the second input like you did with the first one
data2=input("data2: ").split(",")
for i in data2:
l1.append(int(i))
l1.sort(reverse=True)
print("sorted set after updating:",l1)
Here you can work with Python basics.
CodePudding user response:
based on your code when you are having this input ['3', '2'] your list would be l[0] = ['3' and l[1]= '2'], that's why you are getting a weird output.
one way to solve the problem is to parse the input to numbers while adding them into the list.