Python Forum
Find not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find not working properly
#2
You should use the in operator.
The in operator looks up, if an object appears in a sequence.
In [26]: 's' in 'Andre'
Out[26]: False

In [27]: 's' in 'Sandra'
Out[27]: False

In [28]: 's' in 'Sandra'.lower()
Out[28]: True
With a list:
In [29]: l = [1,2,3]

In [30]: 5 in l
Out[30]: False

In [31]: 1 in l
Out[31]: True
With filter:
In [32]: list(filter(lambda x: 's' in x.lower(), ['Leon', 'Sucks', 'Mikes', 'Balls']))
Out[32]: ['Sucks', 'Mikes', 'Balls']
If you have one finite list and want to have a second list multiplied by 2, you should use a list comprehension:
list1 = [1,2,3]
list2 = [i*2 for i in list1]
You can use conditions in a list comprehension:
list1 = list(range(100))
list2 = [i*2 for i in list1 if i % 2 != 0]
I think the to filter with a list comprehension looks very natural and reads like an english sentence.
name for name in A_SEQUENCE if 's' in name.lower
In [33]: [name for name in ['Leon', 'Sucks', 'Mikes', 'Balls'] if 's' in name.lower()]
Out[33]: ['Sucks', 'Mikes', 'Balls']
The list-, set-, dict comprehension and generator expressions are very helpful.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Find not working properly - by fad3r - Feb-11-2018, 02:38 PM
RE: Find not working properly - by DeaD_EyE - Feb-11-2018, 02:56 PM
RE: Find not working properly - by fad3r - Feb-11-2018, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Enigma Program not working properly npd29 3 2,125 May-01-2020, 10:37 AM
Last Post: pyzyx3qwerty
  Printing a number sequence not working properly deepsen 6 3,127 Oct-12-2019, 07:43 PM
Last Post: deepsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020