【问题标题】:How do I perform an action on each element of a list and put the results in a new list in Python?如何对列表的每个元素执行操作并将结果放入 Python 中的新列表中?
【发布时间】:2012-09-07 00:27:29
【问题描述】:

嘿,我在尝试解决这个问题时遇到了问题:

让我们从一个包含元素的列表和一个空白列表开始。

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

我需要做的是在 L[0] 上执行一项任务并将结果放入 BL[0]。 然后在 L[1] 上执行任务并将结果放入 BL [1]。 然后当然与列表中的最后一个元素相同。导致

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

我希望你明白我想要弄清楚的。我是编程新手,我猜这可能是通过 for 循环完成的,但我不断收到错误。

好的,这就是我尝试过的。注意:链接是链接列表。

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

我得到这个错误:

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'

【问题讨论】:

  • 能否请您发布到目前为止的代码以及您遇到的错误?
  • 我知道这似乎是一种回避,但确实值得您花时间阅读教程,例如 official one。您不必全部了解,但至少第 3-5 章是基本的,它们将帮助您至少了解您可以做哪些事情,以及应该在哪些方面寻求帮助。
  • html = [urlopen( links ).read() for link in links]:我想你的意思是html = [urlopen(link).read() for link in links]。想想links 是什么,以及urlopen(links) 如何生成您看到的错误消息。
  • 好电话..再次感谢帝斯曼

标签: python list loops


【解决方案1】:

了解列表推导。

BL = [action(el) for el in L]

【讨论】:

    【解决方案2】:

    简单,这样做:

    BL = [function(x) for x in L]
    

    【讨论】:

    • 我知道你用这个去哪里了。也许是我正在使用的功能导致了问题。
    • 这是最简单的方法,谢谢丘比特,是我搞砸了语法:)
    【解决方案3】:

    您创建一个函数,执行您想要的所有操作并使用 map 函数

    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)
    

    【讨论】:

    • @BlaXpirit:它将在 Python 2 中。
    • @DSM 哦,我知道,但是 OP 没有说明他们使用的是过时的 Python 版本。
    【解决方案4】:

    怎么样?

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

    不像一些答案那么简洁,但对我来说很容易理解。

    下面每个 cmets 的疯狂变化。

    【讨论】:

    • append 列表中的某样东西,你没有分配给它。所以BL.append(x) = do_something(L)错了,应该是BL.append(do_something(x))
    【解决方案5】:

    这里有几种不同的方法,第一次运行时都假设L = ['a', 'b', 'c']BL = []

    # 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]
    

    一些文档:

    【讨论】:

      【解决方案6】:

      好的,我继承了我尝试过的内容。注意:链接是链接列表。

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

      在这里,您已经要求 Python 迭代 links,使用 link 作为每个元素的名称...并且对于每个 link,您调用 urlopen... 和 links,即整个列表。大概你想每次都传递给定的link

      【讨论】:

        【解决方案7】:

        这里一个有用的工具是函数式编程。 Python 支持一些高阶函数可以解决这个问题。

        我们特别想使用的函数称为map。这里的一些答案使用地图,但没有一个完全接受功能方法。为此,我们将使用函数式编程中使用的“lambda”,而不是使用标准的 python 'def' 创建函数。这使我们能够在一行中很好地解决您的问题。

        要了解更多关于 lambda 为何有用的信息,请访问 here。我们将按照以下方式解决您的问题:

        r = map(lambda x: urlopen(x).read(), L)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-30
          • 2023-03-09
          • 2017-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多