[Tutor] how to compare elements of 2 lists
"sith ." wrote: > How can I determine if the elements in a are larger or smaller than > the elements in b. > .. > like 2 columns in excel, the third column would be > a new list of boolean values. Can someone help please? Thank you. Hi, in that case, you need to create the third column (list). Instead of values, iterate over indices and use those to fetch the values, ie: c = [] for i in range(min(len(a), len(b))): c.append(a[i] > b[i]) or using list comprehension: c = [a[i] > b[i] for i in range(min(len(a), len(b)))] tezlo I finally get it. Thank you so much. p.s. Thanks Alan and Ricardo. - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to compare elements of 2 lists
sith . wrote: > Hi, > I've read the posts on comparing 2 lists and couldn't find the answer to > my question. > I have 2 lists > a = [4,3,2,6,7,9] > b = [8,6,3,3,2,7] > How can I determine if the elements in a are larger or smaller than the > elements in b. > > for i in a: > for u in b: > i > u > does not return the result I seek. > > In this example, 4 from a is compared to 8,6,3,3,2,7 then 3 from a is > compared to all the elements in b. > I'd like > 4 to 8, > 3 to 6, > 2 to 3 and so on; like 2 columns in excel, the third column would be a > new list of boolean values. > Can someone help please? Thank you. > all(all(i>j for j in b) for i in a) HTH Hi, Thanks for your reply. This is what I get: >>> a = [4,3,2,6,7,9] >>> b = [8,6,3,3,2,7] >>> all(all(i>j for j in b) for i in a) >>> all(all(i>j for j in b) for i in a) False How do I make it loop through the whole list? - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to compare elements of 2 lists
Hi, I've read the posts on comparing 2 lists and couldn't find the answer to my question. I have 2 lists a = [4,3,2,6,7,9] b = [8,6,3,3,2,7] How can I determine if the elements in a are larger or smaller than the elements in b. for i in a: for u in b: i > u does not return the result I seek. In this example, 4 from a is compared to 8,6,3,3,2,7 then 3 from a is compared to all the elements in b. I'd like 4 to 8, 3 to 6, 2 to 3 and so on; like 2 columns in excel, the third column would be a new list of boolean values. Can someone help please? Thank you. - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] parsing an array
greater !! greater !! smaller only the second and third "greater" and "smaller" output are correct as 1.5 is larger than 1 and 0 is smaller than 1.5. What is i[1] greater than?
[Tutor] parsing an array
a = [[1,1],[3,1.5],[5,0]] for i in range(len(a)) : if a[i][1] > a[i-1][1] : print 'greater !!' else: print "smaller" greater !! greater !! smaller Thanks for taking the time to help me Aditya. I tried the code but have encountered a problem. In this new list, [1,1] [3,1.5] [5,0] only the second and third "greater" and "smaller" output are correct as 1.5 is larger than 1 and 0 is smaller than 1.5. What is i[1] greater than? I also tried using + for next item instread, but that produced an error. Why? if a[i][1] > a[i+1][1] - Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] parsing an array
a = [[1,2],[3,1.5],[5,6]] for i in a: print i if i[1]>i[0]: print "second index is larger" else: print "second index is smaller" [1, 2] second index is larger [3, 1.5] second index is small er [5, 6] second index is larger What I'd like do is compare if 1.5 in i[1] is greater than 2 in i[0]; for time series, t(1)>t(0) and interate through the entire list - is 6 in i[2]>than 1.5 in i[1] etc? Since the data is in columns in a text file or csv, I can't put 1.5 in the same sublist as 2, and 6 in the same sublist as 1.5 to make things easier. What can I do? Thank you for your help. - Get easy, one-click access to your favorites. Make Yahoo! your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] parsing an array
a = [[0,1,2,3,4,5],[1,2,3,4,5,6]] You cannot modify the same array when you are looping through it. You have to loop through the copy of the contents :- a[:]. # Untested code for i in a[:]: # You are looping through the copy of contents # I'm new I'm going to try and explain my understanding of this code; pls correct if wrong # i[0] is [0,1,2,3,4,5] and i[1] is the other # the loop first goes to the list on the left then goes to the list on the right for j in i: #each element in the sublist is now evaluated # first 0 in i[0] then 1 in i[1] then back to 1 in i[0] and then to 2 in i[1]- is this right? # implement your logic with j if j < i[0]: # or any dynamic conditional check. the first time this loops, 0 in the first list or i[0] is evaulated against itself what is the next j value for the next loop? a[i][j] = j <- don't understand this bit Does this help you? I've looked on the net as well as my book (python dummies), but can't find an explantion for nested for loops in nested lists. Would it be possible to explain? Thank you. - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] parsing an array
newfile = open('y.txt') >>> for line in newfile: ... print line.rstrip() 3 5 7 11 8 10 Hi, I'm trying to parse an array. Now that I can open and read lines in my array, how can I access individual elements of my multidimensional array? I'd like to loop through the array and compare values in each line. If w is the array, if w[0][1]http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with setting path
Thank you very much Alan. Kevin __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with setting path
Hi, Thanks for your input. I'm presently downloading enthought python - no asian mirror, so it'll take a really long time to download even on broadband at 10kbps) - but I don't think it's a distro problem. I'm probably not doing it right as I'm now using IDLE and still have the same problem. For example, here is the traceback for a simple file I made and tried to run in IDLE: # test.py x = 1 + 1 print x In IDLE, I tried to run it by typing test.py Traceback (most recent call last): File "", line 1, in test.py NameError: name 'test' is not defined However, when I open the file in IDLE using the menu, and then run it also from the menu, it works. Am I not calling the program properly? p.s. Wonder why word wrap didn't work on my first post. Thanks to all who scrolled. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with setting path
hi, I'm a beginner. Can someone help me with the installation? I've read several tutorials, possess a couple of books, and have followed the instructions, but I stil face a problem. None of the online tutorials address this issue in depth maybe because it could be too simple. I'm having problems setting my path and having the python interpreter recognize my modules in my python folders. My python path browser is showing that the main python directory is listed, along with my subdirectories like lib, and work. I've set python PATH in windows too. However, when I try to run the program, which is stored in pthon25/work using IDLE, I get a traceback error. I can run the program in dos though and it also seems to work when I open the file using the menu, and then select the run button in pythonwin. I'd greatly appreciate it if someone could guide me with a proper setup of path. Thanks. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor