Python 3.x 2D列表python中的list.count(x)
我对python非常陌生,我试图计算2D数组中有多少个x,但是list.count(x)给了我0,但我知道它不是零 我显示我所有的代码。该代码应该执行以下操作:从文件中读取名称和年龄,取最大和最小年龄,并在其他文件中打印,所有这些都具有相同的最大和最小年龄Python 3.x 2D列表python中的list.count(x),python-3.x,Python 3.x,我对python非常陌生,我试图计算2D数组中有多少个x,但是list.count(x)给了我0,但我知道它不是零 我显示我所有的代码。该代码应该执行以下操作:从文件中读取名称和年龄,取最大和最小年龄,并在其他文件中打印,所有这些都具有相同的最大和最小年龄 with open("duomenys.txt") as d: sar = [line.split() for line in d] max = 0 min = 200 for index, item in enumerate(sa
with open("duomenys.txt") as d:
sar = [line.split() for line in d]
max = 0
min = 200
for index, item in enumerate(sar):
if int(sar[index][1]) > max:
max = int(sar[index][1])
pozmax = index
for index, item in enumerate(sar):
if int(sar[index][1]) < min:
min = int(sar[index][1])
pozmin = index
with open('vyr_jaun.txt', 'w') as d:
d.write (sar[pozmin][0]+' '+sar[pozmin][1]+'\n')
with open('vyr_jaun.txt', 'a') as d:
d.write (sar[pozmax][0]+' '+sar[pozmax][1])
(请注意,数组中的年龄是字符串。这不是问题。)
但我只有一张单子<代码>['Justas','23'],['Mantas','18'],['Rokas','33'],['Anzelmute','69'],['Ruta','11'],['Vilius','2'],['Monika','6'],['Petras','17'],['Stasys','26'],['Zose','13'],['Jurgis','2'],['Borisas','69']我不明白你的问题与你展示的代码有什么关系。你在哪里计数?
Justas 23
Mantas 18
Rokas 33
Anzelmute 69
Ruta 11
Vilius 2
Monika 6
Petras 17
Stasys 26
Zose 13
Jurgis 2
Borisas 69
what = str(9) # whatever you are searching for
count = 0
for name,age in array:
if age == what:
count += 1
print("there are",count,"people with the age",what)