将列表中的元素与嵌套循环进行比较
compare elements in list with nested loops
我正在寻找一种变体,以确定字符串列表中的字符 ("b") 是否跟随某个字符 ("a") 使用嵌套循环。然后程序应计算满足上述条件的字符串总数。
我使用 .find
编写了以下适用于我的代码
nStrings = int ( input ( "input n amount of strings: " ))
listStr = [ ]
sumStr = 0
for i in range (0, len(nStrings)):
newStr = input ("enter string: ")
listn.append(newStr)
for i in range (0, len(listStr)):
if listn[i].find("a") < listn[i].find("b"):
sumStr = sumStr + 1
print("sumStr")
但是,我正在寻找一种使用嵌套循环执行此操作的方法。
我目前的做法是
for i in range (0, len(listStr)):
if listStr[i] == "a":
foundA = i
for j in range (i+1, len(listStr)):
if list[j] == "b":
foundB = j
if foundA < foundB:
afterA = True
然而,这对我来说根本不起作用。如果尝试了几个变体,但我确定我犯了一个逻辑错误。
我不会使用嵌套的 for
循环进行比较,您可以使用 str.find()
方法(正如@cricket_007 指出的那样,这是一个隐含的循环):
def getinput():
nStrings = int(input("inuput n amount of strings:"))
l = []
for i in range(nStrings):
l.append(input('enter string\n'))
return l
def compare(itm):
if 'a' in itm and 'b' in itm:
return True if itm.find('a') < itm.find('b') else False
else:
return None
listStr = getinput()
print zip(listStr, map(compare, listStr))
给定输入:['David', 'alphabet', 'bagpipes']
,这应该 return 列表中的元组:
[('David', None), ('alphabet', True), ('bagpipes', False)]
你可以使用一个循环
letter1 = "a"
letter2 = "b"
for i in range (0, len(listStr)):
if listStr[i] == letter1:
pos1 = i
if listStr[i] == letter2:
pos2 = i
foundAfter = (pos2 > pos1) # an error is here if the letters weren't found at all
下面是检查字符串中 a
之后是否出现 b
的函数的实现:
def a_before_b(haystack):
found_a = False
for letter in haystack:
if letter == 'a':
found_a = True
elif letter == 'b' and found_a: # we found 'b' and already have found 'a' earlier
return True
return False
为避免.find()
,请使用此方法代替行
if listn[i].find("a") < listn[i].find("b"):
在你的代码中,像这样:
if a_before_b(listn[i]):
我正在寻找一种变体,以确定字符串列表中的字符 ("b") 是否跟随某个字符 ("a") 使用嵌套循环。然后程序应计算满足上述条件的字符串总数。
我使用 .find
编写了以下适用于我的代码nStrings = int ( input ( "input n amount of strings: " ))
listStr = [ ]
sumStr = 0
for i in range (0, len(nStrings)):
newStr = input ("enter string: ")
listn.append(newStr)
for i in range (0, len(listStr)):
if listn[i].find("a") < listn[i].find("b"):
sumStr = sumStr + 1
print("sumStr")
但是,我正在寻找一种使用嵌套循环执行此操作的方法。
我目前的做法是
for i in range (0, len(listStr)):
if listStr[i] == "a":
foundA = i
for j in range (i+1, len(listStr)):
if list[j] == "b":
foundB = j
if foundA < foundB:
afterA = True
然而,这对我来说根本不起作用。如果尝试了几个变体,但我确定我犯了一个逻辑错误。
我不会使用嵌套的 for
循环进行比较,您可以使用 str.find()
方法(正如@cricket_007 指出的那样,这是一个隐含的循环):
def getinput():
nStrings = int(input("inuput n amount of strings:"))
l = []
for i in range(nStrings):
l.append(input('enter string\n'))
return l
def compare(itm):
if 'a' in itm and 'b' in itm:
return True if itm.find('a') < itm.find('b') else False
else:
return None
listStr = getinput()
print zip(listStr, map(compare, listStr))
给定输入:['David', 'alphabet', 'bagpipes']
,这应该 return 列表中的元组:
[('David', None), ('alphabet', True), ('bagpipes', False)]
你可以使用一个循环
letter1 = "a"
letter2 = "b"
for i in range (0, len(listStr)):
if listStr[i] == letter1:
pos1 = i
if listStr[i] == letter2:
pos2 = i
foundAfter = (pos2 > pos1) # an error is here if the letters weren't found at all
下面是检查字符串中 a
之后是否出现 b
的函数的实现:
def a_before_b(haystack):
found_a = False
for letter in haystack:
if letter == 'a':
found_a = True
elif letter == 'b' and found_a: # we found 'b' and already have found 'a' earlier
return True
return False
为避免.find()
,请使用此方法代替行
if listn[i].find("a") < listn[i].find("b"):
在你的代码中,像这样:
if a_before_b(listn[i]):