相关疑难解决方法(0)

优雅的pythonic解决方案,简单的循环

该函数bars获取一个Foo对象列表并返回这些bar属性的列表:

 def bars(foos):
      bars = []
      for foo in foos:
          bars.append(foo.bar)
      return bars
Run Code Online (Sandbox Code Playgroud)

来自Java背景,这是我过去解决这个问题的方式.但我觉得有更优雅的pythonic方式来解决这个问题.我该怎么办?

python python-2.7

0
推荐指数
1
解决办法
407
查看次数

使用return而不是yield

回报是否好于收益?从我读过它可以.在这种情况下,我无法从if语句获取迭代.基本上该程序所做的是取两点,即开始和结束.如果这两个点相距至少十英里,则需要随机抽样.显示的最终if语句适用于从开始点开始的前20英里,begMi.nCounter.length = 10并且是一个类成员.所以问题是,如何将代码调整到return语句的工作位置而不是yield?或者在这种情况下收益率是否合理?

def yielderOut(self):
    import math
    import random as r
    for col in self.fileData:
        corridor = str(col['CORRIDOR_CODE'])
        begMi = float(col['BEGIN_MI'])
        endMi = float(col['END_MI'])
        roughDiff = abs(begMi - endMi)

        # if the plain distance between two points is greater than length = 10
        if roughDiff > nCounter.length:
            diff = ((int(math.ceil(roughDiff/10.0))*10)-10)
            if diff > 0 and (diff % 2 == 0 or diff % 3 == 0 or diff % 5 == 0)\
               and ((diff % roughDiff) >= diff):

                if (nCounter.length+begMi) …
Run Code Online (Sandbox Code Playgroud)

python csv class

0
推荐指数
1
解决办法
164
查看次数

如何使用嵌套for循环来改进此功能?

我想知道如何使用嵌套for循环来提高此函数的可读性.也许我可以用nested for looptags_files =

def search_repotag_for_file(search_filepath, repo):
    '''Goes through all tags, all files to find a github file entity
       which matches the search_filepath we are looking for'''
    all_tags = (tag for tag in repo.tags)
    tags_files = ((tag, file_ent) for tag in all_tags for file_ent in tag.commit.tree.traverse())
    matches = (tup for tup in tags_files if tup[1].path == search_filepath)
    return matches
Run Code Online (Sandbox Code Playgroud)

python for-loop nested generator

0
推荐指数
1
解决办法
40
查看次数

将函数的输出存储到变量中

我有以下返回数据的函数:

def get_comments():
 for i in data:
  comment_data = i['comments']
  for z in comment_data:
   comments = comment_data['data']
   for j in comments:
    comment = j['message']
    print(comment)
Run Code Online (Sandbox Code Playgroud)

我想将此函数的输出保存到变量中。我使用的是打印而不是返回(在函数 get_comments 中),因为返回只返回数据的最后一行。这就是我试图解释的:

def hypothetical(x):
return x

z = hypothetical(get_comments())
print(z)
Run Code Online (Sandbox Code Playgroud)

然而,变量 z 的输出是“无”。

当我尝试其他一些值(即)时:

 z = hypothetical(5)
 print(z)
Run Code Online (Sandbox Code Playgroud)

z当然等于5。
谢谢

python function

0
推荐指数
1
解决办法
44
查看次数

在Python中创建包含start和end之间所有整数的列表?

如果我有两个整数(开始,结束),

例如,

start, end = 3,8 
Run Code Online (Sandbox Code Playgroud)

然后我想创建一个包含3到8的所有整数的列表(也包括3和8)

[3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

我怎么能得到它?

python

-1
推荐指数
1
解决办法
129
查看次数

我试图在列表中找到所有匹配的答案,但它不起作用

我正在自学编程。在我的代码中,我正在练习嵌套循环,并且我正在做一个练习以查看是否可以从列表中找到所有匹配的数字。但是它只返回第一个数字,并没有遍历整个列表,有人可以帮忙吗?提前致谢

  mylist = []

def intersection(lst1, lst2):
  for n in lst1:
    for o in lst2:
      if n == o:
        mylist.append(n)
        return mylist
Run Code Online (Sandbox Code Playgroud)

python list python-3.x

-1
推荐指数
2
解决办法
107
查看次数

为什么我不能在收益率上使用yield?

我希望您考虑以下代码:

def func(alist):
    if len(alist) == 1:
        return arg * 2
    for item in alist:
        yield item * 2
Run Code Online (Sandbox Code Playgroud)

运行它时,出现以下错误:

SyntaxError: 'return' with argument inside generator
Run Code Online (Sandbox Code Playgroud)

现在,我意识到我无法做到这一点。但是,我想知道为什么。到底是什么导致Python抛出SyntaxError

python yield return function python-2.x

-3
推荐指数
1
解决办法
1375
查看次数

Python:返回函数不会返回列表

以下功能从服务器打印已安装的应用程序.

def getAppNames():
    for app in service.apps:
        print app.name   
Run Code Online (Sandbox Code Playgroud)

它工作得很好,它会打印一个已安装的应用程序列表,如下所示:

应用程序A.

应用程序B.

应用程序C.

App D

应用程序E.

App F.

但是,当我将"打印"更改为"返回"时,我得到的只是"App A".我已经看到了类似的问题,但我找不到解决方案,并探索了不同的方法.基本上我需要返回功能,如打印功能,我将不胜感激任何帮助.谢谢.

python return

-3
推荐指数
1
解决办法
6009
查看次数

从循环中返回元素,一次一个

我有一个简单的函数来解析.csv,如下所示

def grabber3(datafile):

with open(datafile, 'rb') as f:
    r =csv.DictReader(f)
    for line in r:
        del line['thisthing']
        print line
Run Code Online (Sandbox Code Playgroud)

这样可以正确地打印每条dict线,大约50行,如

{'NetworkStatus': '1', 'ID': '1469'}
{'NetworkStatus': '1', 'ID': '1470'}
etc etc
Run Code Online (Sandbox Code Playgroud)

但是我想在调用函数时返回这个,所以我将print语句更改为return as

def grabber3(datafile):

with open(datafile, 'rb') as f:
    r =csv.DictReader(f)
    for line in r:
        del line['thisthing']
    return line
Run Code Online (Sandbox Code Playgroud)

但它只返回第一行

{'NetworkStatus': '1', 'ID': '1469'}
Run Code Online (Sandbox Code Playgroud)

如何返回循环/所有dict行的每一行而不是第一行?

python dictionary

-3
推荐指数
1
解决办法
75
查看次数