Найти - Пользователи
Полная версия: Проблема с string.find()
Начало » Python для новичков » Проблема с string.find()
1
A.N.Onim
Здраствуйте!
Есть ф-ция:
def search(list, field, string):
	result = []
	for book in list:
		print field
		print dict_fields[field]
		print book[dict_fields[field]]
		print string
		if string.find(book[dict_fields[field]], string)>=0:
			result.append(book)
	return result
А вот она выдает ошибку:

То есть на скрине видно, что оба аргумента строки. А выдает ошибку. Почему?
FishHook
string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end].
Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.
у вас
string.find(book[dict_fields[field]], string)
второй аргумент функции должен быть числом, а не строкой
A.N.Onim
FishHook
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s.
Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.

Не пойму - первый аргумент строка в которой ищем, второй строка которую ищем?
FishHook
Вы метод строки вызываете, а не функцию модуля string.
Не надо использовать зарезервированные языком слова: string, list и не будет путаницы
FishHook
if string.find(book[dict_fields[field]])>=0:
A.N.Onim
FishHook
Ааааа, понял)))
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB