Answers for "python split array"

0

python separate string and number in array

numbers = []
animals = []
with open('textfile.txt') as f:
    for x in f:
        try:
            numbers.append(int(x.replace("\n", "")))
        except:
            animals.append(str(x.replace("\n", "")))
print(numbers)
print(animals)

# Output
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# ['dog', 'cat', 'fish', 'bird']
Posted by: Guest on May-22-2021
0

python split array

>>> x = np.arange(9.0)
>>> np.split(x, 3)
[array([0.,  1.,  2.]), array([3.,  4.,  5.]), array([6.,  7.,  8.])]
Posted by: Guest on June-01-2021

Browse Popular Code Answers by Language