PYTHON array_search

is this article helpful?
|
Python replacement for PHP's array_search [ edit | history ]
# It works just for lists, that are number-indexed arrays:
haystack.index(needle)


# It works for dictionaries, that allows text-indexed values
index = [k for k, v in haystack.iteritems() if v == needle][0]


or a new not-sure optimally but works for both list and dict

def array_search(needle, haystack, strict=False):
	if not strict:
		needle = str(needle)
		
	if is_array(haystack):
		if is_array_assoc(haystack):
			if not strict:
				key = [k for k, v in haystack.items() if str(v) == needle] 
				count_ = count(key)
				return key[0] if count_ > 0 else False
			else:
				key = [k for k, v in haystack.items() if v == needle] 
				count_ = count(key)
				return key[0] if count_ > 0 else False
		else:
			key = 0
			if not strict:
				for item in haystack:
					if str(item) == needle:
						return key
					key += 1
			else:
				for item in haystack:
					if item == needle:
						return key
					key += 1
		return False
	else:
		return False

PHP array_search

PHP original manual for array_search [ show | php.net ]