Python列表理解设置局部变量
我在Python中理解列表时遇到问题 基本上我有这样的代码Python列表理解设置局部变量,python,python-2.7,list-comprehension,Python,Python 2.7,List Comprehension,我在Python中理解列表时遇到问题 基本上我有这样的代码 output = [] for i, num in enumerate(test): loss_ = do something test_ = do something else output.append(sum(loss_*test_)/float(sum(loss_))) 我如何使用列表理解编写此内容,例如: [sum(loss_*test_)/float(sum(loss_))) for i, num
output = []
for i, num in enumerate(test):
loss_ = do something
test_ = do something else
output.append(sum(loss_*test_)/float(sum(loss_)))
我如何使用列表理解编写此内容,例如:
[sum(loss_*test_)/float(sum(loss_))) for i, num in enumerate(test)]
然而,我不知道如何分配
loss_
和test_
的值,正如雅罗斯拉夫在评论中提到的那样,列表理解不允许将值直接保存到变量中
但是,它允许您使用函数
我已经做了一个非常基本的示例(因为您提供的示例不完整,无法测试),但它应该显示您如何仍然可以在列表中执行代码
def loss():
print "loss"
return 1
def test():
print "test"
return 5
output = [loss()*test() for i in range(10) ]
print output
这种情况下,将产生一个列表[5,5,5,5,5,5,5,5]
我希望这能以某种方式显示出你是如何最终得到你想要的行为的。正如雅罗斯拉夫在评论中提到的,列表理解不允许你直接将值保存到变量中 但是,它允许您使用函数 我已经做了一个非常基本的示例(因为您提供的示例不完整,无法测试),但它应该显示您如何仍然可以在列表中执行代码
def loss():
print "loss"
return 1
def test():
print "test"
return 5
output = [loss()*test() for i in range(10) ]
print output
这种情况下,将产生一个列表[5,5,5,5,5,5,5,5]
我希望这能以某种方式显示出您最终如何获得所需的行为。您可以使用嵌套列表理解来定义这些值:
output = [sum(loss_*test_)/float(sum(loss_))
for loss_, test_ in ((do something, do something else)
for i, num in enumerate(test))]
当然,这是否更具可读性是另一个问题。您可以使用嵌套列表理解来定义这些值:
output = [sum(loss_*test_)/float(sum(loss_))
for loss_, test_ in ((do something, do something else)
for i, num in enumerate(test))]
ip_list = string.split(" ") # split the string to a list using space seperator
for i in range(len(ip_list)): # len(ip_list) returns the number of items in the list - 4
# range(4) resolved to 0, 1, 2, 3
if (i % 2 == 0): ip_list[i] += "-" # if i is even number - concatenate hyphen to the current IP string
else: ip_list[i] += "," # otherwize concatenate comma
print("".join(ip_list)[:-1]) # "".join(ip_list) - join the list back to a string
# [:-1] trim the last character of the result (the extra comma)
当然,这是否更具可读性是另一个问题。您是否尝试过这样做?怎么搞的?你真正“遇到麻烦”的代码在哪里?你所说的麻烦到底是什么意思?A会有帮助的。为什么?现在的for循环有什么问题吗?在python2中,不是执行
c=a/float(b)
,而是执行来自未来导入部分的;c=a/b
,请参阅。这是Python 3中的默认值。@BasSwinckelsthanks@MorganThrapp当前代码没有任何问题,我只想转换成列表格式。您是否尝试过这样做?怎么搞的?你真正“遇到麻烦”的代码在哪里?你所说的麻烦到底是什么意思?A会有帮助的。为什么?现在的for循环有什么问题吗?在python2中,不是执行c=a/float(b)
,而是执行来自未来导入部分的;c=a/b
,请参阅。这是Python 3中的默认值。@BasSwinckelsthanks@MorganThrapp当前代码没有问题,我只想转换成列表理解格式。我认为问题不在于“如何在列表理解中执行复杂的内容”,而在于“如何计算一次临时变量并在两个位置使用它”,正如OP对loss.
@tobias\u k的解释比我自己解释得更好,我认为问题不在于“如何在列表理解中执行复杂的内容”,而在于“如何计算一次临时变量并在两个位置使用它”,正如OP对损失所做的那样,
@tobias\u k解释得比我自己解释得好
ip_list = string.split(" ") # split the string to a list using space seperator
for i in range(len(ip_list)): # len(ip_list) returns the number of items in the list - 4
# range(4) resolved to 0, 1, 2, 3
if (i % 2 == 0): ip_list[i] += "-" # if i is even number - concatenate hyphen to the current IP string
else: ip_list[i] += "," # otherwize concatenate comma
print("".join(ip_list)[:-1]) # "".join(ip_list) - join the list back to a string
# [:-1] trim the last character of the result (the extra comma)