Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python '<';str和int实例之间不支持_Python_String_Integer_Min_Nested Lists - Fatal编程技术网

Python '<';str和int实例之间不支持

Python '<';str和int实例之间不支持,python,string,integer,min,nested-lists,Python,String,Integer,Min,Nested Lists,我在试着找出一群人中得分最低的 我的代码: list = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]] min_score = list[0][1] for x in list: for y in x: if (y < min_score): min_score=y print(min_score) list=[['Radhe',99],'Bajra

我在试着找出一群人中得分最低的

我的代码:

list = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]]
min_score = list[0][1]
for x in list:
    for y in x:
        if (y < min_score):
            min_score=y
print(min_score)
list=[['Radhe',99],'Bajrangi',98],'Ram',95],'Shyam',94],'Bharat',89]]
最低分数=列表[0][1]
对于列表中的x:
对于x中的y:
如果(y
我应该做些什么不同呢?

您正在迭代
x
的每个元素,而实际上您只关心
x[1]

for x in list:
    if x[1] < min_score:
        min_score = x[1]
列表中x的
:
如果x[1]<最小分数:
最低分数=x[1]
您还可以解压每个子列表,以使值更易于使用:

for name, score in list:
    if score < min_score:
        min_score = score
对于名称,列表中的分数:
如果分数<最小分数:
最低分数=分数
您正在迭代
x
的每个元素,而实际上您只关心
x[1]

for x in list:
    if x[1] < min_score:
        min_score = x[1]
列表中x的
:
如果x[1]<最小分数:
最低分数=x[1]
您还可以解压每个子列表,以使值更易于使用:

for name, score in list:
    if score < min_score:
        min_score = score
对于名称,列表中的分数:
如果分数<最小分数:
最低分数=分数
  • list
    是python中的内置数据类型,因此应该将变量重命名为其他类型,例如
    l

  • 在第一个for循环中,您正在为列表中的x执行
    ,因此您将x分配给列表的第一个元素,即
    ['Radhe',99]
    。在第二个for循环中,您将遍历x中的每一项,第一项是
    'Radhe'
    ,因此您将int与字符串进行比较

  • 考虑到这一点,您可以将代码重写为:

    l = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]]
    min_score = l[0][1]
    for x in l:
        if (x[1] < min_score):
            min_score=x[1]
    print(min_score)
    
    或者,您可以使用列表理解在一行中完成所有操作:

    min_score = min([x[1] for x in l])
    
  • list
    是python中的内置数据类型,因此应该将变量重命名为其他类型,例如
    l

  • 在第一个for循环中,您正在为列表中的x执行
    ,因此您将x分配给列表的第一个元素,即
    ['Radhe',99]
    。在第二个for循环中,您将遍历x中的每一项,第一项是
    'Radhe'
    ,因此您将int与字符串进行比较

  • 考虑到这一点,您可以将代码重写为:

    l = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]]
    min_score = l[0][1]
    for x in l:
        if (x[1] < min_score):
            min_score=x[1]
    print(min_score)
    
    或者,您可以使用列表理解在一行中完成所有操作:

    min_score = min([x[1] for x in l])
    
    试试这个

    given_list = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]]
    print(min(given_list, key=lambda x: x[1])[1])
    
    试试这个

    given_list = [['Radhe',99],['Bajrangi',98],['Ram',95],['Shyam',94],['Bharat',89]]
    print(min(given_list, key=lambda x: x[1])[1])
    

    您的
    x
    项是两个实体的列表,一个
    string
    int
    。x中y的
    生成str,然后是每个if语句的int。因此,您的第一个测试是对照
    99
    min(您的lst,key=lambda x:x[1])
    检查
    “Radhe”
    ?您的
    x
    项是两个实体的列表,一个
    字符串和
    int
    。x中y的
    生成str,然后是每个if语句的int。因此,您的第一个测试是检查
    “Radhe”
    99
    min(您的l st,key=lambda x:x[1])
    ?是的,我现在看到了,谢谢!!有一个新的小故障。尽管部署了一段修正,但我无法比较float和str。下面是代码:n=int(input())list=[]表示范围内的i(n+1):name=input()score=float(input())list.append([name,score])print(list)min_scores=list[0][1]表示列表中的x:if(x[1]是的,我现在看到了,谢谢!!有一个新的问题。尽管部署了一段修正,我还是无法比较float和str。下面是代码:n=int(input())list=[]对于范围(n+1)内的i:name=input()score=float(input())list.append([name,score])为列表中的x打印(list)min_scores=list[0][1]:如果(x[1]有新的故障。尽管部署了一段修正,我还是无法比较float和str。下面是代码:n=int(input())list=[]用于范围内的I(n+1):name=input()scores=float(input())list.append([name,scores])print(list)min_scores=list[0][1]对于列表中的x:if(x[1]@dubey..请将此添加到原始问题中,并在堆栈溢出中提出单独的问题…阅读注释部分中的代码片段有点困难,出现了一个新的问题。尽管部署了一段修正,但我无法比较float和str。下面是代码:n=int(input())list=[]表示范围内的I(n+1):name=input()score=float(input())list.append([name,score])print(list)min_scores=list[0][1]用于列表中的x:if(x[1]@dubey..请将此添加到原始问题中,并在堆栈溢出中提出单独的问题…在注释部分阅读代码片段有点困难