Python:对于列表中的每个元组,检查字符串是否在元组中

Python: for each tuple in list, check if string in tuple

我知道如何循环访问列表中的所有元组。但是,我的问题是查看字符串是否在列表中的元组中。我构建了以下内容:

# where:
unformatted_returns = [('2015-6-10', u'88.48'), ('2015-6-9', u'86.73'), ('2015-6-8', u'86.15'), ('2015-6-5', u'86.05')]
date_new  = '2015-6-8'

for n in unformatted_returns: # The problem is here
    if str(date_new) in n[0]: 
        print "found date"
        rate_of_return_calc(date, date_new)
        pass
    else:
        print "date {0} not found".format(date_new)
        day_accumulater(d, m, y, date_new, date)

问题是循环n in unformatted_returns中的第一个元组不满足条件,因此打印"not found"。我显然不希望它这样做,因为 date_new 实际上在列表中!

那么,如何让程序循环遍历每个 n,然后如果所有 n 都不满足包含 date_new 则打印 "day not found"

else下移一级for 循环也采用 else 套件,当您 没有提前退出循环时执行 。然后添加一个break:

for n in unformatted_returns:
    if date_new == n[0]: 
        print "found date"
        rate_of_return_calc(date, date_new)
        break
else:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

我也清理了你的测试;您正在将 n[0] 与日期字符串进行匹配,您希望它们 相等 ,而不是让一个成为另一个的子字符串。

现在可能会发生以下两种情况之一:

  • date_new 等于其中一个元组的第一个元素。 break被执行,for循环结束,else组被跳过。

  • date_new 不等于元组的任何第一个元素。 break 永远不会执行,循环结束并执行 else 套件以显示未找到匹配项。

演示:

>>> unformatted_returns = [('2015-6-10', u'88.48'), ('2015-6-9', u'86.73'), ('2015-6-8', u'86.15'), ('2015-6-5', u'86.05')]
>>> date_new  = '2015-6-8'
>>> for n in unformatted_returns:
...     if date_new == n[0]: 
...         print "found date"
...         break
... else:
...     print "date {0} not found".format(date_new)
... 
found date
>>> date_new = '2015-6-7'  # not in the list
>>> for n in unformatted_returns:
...     if date_new == n[0]: 
...         print "found date"
...         break
... else:
...     print "date {0} not found".format(date_new)
... 
date 2015-6-7 not found

这显然只会找到第一个这样的匹配元素。

如果您必须处理所有匹配的元素,标志通常是最简单的:

found = False

for n in unformatted_returns:
    if date_new == n[0]: 
        print "found date"
        rate_of_return_calc(date, date_new)
        found = True

if not found:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

所有这些都假设 n[1] 也很有趣。如果您只需要知道日期是否为 present,请使用 any() 和生成器表达式来测试 a 匹配元素:

if any(n[0] == date_new for n in unformatted_returns):
    print "found date"
    rate_of_return_calc(date, date_new)
else:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

现在我们不知道哪个 n匹配了,但这并不重要。

只需使用一个标志:

unformatted_returns = [('2015-6-10', u'88.48'), ('2015-6-9', u'86.73'), ('2015-6-8', u'86.15'), ('2015-6-5', u'86.05')]
date_new  = '2015-6-8'
found_one = False

for n in unformatted_returns: # The problem is here
    if str(date_new) in n[0]: 
        print "found date"
        rate_of_return_calc(date, date_new)
        found_one = True
        # add a break here if you are only interested in the first match
if not found_one:
    print "date {0} not found".format(date_new)
    day_accumulater(d, m, y, date_new, date)

只需搜索匹配项,如果搜索没有结果,则没有找到:

matching_returns = [ur for ur in unformatted_returns if date_new == ur[0]]
if not matching_returns:
    print "date {0} not found".format(date_new)
else:
    # do something with matching_returns