473,406 Members | 2,705 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,406 software developers and data experts.

list of lists

Tom
Hi,

I have a list that consists of lists.
E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it.
a = T[0]
print a[1]

That kind of sucks, becaus I have to read a lot of values from a lot of
lists! :-(
Is there a faster way than my saving it to a "help variable" first?

Thanks folks!!

Regards, Tom

Jul 18 '05 #1
10 17075
In article <bk**********@news.uni-kl.de>, Tom wrote:
Hi,

I have a list that consists of lists.
E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it.
a = T[0]
print a[1]

That kind of sucks, becaus I have to read a lot of values from a lot of
lists! :-(
Is there a faster way than my saving it to a "help variable" first?


To use your example:

T=[[1, 2, 3], [4, 5], [6]]
print T[0][1]

Zach
Jul 18 '05 #2
On Mon, 15 Sep 2003 15:59:56 +0200, Tom wrote:
Hi,

I have a list that consists of lists. E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it. a =
T[0]
print a[1]


Try using T[0][1]. T[0] is exactly equivalent to 'a', so you can use
the same operations, you don't have to assign to 'a' first.

Johannes
Jul 18 '05 #3
Tom wrote:
Hi,

I have a list that consists of lists.
E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it.
a = T[0]
print a[1]

That kind of sucks, becaus I have to read a lot of values from a lot of
lists! :-(
Is there a faster way than my saving it to a "help variable" first?

Thanks folks!!

Regards, Tom


Your example rather than your problem specification suggests that you want
them all. If I'm guessing right:

def flatten(lol):
for lst in lol:
for item in lst:
yield item

T=[[1, 2, 3], [4, 5], [6]]

for item in flatten(T):
print item

Indices seem to be a dying breed :-)

Peter
Jul 18 '05 #4
On Mon, 15 Sep 2003, Tom wrote:
Hi,

I have a list that consists of lists.
E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it.
a = T[0]
print a[1]

That kind of sucks, becaus I have to read a lot of values from a lot of
lists! :-(
Is there a faster way than my saving it to a "help variable" first?

Thanks folks!!

Regards, Tom


Hi Tom,

Do you mean this?
T=[[1, 2, 3], [4, 5], [6]]
T[0][1] 2 print T[0][1] 2 print T[1][1]

5

HTH,

--
Paul Osman
pa**@eval.ca
http://perl.eval.ca

"Idealists...foolish enough to throw caution
to the winds...have advanced mankind and have
enriched the world."
- Emma Goldman

Jul 18 '05 #5

Do you mean this:
T=[[1, 2, 3], [4, 5], [6]]
print T[0][1]

2

--

=*= Lukasz Pankowski =*=
Jul 18 '05 #6
On Mon, 15 Sep 2003 15:59:56 +0200, Tom <ll****@gmx.net> wrote:
I have a list that consists of lists.
E.g. T=[[1, 2, 3], [4, 5], [6]]
Is there a way to address the a specific component in the "inner" list
directly?
E.g. right now I want to get the second value of the first list.
Unfortunately I have to save it to a variable first and then read it.
a = T[0]
print a[1]

That kind of sucks, becaus I have to read a lot of values from a lot of
lists! :-(
Is there a faster way than my saving it to a "help variable" first?


T=[[1, 2, 3], [4, 5], [6]]
print t[0][1]

2

Daniel Klein
Jul 18 '05 #7
Tom
Hi guys,

thanks to everyone.
My mistake was that I had no idea that writing [][] NEXT to each other
goes into depth. I always tried different varieties of [[]] to get into
the deeper lists!
Thanks again.

CU Tom

Jul 18 '05 #8
Tom wrote:
Hi guys,

thanks to everyone.
My mistake was that I had no idea that writing [][] NEXT to each
other goes into depth. I always tried different varieties of [[]]
to get into the deeper lists!
Thanks again.


It's a simple concept, once you grasp it, but for those new to
Python, it may be worth emphasizing -- You can concatenate
operators (to the right, at least) and these operators will
operate on the run-time value produced by the expression
to which they are applied. For example (read from bottom up):
getArray()[3].formatter()
^ ^ ^ ^ ^
| | | | |
| | | | +--- (5) call function retrieved from
| | | | attribute
| | | +-------------- (4) access attribute of object indexed
| | | from array
| | +---------------- (3) index element of array returned by
| | function call
| +------------------- (2) call function retrieved from name
| binding
+------------------------ (1) retrieve value bound to variable
It is also worth thinking about what is meant by saying that this
evaluation is *dynamic*. For example, if the object returned by
the function call to getArray (above) is not indexable, then the []
operator will fail.

And, the only limiting factor is confusion.

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.com
Jul 18 '05 #9
Dave Kuhlman wrote:
It's a simple concept, once you grasp it, but for those new to
Python, it may be worth emphasizing -- You can concatenate
operators (to the right, at least) and these operators will
operate on the run-time value produced by the expression
to which they are applied. For example (read from bottom up):
getArray()[3].formatter()
^ ^ ^ ^ ^
| | | | |
| | | | +--- (5) call function retrieved from
| | | | attribute
| | | +-------------- (4) access attribute of object indexed
| | | from array
| | +---------------- (3) index element of array returned by
| | function call
| +------------------- (2) call function retrieved from name
| binding
+------------------------ (1) retrieve value bound to variable
It is also worth thinking about what is meant by saying that this
evaluation is *dynamic*. For example, if the object returned by
the function call to getArray (above) is not indexable, then the []
operator will fail.

And, the only limiting factor is confusion.


Nice explanation. I take the occasion to warn newbies that the sort() method
is a showstopper in this scheme:

getArray().sort()[3].formatter() #BAD CODE, DON'T USE
^
|
+ -- sort() returns None

By the way, is there any Python programmer who has not made this error at
least once?

Peter
Jul 18 '05 #10
Peter Otten wrote:
Nice explanation. I take the occasion to warn newbies that the sort() method
is a showstopper in this scheme:

getArray().sort()[3].formatter() #BAD CODE, DON'T USE
^
|
+ -- sort() returns None

By the way, is there any Python programmer who has not made this error at
least once?

Also it is a slower approach if you want to look up more than one item
in the list. In that case it is better to bind the sorted result of
getArray to a variable.

arr = getArray()
arr.sort()
arr[3].formatter()
arr[4].formatter()
arr[5].formatter()
So I find that I rarely use the short form of the expression.

regards Max M

Jul 18 '05 #11

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

Similar topics

10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
11
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
2
by: Cox | last post by:
Hello: My address jsmith435@cox.net is subscribed to at least the PHP General mailing list. I have for days now been trying to unsubscribe from all PHP mail lists. I have followed the...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.