简体   繁体   English

这是一个非序列?

[英]how is this a non-sequence?

I'm running a list comprehension of a list of numbers as strings so for example the list looks like this 我正在运行列表理解数字列表作为字符串,所以例如列表看起来像这样

vals = ['0.13', '324', '0.23432']

and try a list comprehension like this: 并尝试这样的列表理解:

best = [x for x in vals > 0.02]

and I get a TypeError: iteration over non-sequence. 我得到一个TypeError:迭代非序列。

Isn't a list a sequence that should be the first thing you should be able to iterate through? 列表不是一个应该是您应该能够迭代的第一个序列吗? What is a sequence? 什么是序列?

It's hard to find answers to basic questions I'm finding. 我找到的基本问题很难找到答案。

Thanks. 谢谢。

You need to check if each item is greater than '0.02', not whether the sequence is greater. 您需要检查每个项目是否大于'0.02',而不是序列是否更大。

best = [x for x in vals if x > '0.02']

Your original expression, [x for x in vals > '0.02'] is parsed as [x for x in (vals > '0.02')] . 您的原始表达式[x for x in vals > '0.02']被解析为[x for x in (vals > '0.02')] Since vals > '0.02' is a boolean value, and not a sequence, it's not possible to iterate over it. 由于vals > '0.02'是一个布尔值,而不是一个序列,因此不可能迭代它。

EDIT: I updated this answer to use the string '0.02' per Joe's suggestion in the comments (thank you). 编辑:我更新了这个答案,在评论中使用每个Joe的建议字符串'0.02' (谢谢)。 That works in this scenario, but in the event that you really wanted to do a numeric comparison instead of a lexicographic one, you could use: 这适用于这种情况,但如果你真的想要进行数字比较而不是字典比较,你可以使用:

best = [x for x in vals if float(x) > 0.02]

This converts x to a float so that you are comparing a floating-point number to another floating-point number, probably as intended. 这会将x转换为float,以便您将浮点数与另一个浮点数进行比较,可能与预期的一样。 The result of the list comprehension will still be a list of strings, since we are collecting [x for ...] and not [float(x) for ...] . 列表推导的结果仍然是字符串列表,因为我们正在收集[x for ...]而不是[float(x) for ...] Just some food for thought. 只是一些值得思考的东西。

No, vals > 0.02 is not exactly a sequence. 不, vals > 0.02并不完全是一个序列。 Also, comparing strings (contained in vals) would not yield the result you expect. 此外,比较字符串(包含在val中)不会产生您期望的结果。 You might want to do: 你可能想做:

vals = [0.13, 324.0, 0.23432]
best = [x for x in vals if x > 0.02]

That being said, be sure to take a look at NumPy . 话虽这么说,一定要看看NumPy It allows you to write your example as: 它允许您将您的示例编写为:

from numpy import *
vals = asarray([0.13, 324.0, 0.23432])
best = vals[vals > 0.02]

While that might not seem much, it offers a plethora of features and advantages you would not want to miss working with numeric arrays and matrices. 虽然这看起来可能不多,但它提供了许多功能和优点,您不希望错过使用数字数组和矩阵。

You are trying to iterate over vals > 0.02 which is not a sequence. 你试图迭代vals > 0.02 ,这不是一个序列。 If you are trying to filter to just anything > 0.02 do: [x for x in vals if x > 0.02] 如果你试图过滤到任何> 0.02 do: [x for x in vals if x > 0.02]

You've also got another problem (apart from the missing if x > 0.02), you're comparing a list of string with a float. 你还遇到了另一个问题(除了缺少x> 0.02),你要比较一个字符串列表和一个浮点数。

So what you probably want is [x for x in vals if x > '0.02'] 所以你可能想要的是[x for x in vals if x > '0.02']

I've tested that this will give you the expected behaviour. 我已经测试过这会给你预期的行为。 ['324', '0.23432']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM