Marketplace
Related Categories
- Study
- Studies
- Diploma
- Case Study
- Scholarships
- Education
- Middle School
- High School
- College
- Degree
- Lesson Plans
- Worksheets
- Comprehension
- Learning
- Teaching
- University
Recently Added
- High School Diploma
- Entrepreneurship Case Studies
- Case Study Samples
- Distance Learning College
- Math Worksheets 5th Grade
- Free Lesson Plans And Worksheets
- The Water Cycle Lesson Plans
- Dementia Case Study
- Free Ged Diploma Online
- The Times University Guide 2011
- Colleges And Universities In California
- Periyar University Distance Education Results 2010
- Learning To Teach
- Distance Learning Accredited
- Help In Reading Comprehension
- Visual Arts Lesson Plans
- University Of East London
- Leading University With
- Leading Universities Of The World
- Victoria University Tafe Melbourne
Join StudyUp.com Today
You Recently Visited
Python List Comprehension
Ricky Said:
Misuse of list comprehensions in python?We Answered:
Well, I can't really give "exact" places to use these because the act of programming necessarily requires you to make decisions about what construct is most appropriate to use in any given context. What I can do for you, though, is explain the uses of each one.list - This is your general-purpose class to use when you need a bunch of stuff grouped together. It's iterable and you can very easily retrieve or change elements uses an index or slices. This is probably the one you'll use most often.
tuple - A tuple works almost exactly like a list...with one exception. You cannot alter individual elements in a tuple once you've created the tuple. For example:
>>>l = [1, 2, 3, 4] #if we make a list...
>>>l[2] = 'something else' #and assign an element to something else
>>>print l #printing the list shows that the element has changed.
[1, 2, 'something else', 4]
>>>t = (1, 2, 3, 4) #if we make a tuple...
>>>t[2] = 'something else' #and try to reassign an element...we get an error!
TypeError: 'tuple' object does not support item assignment
So why would we want this behavior? Well, if you're ever in a situation where it is crucial that changes not be made to your collection of values, then a tuple is exactly what you need. In fact, this will come into play with sets and dicts. A set CANNOT contain a list, but it CAN contain a tuple. Similarly, a dict CANNOT use a list as a key, but it CAN use a tuple! Python works this way so that the hash values of the keys don't change from one point in time to the next. It's essentially a safety measure.
set - A set is a special thing...you can't access individual data members with an index, so....
>>>s = set([1, 2, 3])
>>>print s[1]
...doesn't work.
It's also really picky about what kind of data it stores. It can only store immutable data like tuples and strings, but it cannot store something like a list or a dict since these can change. It also only stores one instance of each identical item, so set([1, 2, 2, 3, 4, 1]) is the same as set([1, 2, 3, 4]). What's the use? Well, this behavior is very useful if you want to weed out duplicates or do set operations. For example, if you want only the elements that are in both lists l1 and l2, but not elements that only exist in one or the other:
>>>l1 = [1, 2, 3, 4]
>>>l2 = [3, 4, 5, 6]
>>>s = set(l1) & set(l2)
>>>print s
set([3, 4])
dict - A dict is helpful when you have a lot of information that you want to access by something other than a positional index. For example, let's say you have a group of people whose ages you want to store, but you want a quick and easy way to retrieve the info without scanning through a list looking for the right name. Just use a dict to store the age with the name as the key!
>>>d = {}
>>>d['Jimmy John'] = 43
>>>d['Eddie Kowalski'] = 28
>>>d['Syed Ahmed'] = 21
>>>print d['Jimmy John']
43
And of course if you need the list of all the people whose ages you've stored, you just use d.keys() to give you the list ['Eddie Kowalski', 'Jimmy John', 'Syed Ahmed'].
I hope that answers your question.
Pearl Said:
Python programming help?We Answered:
The python documentation is on the web at http://docs.python.org/. It includes a nice tutorial, at http://docs.python.org/tutorial/. Lists are introduced at http://docs.python.org/tutorial/introduc… and covered in more detail at http://docs.python.org/tutorial/datastru… That more detailed section discusses list comprehensions, which provide a concise way of generating a list.The examples in the List Comprehensions section do something that is almost exactly what you need to do. This statement:
>>> vec = [2, 4, 6]
creates a list named "vec" that contains the values 2, 4 and 6 and this statement:
>>> [3*x for x in vec if x > 3]
constructs a new list by looking at each of the items in the "vec" list and, if the value of the item is greater than 3, placing an item whose value is three times as large into the new list.
The result is the list [12, 18].
In your case you'll have an input list that was passed as an argument to your function. You'll generate a new list by looking at each of the items in that list and, if the value of the item is exactly divisible by 2, placing the square of that value into the new list. Then your function will return that list to its caller.
The whole thing will consist of two statements, one to name the function and one to do the work.
You'll write more lines of code to test your function than you will for the function itself. BTW, before you actually write the function it's a good idea to spend a few minutes deciding how you're going to test it. Write down some sample input lists and figure out what the correct results should be if those lists are given as input to your function. Then when you've written your function, call it with those samples, print the results and check that the results match your expectations.