mercredi 9 juin 2021

How to check if the value exist in string then get the item of value instead just value of true

I have variable like this

reg_name = ['aceh', 'sumatera utara', 'sumatera barat', 'riau', 'jambi', 'jakarta']
nama_laut = ['selat sunda', 'laut jawa', 'laut natuna', 'teluk natuna']
txt = 'berita ini berasal dari kota jakarta, tenggelamnya kapal KNKT di perairan selat sunda'

and the code like this

if any([x in txt for x in nama_laut]):
  print(nama_laut)
else:
  print(reg_name)

How can I get only name of value true? for example, based the code above I want to get the value of "selat sunda", but I dont know how get it. Can someone help me? Thanks..

Aucun commentaire:

Enregistrer un commentaire