انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

(Lec 6-PL-Python-List Structure (Part II

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 1
أستاذ المادة رائد نصر كاظم العبيدي       15/04/2019 20:22:33

1. Searching a List

After elements have been added to a list, a program can search for a given element. The operator determines an element’s presence or absence, but programmers often are more interested in the position of an element if it is found (for replacement, removal, or other use). Unfortunately, the list type does not include the convenient find method that is used with strings. Recall that find returns either the index of the given substring in a string or -1 if the substring is not found. Instead of finding, you must use the method index to locate an element’s position in a list. It is unfortunate that index raises an error when the target element is not found. To guard against this unpleasant consequence, you must first use the in operator to test for the presence and then the index method if this test returns True. The next code segment shows how this is done for an example list and target element:

Example 1: A program to print the position of the target element.

aList =[34, 45, 67]
target = 45
if target in aList:
print(aList.index(target))
else:
print(-1)


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم