'Filter and sort inputed list of integers for non negative numbers python
I am trying to take a list that a user inputs, filter and sort in ascending order but only for the non-negative numbers. I thought I understood how to do this but it will not work with my logic.
my_list = []
n = int(input())
for i in range(0, n):
element = int(input())
if element > 0:
my_list.append(element)
my_list.sort()
print(my_list)
Here is the error I receive: ValueError: invalid literal for int() with base 10: '10 -7 4 39 -6 12 2' It doesn't like line 2
Solution 1:[1]
You have to put the lenght of the array into n and then use the i do define the element:
my_list = []
n = len(input().split())
for i in range(0, n):
element = int(input().split()[i])
if element > 0:
my_list.append(element)
my_list.sort()
print(my_list)
print:
[2, 4, 10, 12, 39]
EDIT: The upper version was tested and works with Py 3.8.2
This is working also on https://ideone.com/rDw1w6#stdin using Py 3.7.3
my_list = []
my_string_array = input().split()
n = len(my_string_array)
for i in range(0, n):
element = int(my_string_array[i])
if element > 0:
my_list.append(element)
my_list.sort()
print(my_list)
Solution 2:[2]
input()
returns the entire string at once, not the individual elements, so you need to split your input into the individual numbers using str.split
, then convert them to integers (this can be done conveniently using map
) before selecting only the non-negative numbers to put into my_list
(this can be written as a list comprehension). You can then sort the list with list.sort
:
elements = map(int, input().split())
my_list = [e for e in elements if e >= 0]
my_list.sort()
print(my_list)
For input of 10 -7 4 39 -6 12 2
, this produces
[2, 4, 10, 12, 39]
Solution 3:[3]
user_input = input()
my_list = [int(i) for i in user_input.split() if (int(i)>=0)]
my_list.sort()
[print(i, end=' ') for i in my_list]
Solution 4:[4]
# this solution works
user_input = input()
my_list = [int(i) for i in user_input.split() if (int(i)>=0)]
my_list.sort()
[print(i, end=' ') for i in my_list]
Solution 5:[5]
I'm a newbie, but there is probably a better way to write this.
interger = input().split()
new_interger = []
sorted_list = []
for x in interger:
new_interger.append(int(x))
for x in new_interger:
if x >= 0:
new_interger.pop()
sorted_list.append(x)
sorted_list.sort()
for x in sorted_list:
print(x, end=' ')
Solution 6:[6]
I broke it up a little to display the steps required to output the elements of the list rather than the actual list:
data = input()
#turn input to a list based on spaces
dat_list = data.split()
#turn elements in list to integers
dat_list = [int(x) for x in dat_list]
#fix list to only include positive integers
dat_list = [i for i in dat_list if pn >= 0]
#sort list
dat_list.sort()
#print each integer in list with a space after
for i in dat_list:
print(i, end=' ')
Tried to be as explicative as possible for people having a hard time understanding, meaning this is not the shortest or most effective way of doing this, but newbie friendly
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Nick |
Solution 3 | Jeremy Caney |
Solution 4 | PanamaPHat |
Solution 5 | buddemat |
Solution 6 | Jakob Taylor |