So I am trying to better understand functions and using for loops to select from multiple lists. Here is the basic function that I created and it does work:
def my_function(person, feeling):
print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))
my_function('Matthew', 'rejuvinated')
But to take this further, I would like to pick a name and a feeling from their respective lists and insert them into the new function. When I try the following, I get an error. Any help would be greatly appreciated!
people = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']
def my_function(person, feeling):
"""This function produces a statement which inserts a name and a feeling"""
for p in enumerate(people):
person = p
for f in enumerate(feelings):
feeling = f
print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))
return my_function()
my_function(people, feelings)
Hello, (7, 'Pete'), how are you? It seems that you are feeling (8, 'nerdy')!
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-2b2a265c427c> in <module>
11 return my_function()
12
---> 13 my_function(people, feelings)
<ipython-input-1-2b2a265c427c> in my_function(person, feeling)
9 feeling = f
10 print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))
---> 11 return my_function()
12
13 my_function(people, feelings)
TypeError: my_function() missing 2 required positional arguments: 'person' and 'feeling'
Two loop over two list in the same time use, zip
:
peoples = ['Colby', 'Hattie', 'Matthew', 'Stephen', 'Lee', 'Deb', 'Sharon', 'Pete']
feelings = ['happy', 'sad', 'cold', 'cranky', 'happy', 'successful', 'spunky', 'warm', 'nerdy']
def my_function(persons, feelings):
"""This function produces a statement which inserts a name and a feeling"""
# python provide a zip built-in function to loop over multiple list in the same time
for person, feeling in zip(persons, feelings):
# print the message for every element
print('Hello, %s, how are you? It seems that you are feeling %s!' %(person, feeling))
my_function(peoples, feelings)
The
zip()
function takes:iterables - can be built-in
iterables
(like:list, string, dict
), or user-defined iterables (object that has__iter__
method)The
zip()
function returns an iterator of tuples based on theiterable
object.
- If no parameters are passed,
zip()
returns an empty iterator- If a single iterable is passed,
zip()
returns an iterator of 1-tuples. Meaning, the number of elements in each tuple is 1.- If multiple iterables are passed,
ith
tuple containsith
Suppose, two iterables are passed; one iterable containing 3 and other containing 5 elements. Then, the returned iterator has 3 tuples. It's because iterator stops when shortest iterable is exhaused.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다