-1

I have an array that has 1 element. This element contains: print(results_read[0])

[(u'n04019541', u'puck', 0.57829314), (u'n02974003', u'car_wheel', 0.24903433), (u'n03483316', u'hand_blower', 0.025689969), (u'n02910353', u'buckle', 0.015434729), (u'n04542943', u'waffle_iron', 0.012205523)]

How can I check if it contains 'car_wheel'? I tried:

if 'car_wheel' in results_read:
      print('yes')
else:
    print('no')

Is there any other way to do this?

Powisss
  • 1,072
  • 3
  • 9
  • 16
  • duplicate of http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method – yar Mar 30 '17 at 00:17

2 Answers2

2

It seems you have a list of list of tuples, you need to loop through the list to do the check one by one; If you just want to know if any tuple contains car_wheel, you can use any for that:

any('car_wheel' in t for t in results_read[0])
# True
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • As stated in [answer], please avoid answering unclear, overly-broad, typo, unreproducible, or duplicate questions. Write-my-code requests and low-effort homework questions are off-topic for [so] and more suited to professional coding/tutoring services. Good questions adhere to [ask], include a [mcve], have research effort, and have the potential to be useful to future visitors. Answering inappropriate questions harms the site by making it more difficult to navigate and encouraging further such questions, which can drive away other users who volunteer their time and expertise. – TigerhawkT3 Mar 30 '17 at 00:17
  • 1
    THANK YOU! exactly what I was looking for, will put down as answer in 5min! – Powisss Mar 30 '17 at 00:20
1

You can do something like this :

results_read = [(u'n04019541', u'puck', 0.57829314), (u'n02974003', u'car_wheel', 0.24903433), (u'n03483316', u'hand_blower', 0.025689969), (u'n02910353', u'buckle', 0.015434729), (u'n04542943', u'waffle_iron', 0.012205523)]

if 'car_wheel' in [results[1] for results in results_read] :
      print('yes')
else:
    print('no')

This will result in :

yes
Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25