5

Hey I'm having a problem trying to figure this out:

Lets start out with a list with elements and a blank list.

L = [a, b, c]  
BL = [  ]

What I need to do is perform a task on L[0] and have the result put into BL[0]. Then perform a task on L[1] and have the result put into BL [1]. And then of course the same with the last element in the list. Resulting in

L = [a, b, c]
BL =[newa, newb, newc]

I hope you understand what I'm trying to figure out. I'm new to programming and I'm guessing this is probably done with a for loop but I keep getting errors.

Ok SO I here's the what I tried. Note: links is a list of links.

def blah(links):
   html = [urlopen( links ).read() for link in links]
   print html[1]

and I get this error:

Traceback (most recent call last):
File "scraper.py", line 60, in <module>
main()
File "scraper.py", line 51, in main
getmail(links)
File "scraper.py", line 34, in getmail
html = [urlopen( links ).read() for link in links]
File "/usr/lib/python2.6/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.6/urllib.py", line 177, in open
fullurl = unwrap(toBytes(fullurl))
File "/usr/lib/python2.6/urllib.py", line 1032, in unwrap
url = url.strip()
AttributeError: 'list' object has no attribute 'strip'
BenMorel
  • 34,448
  • 50
  • 182
  • 322
moretimetocry
  • 73
  • 1
  • 1
  • 4
  • Can you please post your code so far and the error you are getting? – Martijn Pieters Sep 13 '12 at 20:35
  • 2
    I know it might seem like a sidestep, but it's really worth your time to go through a tutorial, like the [official one](http://docs.python.org/tutorial/). You don't have to understand it all, but at least chapters 3-5 are fundamental, and they'll help you know at least what sort of things you can do, and therefore what to search for help on. – DSM Sep 13 '12 at 20:38
  • 4
    `html = [urlopen( links ).read() for link in links]`: I think you mean `html = [urlopen(link).read() for link in links]`. Think about what `links` is, and how `urlopen(links)` could generate the error message you see. – DSM Sep 13 '12 at 20:50
  • Good call there.. Thank you again DSM – moretimetocry Sep 13 '12 at 21:01

7 Answers7

10

Simple, do this:

BL = [function(x) for x in L]
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
5

Ok SO I heres the what i tried.. Note: links is a list of links.

html = [urlopen( links ).read() for link in links]

Here, you have asked Python to iterate over links, using link as a name for each element... and with each link, you call urlopen... with links, i.e. the entire list. Presumably you wanted to pass a given link each time.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1

Learn about list comprehensions.

BL = [action(el) for el in L]
Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
1

Here's a few different approaches, all assuming L = ['a', 'b', 'c'] and BL = [] when they're first run.

# Our function
def magic(x):
    return 'new' + x

#for loop - here we loop through the elements in the list and apply
# the function, appending the adjusted items to BL
for item in L:
    BL.append(magic(item))

# map - applies a function to every element in L. The list is so it
# doesn't just return the iterator
BL = list(map(magic, L))

# list comprehension - the pythonic way!
BL = [magic(item) for item in L]

Some documentation:

thegrinner
  • 11,546
  • 5
  • 41
  • 64
0

You make a function, that does all the operations you want and use map function

def funct(a): # a here is L[i]
     # funct code here
     return b #b is the supposed value of BL[i]
BL = map(funct, L)
Mahmoud Aladdin
  • 536
  • 1
  • 3
  • 13
0

how about?

x = 0
for x in range(len(L)):
    BL.append(do_something(x))

not as succinct as some answers, but easy for me to understand.

mad changes per comments below.

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • 1
    You `append` something to your list, you don't assign to it. So `BL.append(x) = do_something(L)` is wrong, it should be `BL.append(do_something(x))`. – SexyBeast Sep 14 '12 at 06:50
0

A useful tool here is functional programming. Python supports some higher order functions which can solve this problem.

The function in particular that we want to use is called map. A few of the answers here use map, but none fully embrace the functional approach. To do so, instead of using your standard python 'def' to create a function, we will use 'lambda' which is used in functional programming. This allows us to solve your problem nicely in one line.

To see more about why lambdas are useful go here. We will solve your problem as follows:

r = map(lambda x: urlopen(x).read(), L)
Community
  • 1
  • 1
Andrew
  • 1
  • 3