Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List and string
#1
I have a list something like this:
courses = ['MATH201:A', 'CS222:C', 'IF100:B']

and user enters course name like
coursename = "MATH201"

I just want to find the index of the course that user entered in the list but error message says that "MATH201" is not in the list because "MATH201" is not exactly in the list. How can i find the index of this course without asking from user exactly "MATH201:A"
Reply
#2
You need to check if the pattern is in each element.
To achieve this, you've to iterate (direct or indirect) over the list.

Each element in the list is a str.
You can check each element. The methods startswith and endswith can be used with an if-condition. Str do also support containment checking. You can look if a str is in another str.


courses = ['MATH201:A', 'CS222:C', 'IF100:B']
predicate = "MATH201"
for index, course in enumerate(courses):
    if course.startswith(predicate):
        print(course, "starts with", predicate, "at index", index)
    elif predicate in course:
        print(course, "is contained in", predicate, "at index", index)
# is a generator
# https://wiki.python.org/moin/Generators
def find_all(sequence, predicate):
    for index, element in enumerate(sequence):
        if predicate in element:
            yield index


def find_first(sequence, predicate):
    for first in find_all(sequence, predicate):
        return first


courses = ['MATH201:A', 'CS222:C', 'IF100:B']
predicate = "MATH201"
index = find_first(courses, predicate)
if index is None:
    print(predicate, "was not found.")
else:
    print(predicate, "was found in sequence")
    print("Index:", index)
You can use the previous defined find_all to get more than one result.

courses = ['MATH201:A', 'CS222:C', 'MATH201:B', 'IF100:B', 'MATH201:C']
indices = list(find_all(courses, "MATH"))
You can also use a list comprehension:
courses = ['MATH201:A', 'CS222:C', 'MATH201:B', 'IF100:B', 'MATH201:C']
indicies = [index for index, element in enumerate(courses) if "MATH" in element]
Here are the operations for sequences:
https://docs.python.org/3/library/stdtyp...operations

A str is also a sequence. All this operation could be applied on str.
Enumerate: https://docs.python.org/3/library/functi...#enumerate
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,202 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,479 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,177 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,484 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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