Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create array from string
#1
Hi,

I need to create an array from string.
Array should have two digit consistent fragments.
For example if
s="10886"
I should have [10, 8, 88, 86]

Can anyone help me with this
Reply
#2
What parts are you having trouble with? Have you tried anything so far? Show us the code for your attempt.
Reply
#3
Can't figure it out how to get two digit consistent from string. I made this but it is returning 2 by 2 digit which is not what I wanted

[int(s[i:i+2]) for i in range(0, len(s), 2) ] 
Reply
#4
In the range statement, you're skipping by 2s. But you really want to go by 1s, just making sure you stop 1 before the end (since you don't want to ask for the n and n+1 characters).
Reply
#5
If you get rid of the last 2 in your range...
s='10886'
[int(s[i:i+2]) for i in range(0, len(s)) ]
Output:
[10, 8, 88, 86, 6]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create array of values from 2 variables paulo79 1 1,086 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,682 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  cannot create animation on 2D array using Matplotlib and FuncAnimation Caffeine_Addict 1 2,490 Jan-12-2021, 11:35 AM
Last Post: Caffeine_Addict
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,825 Dec-15-2020, 05:21 PM
Last Post: snippsat
  create an array of each line of text macieju1974 7 3,403 Jun-07-2020, 06:30 PM
Last Post: Yoriz
  Make an array of string number in a List polantas 5 3,100 May-27-2020, 07:18 AM
Last Post: buran
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 2,006 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  converting array to and from string in python 3.7.2 srm 5 6,181 Jul-03-2019, 01:11 PM
Last Post: snippsat
  python3 List to array or string to extract data batchenr 4 3,241 May-28-2019, 01:44 PM
Last Post: buran
  Create an empty NumPy array karansingh 3 3,099 May-04-2019, 10:01 AM
Last Post: dukoolsharma

Forum Jump:

User Panel Messages

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