Solved

access an attribute of a class from a list

Posted on 2009-05-03
2
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 125 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 125 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 178
Python multiple IF statements 4 112
Python -- Read from csv for sending mail 9 85
78 files, need to delete row 2 in every file 3 33
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

739 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