Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access an attribute of a class from a list

Posted on 2009-05-03
2
Medium Priority
?
237 Views
Last Modified: 2012-05-06
I have the following class.
class chrom(object):
      """docstring for chrom"""
      def __init__(self, chr, position, ma2c):
            self.chr = chr
            self.position = position
            self.ma2c = ma2c
and then i have a list of this chrom objects called chromlist.

Let us imagine I want to create a sublist with the position of the items i to j from the chromlist!!!!

How would i do that in the esiest and most efficient way. I can' t do chromsublist[0:4].position???? why?
0
Comment
Question by:dfernan
  • 2
2 Comments
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
ID: 24291456
chromsublist[0:4] does return the sublist (slice in python lingo), but you can not do chromsublist[0:4].position, because the list object does not have a 'position' property. This sublist contains 5 objects, each object have the 'position' property, so you need to use a loop to access it:
positions = []
for c in chromsublist[0:4]:
    positions.append(c.position)
print positions

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24291474
Sorry, the [0:4] slice contains 4 objects, not 5!

You could also make the list with a single statement:
# using map and lambda:
print map(lambda c:c.position,chromsublist[0:4])
 
# using list comprehension
print [x.position for x in chromsublist[0:4]]

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question