Splitting into an array

This is the place for queries that don't fit in any of the other categories.

Splitting into an array

Postby jogl8 » Fri Feb 07, 2014 7:54 pm

I have some data in a string called reply. It has 18 words separated by spaces, like:

reply = "0 23 34 1 45 56 2 45 78 ..."

I would like to split it into an array, something like this:

Code: Select all
data [3,6]

for x in range (0,5):
   for y in range (0,2):
      data(x,y) = reply.split

print data(1,2)


Is there some way I can make split step through the string like that?

Thanks,
JP
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby Yoriz » Fri Feb 07, 2014 8:27 pm

Do you mean like this ?
Code: Select all
reply = "0 23 34 1 45 56 2 45 78 10 11 12 13 14 15 16 17 18"

split_reply = reply.split()

print map(lambda x, y: [x, y], split_reply[::2], split_reply[1::2])

Output
Code: Select all
[['0', '23'], ['34', '1'], ['45', '56'], ['2', '45'], ['78', '10'], ['11', '12'], ['13', '14'], ['15', '16'], ['17', '18']]
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1177
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Splitting into an array

Postby jogl8 » Fri Feb 07, 2014 8:43 pm

Yes, that looks very promising, I'll have to monkey around with it for a bit.

Thanks again,
JP.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby stranac » Fri Feb 07, 2014 9:17 pm

Yoriz wrote:
Code: Select all
map(lambda x, y: [x, y], ...)


You mean zip(), right?
Unless of course those have to be lists, in which case it's
Code: Select all
[list(t) for t in zip(...)]
(or the equivalent map, if you're that kind of a guy...)

jogl8 wrote:I would like to split it into an array, something like this:

That's a list. Arrays are a different thing.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Splitting into an array

Postby Yoriz » Fri Feb 07, 2014 9:23 pm

I assumed a list of lists was wanted :?
Another
Code: Select all
reply = "0 23 34 1 45 56 2 45 78 10 11 12 13 14 15 16 17 18"
print [list(t) for t in zip(*[iter(reply.split())] * 2)]

Output
Code: Select all
[['0', '23'], ['34', '1'], ['45', '56'], ['2', '45'], ['78', '10'], ['11', '12'], ['13', '14'], ['15', '16'], ['17', '18']]
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1177
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Splitting into an array

Postby jogl8 » Fri Feb 07, 2014 9:53 pm

That's a list. Arrays are a different thing.


Well, I wanted an array because it will, eventually, be converted to integers and committed to a data base.

I'm looking for an easy way to index through the data for conversion and saving to DB and other reasons and wanted to get it into an array right up front.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby jogl8 » Sat Feb 08, 2014 11:28 am

I probably posted too much code before. All that I need is to have the data in an array, the one called data.

Code: Select all
for x in range (0,5):
   for y in range (0,2):
      data(x,y) = reply.split


I just had that print statement there to see if the right info was in data at column 1 row 2.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby stranac » Sat Feb 08, 2014 11:33 am

Ok, so what is an array?
And what do you want the result to be?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Splitting into an array

Postby jogl8 » Sat Feb 08, 2014 12:06 pm

I guess what I want is a two dimensional list, what would have been called an array in the old days.

I need to be able to access the data by row and column.

So in:

Code: Select all
reply = "0 23 34 1 45 56 2 45 78 10 11 12 13 14 15 16 17 18"


data(1,1) = 0

The data in row 1 column 1 would be 0

data(1,2) = 23
data(1,3) = 34
data(2,1) = 1
...
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby jogl8 » Sat Feb 08, 2014 12:15 pm

So here's where I am now:

Code: Select all
reply = s.recv(65536)

print reply

reply_s = reply.split()

print reply_s

for x in range (1,6):
   for y in range (1,3):
      data[x,y] = reply_s[x*y]

for x in range (1,6):
   print'\n'
   for y in range (1,3):
      print data[x,y]


I get:

Socket Connected to Arduino on ip 192.168.1.11
0 745 495 1 743 494 2 728 483 3 715 474 4 380 250 5 262 186
['0', '745', '495', '1', '743', '494', '2', '728', '483', '3', '715', '474', '4', '380', '250', '5', '262', '186']
Traceback (most recent call last):
File "ss1.py", line 57, in <module>
data[x,y] = reply_s[x*y]
NameError: name 'data' is not defined


So the current problem is that I can't figure out how to declare the "two dimensional list" called data.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby stranac » Sat Feb 08, 2014 12:16 pm

Ok, if you just want a list of lists:
Code: Select all
>>> numbers = [int(n) for n in reply.split()]
>>> numbers
[0, 23, 34, 1, 45, 56, 2, 45, 78, 10, 11, 12, 13, 14, 15, 16, 17, 18]
>>> [numbers[i:i+3] for i in xrange(0, len(numbers), 3)]
[[0, 23, 34], [1, 45, 56], [2, 45, 78], [10, 11, 12], [13, 14, 15], [16, 17, 18]]
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Splitting into an array

Postby stranac » Sat Feb 08, 2014 12:25 pm

You should really go through a basic python tutorial to learn how to use lists...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Splitting into an array

Postby jogl8 » Sat Feb 08, 2014 1:08 pm

OK, did the tutorial, it is working as desired.

After 40 years of using multidimensional arrays lists just seem a little too simple.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby stranac » Sat Feb 08, 2014 1:24 pm

What language did you use multidimensional arrays in?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Splitting into an array

Postby jogl8 » Sat Feb 08, 2014 1:46 pm

Fortran, Basic, Pascal, and have done some C coding.

I was/am a controls engineer. Most of the computer programming I did was in Basic in the '80's. Things have changed ALOT since then. Did more PLC work in the '90's and some C programming on PC's running QNX OS.

Moved into management in mid '90's and then quit automotive to farm in mid 2000's so haven't done a lot of programming since the mid '90's.

Now I am working on data collection, monitoring and controlling systems for the farm. I'm hanging Arduino's all over the place, they are amazing little controllers.

In any other language I know of a list is called an array.

But the older languages are much stricter in using all variables, must be declared very precisely or problems. Python is very loose in that regard.
jogl8
 
Posts: 33
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Splitting into an array

Postby stranac » Sat Feb 08, 2014 2:12 pm

I see.
Python's lists are way more flexible and powerful than arrays in any of those languages.
You can use them like you would those, but there's really no point, since there is usually a better way.
I guess you just need some time to get used to the way python does stuff...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot], Superiorform and 4 guests