-2

How to check if part of text is in tuple? For example:

my_data = ((1234L,), (23456L,), (3333L,))

And we need to find if 123 or 1234 is in tuple. I didn't worked lot with tuples before. In array we use:

if variable in array

But its not working for tuples like my_data

PS. 1st answer solved problem.

Emin Mastizada
  • 1,375
  • 2
  • 15
  • 30
  • 1
    Is it _text_ or _numbers_ you need to search (your example contains numbers); and do you want to find an exact match, or just if any element _contains_ the key. So in your case your are searching for `123`, but there is `1234`; should this be a match (because it starts with `123`) or no match (because it isn't exactly `123`)? – Burhan Khalid Dec 01 '13 at 05:57
  • @Emin you can find your answer here. http://stackoverflow.com/questions/2917372/how-to-search-a-list-of-tuples-in-python – Surinder ツ Dec 01 '13 at 06:08
  • @Surinderツ Name of that topic is How to search a list of tuples in Python and of course i know how to search it, so, that's why i couldn't find that post in search. – Emin Mastizada Dec 01 '13 at 06:29
  • @BurhanKhalid i wrote how to check if PART of text is in tuple, so, i don't need exact mach of text. – Emin Mastizada Dec 01 '13 at 06:30
  • @EminMastizada The Link I have provided is not the direct answer to the questions you asked. Its just a reference to get the idea on how to achieve what you want. – Surinder ツ Dec 01 '13 at 06:39
  • @Surinderツ thanks :) But you wrote it after somebody answered to the question, and users of stackoverflow are doing that when the question is copy of already answered question. Have a nice day. – Emin Mastizada Dec 01 '13 at 06:43

1 Answers1

2
def findIt(data, num):
    num = str(num)
    return any(num in str(i) for item in data for i in item)

data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)

Output

True
thefourtheye
  • 233,700
  • 52
  • 457
  • 497