Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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/3/clojure/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 列表检查第二项打印列表中的第一项_Python_Python 3.x - Fatal编程技术网

Python 列表检查第二项打印列表中的第一项

Python 列表检查第二项打印列表中的第一项,python,python-3.x,Python,Python 3.x,我正在尝试从列表(从文件)中剖析项目 到目前为止: filen=open("WorldData.txt", 'r') values = []##List for line in open(filen): values.append(line.rstrip('\n').split(',')) xl=values numlist=[] for line in xl: numlist.append(int(line[1]))#second element which are go

我正在尝试从列表(从文件)中剖析项目

到目前为止:

filen=open("WorldData.txt", 'r')

values = []##List
for line in open(filen):
    values.append(line.rstrip('\n').split(','))

xl=values

numlist=[]
for line in xl:
    numlist.append(int(line[1]))#second element which are goals

x=(max(numlist))
print(x)

filen.close()
答案是:22,但我也在努力找到获胜者的名字。 我尝试建立索引,以便找到值为[::]的行号,然后打印第一个元素

print(xl.index(str(x))) 
但是它说

ValueError: '22' is not in the list
所以我甚至到不了那里


我怎样才能找到谁的目标最多?

使用numlist作为字典。所以代码应该是这样的

goals = 0

numlist={}
for line in xl:
    numlist[line[0]]=int(line[1])#second element witch are goals

for player in numlist:
    if numlist[player] > goals:
        winner = player
        goals = numlist[player]
print goals, winner

答案是22沙龙。但会处理重复的,或者在多个获胜者之间打成平局。为此,您可以维护赢家列表并附加它。

以下是我如何读取csv数据:

import csv

data={}
with open(fn, 'r') as fin:
    reader=csv.reader(fin)
    header=next(reader)
    for row in reader:
        data[row[0]]=list(map(int, row[1:]))
print(data)
# {'Luis': [8, 3], 'Nayum': [3, 8], 'Montes and Kyle': [10, 8], 'Sharon': [22, 4]}
你现在可以看到你有一个

现在只需找到具有最大值的条目,该值是每个列表中的第一个元素。您可以使用内置的按键功能查看谁获胜:

print(max(data.items(), key=lambda t: t[1][0]))
# ('Sharon', [22, 4])
print(max(data.items(), key=lambda t: t[1][0]))
# ('Sharon', [22, 4])