Python 函数:返回列表的字符串和元组?
在使用这些函数时,我很难将字符串列表返回到元组和索引。第一个,我试图返回每个字母字符串的第一次出现及其索引位置,但到目前为止,我只能返回一个带有索引的字符串,我如何使它遍历每个列表并选择最早出现的字符串Python 函数:返回列表的字符串和元组?,python,list,function,python-3.x,Python,List,Function,Python 3.x,在使用这些函数时,我很难将字符串列表返回到元组和索引。第一个,我试图返回每个字母字符串的第一次出现及其索引位置,但到目前为止,我只能返回一个带有索引的字符串,我如何使它遍历每个列表并选择最早出现的字符串 area3 = [['.', '.', '.', 'a'], ['.', 'B', '.', 'a'], ['.', '.', '.', '.'], ['.', 'Y', 'Y', 'Y'], ['.', '.', '
area3 = [['.', '.', '.', 'a'],
['.', 'B', '.', 'a'],
['.', '.', '.', '.'],
['.', 'Y', 'Y', 'Y'],
['.', '.', '.', '.'],
['.', 'x', '.', '.']]
def zone_lists(area):
for row in area:
for letter in row:
if ('A' <= letter <= 'Z' or 'a' <= letter <= 'z'):
return letter, area.index(row), row.index(letter)
zone_lists(area3) # Should return [('a', 0, 3), ('B', 1, 0), ('Y', 3, 1), ('x', 5, 1)]
第一个功能:
area3 = [['.', '.', '.', 'a'],
['.', 'B', '.', 'a'],
['.', '.', '.', '.'],
['.', 'Y', 'Y', 'Y'],
['.', '.', '.', '.'],
['.', 'x', '.', '.']]
def zone_lists(area):
result = []
for i, row in enumerate(area):
for j, letter in enumerate(row):
if letter.isalpha():
result.append((letter, i, j))
break # remove this line if you want to have both 'B' and 'a' from row 1
return result
zone_lists(area3) # Should return [('a', 0, 3), ('B', 1, 0), ('Y', 3, 1), ('x', 5, 1)]
第二点:
def last_list_val(value, area):
for i in range(len(area) - 1, -1, -1):
row = area[i]
for j in range(len(row) - 1, -1, -1):
letter = row[j]
if value == letter:
return i, j
我在第一个函数中使用了enumerate
。但是如果您不喜欢它(或者由于某种原因不能使用它),您可以始终使用旧式的for i in range(len(area)):
来获取指示符,并使用area[i]
来获取相应的值,就像我在第二个函数中所做的那样(因为我反向操作,所以略有不同)。第一个函数:
area3 = [['.', '.', '.', 'a'],
['.', 'B', '.', 'a'],
['.', '.', '.', '.'],
['.', 'Y', 'Y', 'Y'],
['.', '.', '.', '.'],
['.', 'x', '.', '.']]
def zone_lists(area):
result = []
for i, row in enumerate(area):
for j, letter in enumerate(row):
if letter.isalpha():
result.append((letter, i, j))
break # remove this line if you want to have both 'B' and 'a' from row 1
return result
zone_lists(area3) # Should return [('a', 0, 3), ('B', 1, 0), ('Y', 3, 1), ('x', 5, 1)]
第二点:
def last_list_val(value, area):
for i in range(len(area) - 1, -1, -1):
row = area[i]
for j in range(len(row) - 1, -1, -1):
letter = row[j]
if value == letter:
return i, j
我在第一个函数中使用了
enumerate
。但是,如果您不喜欢它(或者出于某种原因不能使用它),您可以始终使用老式的for i in range(len(area)):
来获取指示符,并使用area[i]
来获取相应的值,就像我在第二个函数中所做的那样(因为我做了相反的操作,所以略有不同)。这是一个很好的生成器
考虑:
from string import ascii_letters
def zone_list(area):
for i, sl in enumerate(area):
t=next(((j,e) for j,e in enumerate(sl) if e in ascii_letters), None)
if t:
yield (t[1], i, t[0])
然后测试:
>>> list(zone_list(area3))
[('a', 0, 3), ('B', 1, 1), ('Y', 3, 1), ('x', 5, 1)]
然后,第二个功能变为:
def last_list_val(value,area):
for i, sl in enumerate(area):
seq=[(j,e) for j,e in enumerate(sl) if e==value]
if len(seq)>1:
yield (i, seq[-1][0])
然后:
>>> list(last_list_val('Y', area3))
[(3, 3)]
这是一个很好的发电机 考虑:
from string import ascii_letters
def zone_list(area):
for i, sl in enumerate(area):
t=next(((j,e) for j,e in enumerate(sl) if e in ascii_letters), None)
if t:
yield (t[1], i, t[0])
然后测试:
>>> list(zone_list(area3))
[('a', 0, 3), ('B', 1, 1), ('Y', 3, 1), ('x', 5, 1)]
然后,第二个功能变为:
def last_list_val(value,area):
for i, sl in enumerate(area):
seq=[(j,e) for j,e in enumerate(sl) if e==value]
if len(seq)>1:
yield (i, seq[-1][0])
然后:
>>> list(last_list_val('Y', area3))
[(3, 3)]
所以只有第一个字母才重要?i、 例如,
[','B','a']
忽略'a'
?是,对于第一个函数是。在第二个函数中,它应该选择距离底部最远的“Y”,即(3,3)。所以只有第一个字母才重要?i、 例如,[','B','a']
忽略'a'
?是,对于第一个函数是。在第二个函数中,它应该选择距离底部最远的“Y”,即(3,3)。区域[i]是在for循环的末尾还是在for循环之前?@HalLawrence仅使用enumeratearea[i]
可以去任何row
去的地方,row[j]
可以去任何letter
去的地方,但正如您所看到的,enumerate的可读性更高。是否有替代.isalpha的方法?我正在尝试使用尽可能少的内置函数。您仍然可以使用'A',区域[I]是在for循环的末尾还是之前?@HalLawrence只需使用enumeratearea[i]
可以去任何row
去的地方,row[j]
可以去任何letter
去的地方,但正如您所看到的,enumerate的可读性更高。是否有替代.isalpha的方法?我正在尝试使用尽可能少的内置函数。您仍然可以使用'A'