Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question
#1
Hi,
is there a faster way of changing s list full of numbers in a "5" format, into a int(5) format than:

for i in range(len(a)):
a[i]=int(a[i])
Reply
#2
The "" format you mean, is a str.

If you have this list with strings:
some_values = ["1", "-5", "10", "-15"]
You can cast the str to int with different methods.

Some examples:
# the values defined here
some_values = ["1", "-5", "10", "-15"]

# list comprehension
new_some_values_1 = [int(value) for value in some_values]

# classical for-loop
new_some_values_2 = [] # empty list
for value in some_values:
    value = int(value)
    new_some_values_2.append(value)

# functional style with map
# each element of some_values is called with int()
# the list consumes the resulting map object
# it's lazy evaluated, not consuming the map object -> no values
new_some_values_3 = list(map(int, some_values))


# generator expression + for-loop
values = (int(value) for value in some_values)
for value in values: # <- evaluation of the generator begins here
    print(value, type(value)) # <- printing the value and type of value
# now values is consumed
# you can't use it again because it's now empty
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
But what I'm talking about is that the user types the numbers in 1 line:
3 5 7 2 53 2
and not:
23
5
7
2
53
2

I have it like this now:
t.extend(input().split())
Reply
#4
One way is to use list comprehension:

>>> data = input('Enter number separated by space: ')
Enter number separated by space: 23 4 5 6 7
>>> [int(entry) for entry in data.split()]
[23, 4, 5, 6, 7]
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
#5
(Dec-04-2020, 10:44 AM)1234 Wrote: But what I'm talking about is that the user types the numbers in 1 line

The solution provided perfringo.

The only problem is the user itself. The user could make mistakes during entering the numbers.
If there is for example one accidental comma in the user-input, int(entry) will raise a ValueError.

Just saying, don't trust users input.
perfringo likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Question - ' defined as "a". ?' Ryan012 10 1,491 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,830 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,472 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple arithmetic question ebolisa 5 2,007 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Simple Timer Question cranberrica 3 2,140 Jun-22-2020, 06:29 PM
Last Post: cranberrica
  Simple question on tab stuartsmith22 4 2,384 Apr-15-2020, 04:42 PM
Last Post: stuartsmith22
  Simple question concerning python dot notation. miner_tom 1 1,875 Mar-24-2020, 05:20 PM
Last Post: buran

Forum Jump:

User Panel Messages

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