简体   繁体   English

比较列表中的两个项目的问题

[英]Problem with comparing two items in a list of lists

I have this list我有这个清单

zipped_values = [(3.0, 4.242640687119285), (3.605551275463989, 3.1622776601683795), (5.0, 3.1622776601683795), (4.123105625617661, 4.47213595499958), (5.0, 4.0), (5.830951894845301, 7.810249675906654), (5.0990195135927845, 5.385164807134504), (7.0710678118654755, 8.06225774829855), (7.0710678118654755, 7.280109889280518), (8.06225774829855, 8.246211251235321)]

what I am trying to do is compare each item within the lists to each other (within the same list), to find out what the minimum is.我想要做的是比较列表中的每个项目(在同一个列表中),找出最小值是多少。 In the first case, it would be 3.0, in the second, it would be 3.1.在第一种情况下,它将是 3.0,在第二种情况下,它将是 3.1。

Now what I am trying to do is, if the minimum is the first element, assign that element (3.0) to a dictionary key named M1, and if the minimum is the second element, assign that element to a dictionary key named M2.现在我要做的是,如果最小值是第一个元素,则将该元素(3.0)分配给名为 M1 的字典键,如果最小值是第二个元素,则将该元素分配给名为 M2 的字典键。

This is what I've come up with:这就是我想出的:

  for jk in zipped_value:
    for n in jk:
      if zipped_value[0][n] < zipped_value[0][n+1]:
        dictionary["M1"].append(zipped_value[0][n])

But it gives me this error: TypeError: tuple indices must be integers or slices, not float但它给了我这个错误: TypeError: tuple indices must be integers or slices, not float

What do I do?我该怎么办?

You don't need the inner loop.您不需要内部循环。 jk is the tuple, you just need to compare jk[0] with jk[1] . jk是元组,您只需要将jk[0]jk[1]进行比较。 You can simplify this by unpacking to two variables when looping.您可以通过在循环时解压缩为两个变量来简化此操作。

for a, b in zipped_values:
    if a < b:
        dictionary["M1"].append(a)
    else:
        dictionary["M2"].append(b)

Your inner for loop is iterating over the tuple, so the value of n is actually the float s in your tuple.您的内部for循环正在遍历元组,因此n的值实际上是您的元组中的float s。 Additionally, you will have problems with the append statement since it does not seem like you are initializing the dictionary values to be list type.此外,您将遇到append语句的问题,因为您似乎没有将字典值初始化为list类型。 Lastly, python allows you to unpack your tuples as you are iterative over them, making the code easier to read and comprehend rather than needing to use indices.最后,python 允许您在迭代元组时解包元组,使代码更易于阅读和理解,而不需要使用索引。

Try this:尝试这个:

from collections import defaultdict

dictionary = defaultdict(list)

for item1, item2 in zipped_value:
    if item1 < item2:
        dictionary["M1"].append(item1)
    else:
        dictionary["M2"].append(item2)

You are getting this error "TypeError: tuple indices must be integers or slices, not float" because in the "for n in jk" the 'n' here is actually representing the float values in tuples and is not the index of tuple.您收到此错误“TypeError:元组索引必须是整数或切片,而不是浮点数”,因为在“for n in jk”中,此处的“n”实际上表示元组中的浮点值,而不是元组的索引。 So,所以,

if zipped_value[0][n] < zipped_value[0][n+1]:
    dictionary["M1"].append(zipped_value[0][n])

is not correct indexing to access elements of tuples in a list.不正确的索引来访问列表中的元组元素。

You can modify your code to something below to get correct results:您可以将代码修改为以下内容以获得正确的结果:

for jk in zipped_values:
  if jk[0] < jk[1]:
    dictionary["M1"].append(jk[0])
  else:
    dictionary["M2"].append(jk[1])

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

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