PDA

View Full Version : [SOLVED] Python - List Comprehension Problems

Yozuru
April 26th, 2014, 08:36 PM
Hello good people,

I am working on a practice problem that involves list comprehension and range.

So I have a list called 'sampleList', and I have to write a list comprehension that would return the sublist of 'sampleList' consisting of numbers that belong in interval [-11, 5]

This is currently what I have so far:

subsampleList = [x for x in sampleList if range(-11, 6)]

On another problem, I need to make a list of hexadecimals of entries in sampleList that returns non-negative numbers.

[hex(x)) for x if sampleList >0]

I am as of now lost with list comprehension and any help would be greatly appreciated.

Yozuru
April 26th, 2014, 08:59 PM
I have solved my first problem right now using:

subsampleList = [x for x in sampleList if -10 <= x <= 10]

I still need a bit of help figuring out the second one.

steeldriver
April 26th, 2014, 10:26 PM
I don't know if it's the "right" way, but you can do

>>> samplelist = [-16, -12, -8, -4, 0, 4, 8, 12, 66]

>>> subsamplelist = [x for x in samplelist if x in range(-11,5)]
>>> print subsamplelist
[-8, -4, 0, 4]

>>> hexlist = [hex(x) for x in samplelist if x > 0]
>>> print hexlist
['0x4', '0x8', '0xc', '0x42']

Yozuru
April 26th, 2014, 10:45 PM
You're wonderful steel!

I say there is no right way of coding in python as long as it solves it. (:

ofnuts
April 26th, 2014, 11:25 PM
>>> subsamplelist = [x for x in samplelist if x in range(-11,5)]
>>> print subsamplelist
[-8, -4, 0, 4]

Using range() for this isn't very good, it will make python search a list instead of comparing with two values. And range(-11,5) doesn't include 5 which is an allowed value.

trent.josephsen
April 27th, 2014, 12:22 AM
Using range() for this isn't very good, it will make python search a list instead of comparing with two values.
True for Python 2, but not for Python 3.