简体   繁体   English

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

[英]Python: for each tuple in list, check if string in tuple

I know how to cycle through all tuples in a list. 我知道如何遍历列表中的所有元组。 However, my problem is to see if a string is in a tuple within the list. 但是,我的问题是查看字符串是否在列表中的元组中。 I have constructed the following: 我构造了以下内容:

# 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)

The problem is that the first tuple in the cycle n in unformatted_returns does not satisfy the condition, therefore it prints "not found". 问题在于unnated_returns中循环n in unformatted_returns的第一个元组不满足条件,因此它打印“找不到”。 I don't want it do this obviously, because date_new is actually in the list! 我不希望它这样做,因为date_new实际上在列表中!

So, how do I get the program to cycle through each n , then if all n 's do not satisfy containing date_new then print "day not found" ? 因此,如何使程序循环遍历每个n ,然后如果所有n不满足包含date_new则打印"day not found"

Move the else one level down . else 向下一级 for loops also take an else suite, which is executed when you did not exit the loop early . for循环还采用了else套件,该套件在您没有提前退出循环时执行。 Then add a break : 然后添加一个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)

I've also cleaned up your test; 我也清理了你的考试; you are matching n[0] against a date string, you want them to be equal , not for one to be a substring of the other. 您要将n[0]与日期字符串进行匹配,希望它们相等 ,而不是让其中一个成为另一个的子字符串。

Now one of two things can happen: 现在可以发生以下两种情况之一:

  • date_new is equal to the first element of one of the tuples. date_new等于其中一个元组的第一个元素。 The break is executed, the for loop ends, and the else suite is skipped. 执行breakfor循环结束, else套件被跳过。

  • date_new is not equal to any of the first elements of the tuples. date_new不等于元组的任何第一个元素。 The break is never executed, the loop ends and the else suite is executed to show no match was found. 决不执行break ,循环结束,执行else套件以显示未找到匹配项。

Demo: 演示:

>>> 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

This obviously would only ever find the first such matching element. 显然,这只会找到第一个这样的匹配元素。

If you must process all matching elements, a flag is usually easiest: 如果必须处理所有匹配的元素,则通常最简单的标志是:

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)

All this assumes that n[1] is of interest too. 所有这些都假定n[1]也很重要。 If all you need to know if the date is present , use any() and a generator expression to test for a matching element: 如果你需要知道,如果日期是目前使用any()和发电机表达式来测试一个匹配的元素:

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)

Now we won't know which n matched, but that doesn't actually matter. 现在我们不知道哪个 n匹配,但这实际上并不重要。

Just use a flag: 只需使用一个标志:

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)

Just search for matches and if the search comes with no results, nothing was found: 只是搜索匹配项,如果搜索没有结果,则找不到任何内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM