This can be done as a temporary step for ease of use if you would rather keep the original data in the format you have it in currently. Now you can iterate across the user and not have to deal with the more complex indexing that you would need with your current setup. )Īnd pair across each of these lists by their indexes into tuples: [ ('Zdolfalos', 'Fred', 'Charlotte', 'NC', '28210'), Upzip it by each entry, which are your categories zip( , The zip function in this case tells python to take the matrix, excluding the first entry which are your headers. Python being the awesomeness that it is, provides very quick and easy operations for such transformations: names_matrix = zip(*names_matrix) Making the iterations rather simple to iterate through your entries: for user in names_matrix: # means take the list from the 1st element to the end, noted by the lack of a number after the colon (on a 0 based index) I like it would make more sense to structure it as: names_matrix = ( One note here, I find your data to be arranged in a rather strange manner. I would say to perform the user input before the loop, and then compare as you have done with the slight change to the indexing performed above. It looks like you are trying to perform a search for someone in your data. Will give you: lname fname city state zipcode Print names_matrix,names_matrix,names_matrix,names_matrix,names_matrix This means you want to iterate across the size of your names_matrix, the second object in your list. Iteration 2 - Johnson, Malcom, Monroe, NC, 28337 You probably want information by column correct and ignoring the headers? iteration 1 - Zdolfalos, Fred, Charlotte, NC, 28210 In your case you have this data: names_matrix = (, When one argument is provided is it interpreted as the upper limit. Python allows for some shorthand calls for this: for i in range(10):ĭoes the same thing. i will have the value 0 and is incremented by the third argument 1 until it is of value 10 and will not reenter the loop which means the last printed value will be 9. This will print the numbers from 0-9, i.e. In english, take each item in your iterable_object, call it x, and perform some code involving x.įor range objects: for i in range(0,10,1): The correct syntax for a for loop iteration is: for x in iterable_object: I am reading Think Like a CS with Python as additional help with the class as well as Learn Python the Hard Way. We haven't gone over Class and Objects and did see how to do that, and we also haven't gone over numpy and tried using import numpy and was having some problems with that as I'm new to numpy as well. I researched the following helpful links for For In Range but was getting stumped. I was just showing my progress so far and was trying to get this program to work better. I don't believe I need the boolean flag or index part at the beginning of my coding for a For In range loop. It might be a Nested Loop and those are a bit more tricky. I was wondering how to do this with a For In Range Loop. Print 'That name was not found in the list.' Print 'That name was found in element ' + str(index + 1) While found = False and index < len(names): # Step through the list searching for the SearchValue = raw_input('Enter a name to search for in the list: ') I was having difficulty understand pass by values and references I think as well. I'm new to python and programming and have seen in the book how they made this with a While Loop with the false and index. #I want to print the Matrix value from the user input If userFirstName = names_matrix and userLastName = names_matrix : UserLastName = raw_input('What is the user last name?') UserFirstName = raw_input('What is the user first name?') #Create a variable to use as a loop counter.įor in names_matrix: #Having problems here with what goes here in For In Loop ['Charlotte', 'Monroe', 'Broken Pine', 'Hogwart', 'Spot in Road', ['Zdolfalos', 'Johnson', 'Terrell', 'Wilson', 'Key', 'Smith', Person if found if not found, print 'User Not Found!' In the matrix, then print the corresponding information (entire row) for that Then ask the user to enter both the first name and last name of one of the users Here are the Array's Programming requirements from the user and comparing the value to the arrays value: In Python I am developing a Matrices or 2D Array. I understand the communities efforts to curb the homework questions, but I am stumped and would like to learn this concept and move on to learn more programming. So I have been searching on Overflow for a few days now for a problem that I am working on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |