Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help
#1
Hi,
I want to read some numbers into my list and i want to insert them in 1 line:
1 2 3 4...
instead of:
1
2
3
4
When I read it in 1 line, it treats the numbers as a text, to change the format from text to number, I have to do it one by one. Is there any way to speed up the process and change the whole list into a number format?
t.extend(input().split())
for i in range(n):
    t[i]=int(t[i]) 
Reply
#2
Something like this.
>>> lst = ['1', '2', '3' ,'4']
>>> lst = [int(i) for i in lst]
>>> lst
[1, 2, 3, 4]
>>> 
>>> for i in lst:
...     print(i, end=' ') 
...     
1 2 3 4 
Reply
#3
inp_str = input("Enter numbers ")
inp_list = eval(inp_str)  # 1, 2, 3, 4
# or
inp_list = list(map(int, inp_str.split()))  # 1 2 3 4
Reply
#4
Thanks snippsat, I used your code, unfortunately the amount of steps the program executes stays the same.
Reply
#5
Tanks deanhystad, but I don't really know how to implement your code in my situation.
Reply
#6
(Nov-26-2020, 08:43 PM)Radez Wrote: Tanks deanhystad, but I don't really know how to implement your code in my situation.
t = eval(input()) # Type numbers separated by commas: 1, 2, 3, 4
# or
t = list(map(int, input())  # Type numbers separated by spaces: 1 2 3 4
Reply
#7
Should not advice to use eval() with input() @deanhystad.
It's a security risk,old input input() in Python 2 had this problem(did use eval).
Was changed in Python 3,so it return only a string then can use it with int(input(' ')) or float(input(' ')) if that's needed.

Example this could be my input string.
So if named a real exciting file it would be removed.
inp_str = '__import__("os").remove("important_file")'
eval(inp_str)
Error:
FileNotFoundError Traceback (most recent call last) 1 inp_str = '__import__("os").remove("important_file")' 2 eval(inp_str) FileNotFoundError: [Errno 2] No such file or directory: 'important_file'
ndc85430 and buran like this post
Reply
#8
More on the subject of eval() and how futile is trying to mitigate risks associated with it: Eval really is dangerous
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
I tried a few times to use your code but it still uses the same amount of steps to change text to numbers as my old one did.
for i in range(n):
    t[i]=int(t[i]
Reply
#10
(Nov-27-2020, 10:18 AM)Radez Wrote: I tried a few times to use your code but it still uses the same amount of steps to change text to numbers as my old one did.
for i in range(n):
    t[i]=int(t[i]

The number of steps is the same because you have to process every number entered, but... lookup of items at specific indices takes time (you make two lookups for every iteration) and this code is not idiomatic Python. It is something you write in Java. In Python you can iterate directly over items:

for item in n:
    # do something with item
There is list comprehension (example provided earlier by snippsat) for creating new list from iterable. As snippsat pointed out, you can do: 'split user input on whitespaces and convert every item in resulting list to integer'. This is efficient and idiomatic Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020