Use int which converts a string to an int, inside a list comprehension, like this:
desired_array = [int(numeric_string) for numeric_string in current_array]
Answer from sepp2k on Stack OverflowUse int which converts a string to an int, inside a list comprehension, like this:
desired_array = [int(numeric_string) for numeric_string in current_array]
List comprehensions are the way to go (see @sepp2k's answer). Possible alternative with map:
list(map(int, ['1','-1','1']))
Convert string array into integer python - Stack Overflow
How to convert string list to int list?
python - Converting a string to an array of integers - Code Review Stack Exchange
Converting a single line string to integer array in Python - Stack Overflow
Use int with base=16 for conversion, and list comprehension for output list:
[int(num, base=16) for num in nums]
Example:
In [376]: nums = ['02', '01', '03', '01', '04', 'ab', 'cd']
In [377]: [int(num, base=16) for num in nums]
Out[377]: [2, 1, 3, 1, 4, 171, 205]
You can use a list comprehension for this
>>> data = ['02', '01', '03', '01', '04', '01', '05', '01', '06', '01', '07', '01', '08', '01', '09', '01', '0a', '01', '0b', '01', '0c', '01', '0d', '01', '0e', '01', '0f', '01', '10', '01', '11', '01', '12', '01', '13', '01', '14', '01', '03', '02', '04', '02', '05', '02', '06', '02', '07', '02', '08', '02', '09', '02', '0a', '02', '0b', '02', '0c', '02', '0d', '02', '0e', '02', '0f', '02', '10', '02', '11', '02', '12', '02', '13', '02', '14', '02', '04', '03', '05', '03', '06', '03', '07', '03', '08', '03', '09', '03', '0a', '03', '0b', '03', '0c', '03', '0d', '03', '0e', '03', '0f', '03', '10', '03', '11', '03', '12', '03', '13', '03', '14', '03', '05', '04', '06', '04', '07', '04', '08', '04', '09', '04', '0a', '04', '0b', '04', '0c', '04', '0d', '04', '0e', '04', '0f', '04', '10', '04', '11', '04', '12', '04', '13', '04', '14', '04', '06', '05', '07', '05', '08', '05', '09', '05', '0a', '05', '0b', '05', '0c', '05', '0d', '05', '0e', '05', '0f', '05', '10', '05', '11', '05', '12', '05', '13', '05', '14', '05', '07', '06', '08', '06', '09', '06', '0a', '06', '0b', '06', '0c', '06', '0d', '06', '0e', '06', '0f', '06', '10', '06', '11', '06', '12', '06', '13', '06', '14', '06', '08', '07', '09', '07', '0a', '07', '0b', '07', '0c', '07', '0d', '07', '0e', '07', '0f', '07', '10', '07', '11', '07', '12', '07', '13', '07', '14', '07', '09', '08', '0a', '08', '0b', '08', '0c', '08', '0d', '08', '0e', '08', '0f', '08', '10', '08', '11', '08', '12', '08', '13', '08', '14', '08', '0a', '09', '0b', '09', '0c', '09', '0d', '09', '0e', '09', '0f', '09', '10', '09', '11', '09', '12', '09', '13', '09', '14', '09', '0b', '0a', '0c', '0a', '0d', '0a', '0e', '0a', '0f', '0a', '10', '0a', '11', '0a', '12', '0a', '13', '0a', '14', '0a', '0c', '0b', '0d', '0b', '0e', '0b', '0f', '0b', '10', '0b', '11', '0b', '12', '0b', '13', '0b', '14', '0b', '0d', '0c', '0e', '0c', '0f', '0c', '10', '0c', '11', '0c', '12', '0c', '13', '0c', '14', '0c', '0e', '0d', '0f', '0d', '10', '0d', '11', '0d', '12', '0d', '13', '0d', '14', '0d', '0f', '0e', '10', '0e', '11', '0e', '12', '0e', '13', '0e', '14', '0e', '10', '0f', '11', '0f', '12', '0f', '13', '0f', '14', '0f', '11', '10', '12', '10', '13', '10', '14', '10', '12', '11', '13', '11', '14', '11', '13', '12', '14', '12', '14', '13']
>>> [int(i, base=16) for i in data]
[2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 2, 12, 2, 13, 2, 14, 2, 15, 2, 16, 2, 17, 2, 18, 2, 19, 2, 20, 2, 4, 3, 5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 3, 12, 3, 13, 3, 14, 3, 15, 3, 16, 3, 17, 3, 18, 3, 19, 3, 20, 3, 5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 4, 11, 4, 12, 4, 13, 4, 14, 4, 15, 4, 16, 4, 17, 4, 18, 4, 19, 4, 20, 4, 6, 5, 7, 5, 8, 5, 9, 5, 10, 5, 11, 5, 12, 5, 13, 5, 14, 5, 15, 5, 16, 5, 17, 5, 18, 5, 19, 5, 20, 5, 7, 6, 8, 6, 9, 6, 10, 6, 11, 6, 12, 6, 13, 6, 14, 6, 15, 6, 16, 6, 17, 6, 18, 6, 19, 6, 20, 6, 8, 7, 9, 7, 10, 7, 11, 7, 12, 7, 13, 7, 14, 7, 15, 7, 16, 7, 17, 7, 18, 7, 19, 7, 20, 7, 9, 8, 10, 8, 11, 8, 12, 8, 13, 8, 14, 8, 15, 8, 16, 8, 17, 8, 18, 8, 19, 8, 20, 8, 10, 9, 11, 9, 12, 9, 13, 9, 14, 9, 15, 9, 16, 9, 17, 9, 18, 9, 19, 9, 20, 9, 11, 10, 12, 10, 13, 10, 14, 10, 15, 10, 16, 10, 17, 10, 18, 10, 19, 10, 20, 10, 12, 11, 13, 11, 14, 11, 15, 11, 16, 11, 17, 11, 18, 11, 19, 11, 20, 11, 13, 12, 14, 12, 15, 12, 16, 12, 17, 12, 18, 12, 19, 12, 20, 12, 14, 13, 15, 13, 16, 13, 17, 13, 18, 13, 19, 13, 20, 13, 15, 14, 16, 14, 17, 14, 18, 14, 19, 14, 20, 14, 16, 15, 17, 15, 18, 15, 19, 15, 20, 15, 17, 16, 18, 16, 19, 16, 20, 16, 18, 17, 19, 17, 20, 17, 19, 18, 20, 18, 20, 19]
When you convert to int there is a second parameter that specifies the base, 16 in this case for hex.
numbers = input("Please type some numbers: ").split()for n in range(0, len(numbers)):numbers[n] = int(numbers[n])
Just tried converting a string list to an int list using a for loop and made this code but not sure what it means step by step, if someone could please explain the last 2 lines it would be much appreciated. Thanks!
Nice solution, a few suggestions:
- The function name
convert_to_int_arrayseems too general. Consider a more specific name, for example,extract_numbers, or something similar. - There is no need to initialize
results_as_arraytoNone. - Mapping a list of strings to integers:
looks like the job forfor index in range(0, len(results_as_array)): results_as_array[index] = int(results_as_array[index].strip()) return results_as_arraymap:
Since the result ofreturn list(map(int, results_as_array))mapis a generator, it needs to be converted to a list withlist. - Tests: I noticed this line:
testing by changingdraw_result = "04-15-17-25-41" # change to "01-12 + 08-20" or "04-15-17-25-41" or "03-23-27-34-37, Mega Ball: 13" to testdraw_resultmanually it is time-consuming. A better way would be to keep the tests in a dictionary and to useassert. For example:
If you want to explore more "unit testing" have a look at unittest.tests = { "04-15-17-25-41": [4, 15, 17, 25, 41], "01-12 + 08-20": [1, 12, 8, 20], "03-23-27-34-37, Mega Ball: 13": [3, 23, 27, 34, 37] } for lottery_string, expected_output in tests.items(): assert expected_output == convert_to_int_array(lottery_string)
Alternative approach:
- Remove
, Mega Ball: 13if exists - Extract all numbers with a regular expression
import re
def extract_numbers(draw_result):
draw_result = draw_result.split(',')[0]
matches = re.findall(r'\d+', draw_result)
return list(map(int, matches))
Marc's answer is an excellent option if you don't care about validation.
If you need to be somewhat sure that the given string represents one of the known formats, then you can pre-bind a match method in a list of pre-compiled regular expressions:
import re
from typing import Iterable
lottery_searches = [
re.compile(pat).match
for pat in (
r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+), Mega Ball.*$',
r'^(\d+)-(\d+) \+ (\d+)-(\d+)$',
r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$',
)
]
def lottery_string_to_ints(lottery: str) -> Iterable[int]:
for search in lottery_searches:
match = search(lottery)
if match:
return (int(g) for g in match.groups())
raise ValueError(f'"{lottery}" is not a valid lottery string')
with output
In[2]: tuple(lottery_string_to_ints('03-23-27-34-37, Mega Ball: 13'))
Out[2]: (3, 23, 27, 34, 37)
In[3]: tuple(lottery_string_to_ints('01-12 + 08-20'))
Out[3]: (1, 12, 8, 20)
In[4]: tuple(lottery_string_to_ints('04-15-17-25-41'))
Out[4]: (4, 15, 17, 25, 41)
In[5]: tuple(lottery_string_to_ints('04-15'))
Traceback (most recent call last):
File "262933.py", line 20, in lottery_string_to_ints
raise ValueError(f'"{lottery}" is not a valid lottery string')
ValueError: "04-15" is not a valid lottery string
One way is to use ast.literal_eval.
If you need a numpy integer array, the conversion is trivial.
import numpy as np
from ast import literal_eval
arr_str = '[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]'
res = literal_eval(arr_str.replace(' ', ','))
# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
res_np = np.array(res)
# array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
arr_str = arr_str.strip("[]")
voila = [int(x) for x in arr_str.split()]
Edit 1: Being pedantic about variable assignment.
Given:
xs = ['1', '2', '3']
Use map then list to obtain a list of integers:
list(map(int, xs))
In Python 2, list was unnecessary since map returned a list:
map(int, xs)
Use a list comprehension on the list xs:
[int(x) for x in xs]
e.g.
>>> xs = ["1", "2", "3"]
>>> [int(x) for x in xs]
[1, 2, 3]