![]() |
about cmp fun - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: about cmp fun (/thread-7474.html) Pages:
1
2
|
about cmp fun - Sandesh - Jan-12-2018 why cmp() fun wll not work in python 3? RE: about cmp fun - buran - Jan-12-2018 from Guido: What’s New In Python 3.0, see the last bullet in Ordering Comparisons section Quote:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).) RE: about Dictionary - Sandesh - Jan-12-2018 dict = {'Name': 'Zara', 'Age': 7} print ("Value :" , dict.items()) if i run the above code i supposed to get= Value :([('Name', 'Zara'), ('Age', 7)]). But i am getting it as=Value : dict_items([('Name', 'Zara'), ('Age', 7)]).. Help me out... RE: about Dictionary - hshivaraj - Jan-12-2018 (Jan-12-2018, 10:29 AM)Sandesh Wrote: dict = {'Name': 'Zara', 'Age': 7} Firstly please use bbcode to wrap you code around. You have better chances of getting help, than one without it. Quote:dict = {'Name': 'Zara', 'Age': 7} print ("Value :" , dict.items())if i run the above code i supposed to get= And I suspect that your getting this output Value : dict_items([('Name', 'Zara'), ('Age', 7)]) . Is because how the __repr__ magic method had been implemented for dict class in python3. In Python2 the output is what you expect. However this has changed in python>>> dict = {'Name': 'Zara', 'Age': 7} >>> dict {'Age': 7, 'Name': 'Zara'} >>> dict.items() [('Age', 7), ('Name', 'Zara')] RE: about cmp fun - buran - Jan-12-2018 in python3 dict.items() method returns Dict View objects. In python2 same method will return list of 2-element tuples.So, you need to pass the dict view object to list() function in order to convert it to list (what you expect) >>> d = {'a':1, 'b':2} >>> d.items() dict_items([('b', 2), ('a', 1)]) >>> list(d.items()) [('b', 2), ('a', 1)]However there is much serious problem in your code. Never use built-in functions as names, i.e. your dict name overwrite the built-in dict function: >>> my_dict = dict((('a',1), ('b',2))) >>> my_dict {'b': 2, 'a': 1} >>> dict = {'Name': 'Zara', 'Age': 7} >>> my_dict = dict((('a',1), ('b',2))) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'dict' object is not callable RE: about cmp fun - Sandesh - Jan-23-2018 fo = open("foo.txt", "rw+") print ("Name of the file: ", fo.name) line = fo.readlines() print ("Read Line: %s" % (line)) # Again set the pointer to the beginning fo.seek(0, 0) line = fo.readline() print ("Read Line: %s" % (line)) # Close opened file fo.close() If i run the above code I should get result as Name of the file: foo.txt Read Line: ['This is 1st line\n', 'This is 2nd line\n', 'This is 3rd line\n', 'This is 4th line\n', 'This is 5th line'] Read Line: This is 1st line But i am getting something like Name of the file: foo.txt Read Line: [] Read Line: Even though my txt file containing info....???? RE: about cmp fun - buran - Jan-23-2018 actually, you are getting
RE: about cmp fun - snippsat - Jan-23-2018 (Jan-23-2018, 06:22 AM)Sandesh Wrote: Even though my txt file containing info....????You are opening file for read and write,open for read 'r' which is the default mode.Here a better way with with open and new string formatting f-string .with open('in.txt') as f: contend = [i.strip() for i in f] print(f'Name of file: {f.name}') print(f'First line: {contend[0]}') print(f'Read lines: {contend}')
RE: about cmp fun - Sandesh - Jan-26-2018 class Employee: def _init_(self,first,last,pay): self.first=first self.last=last self.pay=pay self.number=number emp1=Employee('corey','schafer',50000) emp1=Employee('test','user',60000) print(emp1.first) print(emp1.last) print(emp1.pay)when i try to run the above code it shows type Error saying that object() takes no parameter....!!!!!????? RE: about cmp fun - snippsat - Jan-26-2018 Two underscore __init__ .Indentation is 4-space not tree. class Employee: def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay #self.number = number # Not used emp1 = Employee('test', 'user', 60000) print(emp1.first) print(emp1.last) print(emp1.pay)
|