Posts: 5
Threads: 2
Joined: Nov 2020
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])
Posts: 7,320
Threads: 123
Joined: Sep 2016
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
Posts: 6,806
Threads: 20
Joined: Feb 2020
Nov-26-2020, 06:29 PM
(This post was last modified: Nov-26-2020, 06:37 PM by deanhystad.)
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
Posts: 5
Threads: 2
Joined: Nov 2020
Thanks snippsat, I used your code, unfortunately the amount of steps the program executes stays the same.
Posts: 5
Threads: 2
Joined: Nov 2020
Tanks deanhystad, but I don't really know how to implement your code in my situation.
Posts: 6,806
Threads: 20
Joined: Feb 2020
(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
Posts: 7,320
Threads: 123
Joined: Sep 2016
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'
buran and ndc85430 like this post
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 5
Threads: 2
Joined: Nov 2020
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]
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
|