473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

accessing elements in multi-dimensional sequences

Is there an elegant way to directly refer the 2nd dimension of a
multi-dimensional sequence (like the nth character in a list of strings).
An example would be deleting the newline in all the strings from a list
obtained through readlines without recurring to 'for' loops.
I would expect this to work:
d ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
'0922210996\n'] del d[:][-1]
d ['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']

But it doesn't, d remains unchanged.

Another attempt produced what seemed to me like a counter-intuitive result
b=d[:][:-1]
b

['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
Regards from a python newbie,
Rodrigo Daunarovicius
Jul 18 '05 #1
7 1398
Rodrigo Daunaravicius wrote:
Is there an elegant way to directly refer the 2nd dimension of a
multi-dimensional sequence (like the nth character in a list of strings).
An example would be deleting the newline in all the strings from a list
obtained through readlines without recurring to 'for' loops.
I would expect this to work:

d
['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n',
'0922210996\n']
del d[:][-1]
d
['0891931243', '0325443777', '0933477028', '0699624617', '0922210996']

But it doesn't, d remains unchanged.

Another attempt produced what seemed to me like a counter-intuitive result

b=d[:][:-1]
b


['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
Regards from a python newbie,
Rodrigo Daunarovicius


You can express that as [s[:-1] for s in d].
Besides, strings are immutable: if s is a string, you can't do something
like s[3] = 'a': though, you can create a new object and tell s to refer
to it: s = s[:3] + 'a' + s[4:]

If all you want to do is remove trailing whitespace, have also a look at
the string strip, lstrip and rstrip methods.

--
Ciao,
Matteo
Jul 18 '05 #2
Rodrigo Daunaravicius wrote:
Is there an elegant way to directly refer the 2nd dimension of a
multi-dimensional sequence (like the nth character in a list of strings).

[list of strings example]

While your example doesn't work, as Matteo already explained, the Numeric
package provides an intuitive way to refer to the nth dimension of a
matrix:
a = Numeric.reshape(range(9), (3,3))
a array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]) a[:, :-1] array([[0, 1],
[3, 4],
[6, 7]])

In spite of its name, Numeric is not limited to numbers:
d = ['0891931243\n', '0325443777\n', '0933477028\n', '0699624617\n']
a = Numeric.array(d)
a.tostring() '0891931243\n0325443777\n0933477028\n0699624617\n' a[:, :-1].tostring() '0891931243032544377709334770280699624617'

Note that shorter strings are filled with spaces:
Numeric.array(["123\n", "4\n", "789\n"]).tostring()

'123\n4\n 789\n'

Of course this is overkill for your example use case.

Peter

Jul 18 '05 #3
Rodrigo Daunaravicius <ro******@hotmail.com> wrote:
Is there an elegant way to directly refer the 2nd dimension of a
multi-dimensional sequence (like the nth character in a list of strings).
An example would be deleting the newline in all the strings from a list
obtained through readlines without recurring to 'for' loops.


d = ['0891931243\n', '0325443777\n', '0933477028\n',
'0699624617\n', '0922210996\n']

#swap rows and columns, with the side effect of turning
#strings into lists, strings must be of equal length or
#else some information will be lost:

d1 = zip(*d)

#remove a row (corresponds to a *column* in the old view)
#this is an elementary operation now:

del d1[-1]

#swapping again restores the old row and column view:

d1 = zip(*d1)

#join the elements of the sublists in order to produce strings:

d1 = map(''.join,d1)

print d1

#output is:
#['0891931243', '0325443777', '0933477028', '0699624617',
#'0922210996']
While this way of coding enables one to *think* about the problem more
efficiently it is not necessarily the most efficient algorithm for
accomplishing this specific effect. If it's fast enough however, why
not reduce ones mental computing cycles and let the computer do all
the leg work?

It might even ameliorate entropy problems later on in the evolution of
the universe since the operations could probably be reversed more
efficiently even if the computer moves more electrons through the
silicon?

Anton

Jul 18 '05 #4
an***@vredegoor.doge.nl (Anton Vredegoor) wrote in
news:40*********************@reader3.nntp.hccnet.n l:
d = ['0891931243\n', '0325443777\n', '0933477028\n',
'0699624617\n', '0922210996\n']

#swap rows and columns, with the side effect of turning
#strings into lists, strings must be of equal length or
#else some information will be lost:

d1 = zip(*d)

#remove a row (corresponds to a *column* in the old view)
#this is an elementary operation now:

del d1[-1]

#swapping again restores the old row and column view:

d1 = zip(*d1)

#join the elements of the sublists in order to produce strings:

d1 = map(''.join,d1)

print d1

#output is:
#['0891931243', '0325443777', '0933477028', '0699624617',
#'0922210996']


This, of course, only works when the strings are all exactly the same
length. If they are different lengths it truncates all the strings to the
length of the shortest.
Jul 18 '05 #5
Thank you all for you great tips! Matteo's list comprehension solution
suits best my particular problem and I'll be using it.

On Fri, 28 May 2004 10:34:15 GMT, Matteo Dell'Amico wrote:
You can express that as [s[:-1] for s in d].

It was great to know about the Numeric package, Peter, it will be really
useful for some of the matrix handling I'm going to need.
Anton's solution was a good brain teaser. I couldn't find in the docs what
is the meaning of the asterisk in zip(*d), though. Nothing to do with C
pointers, I guess?
On Fri, 28 May 2004 13:58:23 +0200, Anton Vredegoor wrote:
Rodrigo Daunaravicius <ro******@hotmail.com> wrote:
Is there an elegant way to directly refer the 2nd dimension of a
multi-dimensional sequence (like the nth character in a list of strings).
An example would be deleting the newline in all the strings from a list
obtained through readlines without recurring to 'for' loops.


d = ['0891931243\n', '0325443777\n', '0933477028\n',
'0699624617\n', '0922210996\n']

#swap rows and columns, with the side effect of turning
#strings into lists, strings must be of equal length or
#else some information will be lost:

d1 = zip(*d)


Thanks again,
Rodrigo Daunaravicius
Jul 18 '05 #6
Rodrigo Daunaravicius <ro******@hotmail.com> wrote in
news:1e******************************@40tude.net:
I couldn't find in the docs what
is the meaning of the asterisk in zip(*d), though. Nothing to do with C
pointers, I guess?


Python Reference Manual, section 5.3.4 Calls:

If the syntax "*expression" appears in the function call, "expression" must
evaluate to a sequence. Elements from this sequence are treated as if they
were additional positional arguments; if there are postional arguments
x1,...,xN , and "expression" evaluates to a sequence y1,...,yM, this is
equivalent to a call with M+N positional arguments x1,...,xN,y1,...,yM.
Jul 18 '05 #7
On 28 May 2004 13:19:59 GMT, Duncan Booth wrote:
Rodrigo Daunaravicius <ro******@hotmail.com> wrote in
news:1e******************************@40tude.net:
I couldn't find in the docs what
is the meaning of the asterisk in zip(*d), though. Nothing to do with C
pointers, I guess?


Python Reference Manual, section 5.3.4 Calls:

If the syntax "*expression" appears in the function call, "expression" must
evaluate to a sequence. Elements from this sequence are treated as if they
were additional positional arguments; if there are postional arguments
x1,...,xN , and "expression" evaluates to a sequence y1,...,yM, this is
equivalent to a call with M+N positional arguments x1,...,xN,y1,...,yM.


Got it. I was looking in the wrong places. '*' is not the easiest string to
conduct a search on, if it's not referred to as an 'asterisk' somewhere on
the text.

Thanks, Duncan!
Rodrigo Daunaravicius
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
5
by: Craig Anderson | last post by:
Can anyone tell me the best way to access a hidden object in a form? I could use a hard-coded index to the elements of the form, but it's too easy to add something before the hidden object and mess...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
5
by: Rune Runnestø | last post by:
How do I focus the cursor in the input field 'numberField' when accessing this jsp-file (or html-file) ? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
19
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' );...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.