Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13

    Tryign to add a valkue to a set


    Code:
    names = ()
    # populate names, months, years
    names.add( '----------' )
    years = ( 2010, 2011, 2012, 2013 )

    [Sun Jun 09 08:53:18 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 286, in <module>, referer: http://superhost.gr/
    [Sun Jun 09 08:53:18 2013] [error] [client 79.103.41.173] names.add( '----------' ), referer: http://superhost.gr/
    [Sun Jun 09 08:53:18 2013] [error] [client 79.103.41.173] AttributeError: 'tuple' object has no attribute 'add', referer: http://superhost.gr/


    Why? all i tried to do is to add a value to 'names' set.
    What is now proved was once only imagined!
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    names = set() i guess for python 3.3.2
    What is now proved was once only imagined!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Re:


    tuple (name=()) is a special kind of immutable collection, it means you can not to add, delete or change any item.
    use list instead of tupleif you add items:
    name=[]
    name.append('--------------')
  6. #4
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    I tries the list to to but is says:

    AttributeError: 'list' object has no attribute 'add', referer: http://superhost.gr/

    the only way for this tio run properly is by names = set(), in python 3.3.2 iam using
    What is now proved was once only imagined!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    re :


    Call name. append(), not add()
  10. #6
  11. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    append also faild it says there is not attribute sappend
    What is now proved was once only imagined!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    164
    Rep Power
    19
    The reason you can add an object to a set or a list is because they are mutable objects.

    Tuple is an immutable object. And hence you don't have methods that will mutate a tuple object.

    Set:
    Code:
    >>> a={'hello', 'world'}
    >>> type(a)
    <class 'set'>
    >>> a.add('hi')
    >>> a
    {'world', 'hi', 'hello'}
    >>>
    Tuple:
    Code:
    >>> a=(1,2,3)
    >>> type(a)
    <class 'tuple'>
    >>> a = a + (4,5,6)
    >>> a
    (1, 2, 3, 4, 5, 6)
    >>>
  14. #8
  15. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    so the error was that iu was trying

    names = ()

    instead of

    names = {}

    The way that worked for me was names = set()

    is this worng?

    what syntax should i be using?
    What is now proved was once only imagined!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    If what you actually want is an empty set, then:
    Code:
    var = set()
    is the correct way.

    Code:
    var = () #declares an empty tuple
    var = {} #declares an empty dictionary
    var = []  #declares an empty list
    It gets confusing because:
    Code:
    var = {1,2,3,4}
    creates a set, but:
    Code:
    var = {"a" : 1, "b" : 2}
    creates a dictionary.

    add works with sets and append works with lists. Be sure a set is actually what you want to work with. Sets are unordered and will not contain duplicates. Lists are ordered and can contain as many duplicates as you like. They are different tools for different jobs.

    -Mek
  18. #10
  19. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    names = set()
    for name in sorted( names ):

    can order a set

    iam having trouble ordering a dictionaty though, al though i use the following it deosnt get ordered.

    Code:
    months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
    	   'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
    
    for key in sorted( months.keys() ):
    How i should order the keys of my dictionary?
    What is now proved was once only imagined!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    sets and dicts are unordered. If you just want to print out an ordered list of keys/values then sure you can do that (although I'm not sure you will get the results you want in Greek), but the data type itself is not ordered.

    If you really want an ordered dict you could use this, but I somehow doubt it is what you actually need in this situation.
    OrderedDict

    -Mek
  22. #12
  23. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    But how come i was able to sort the set names() and not beine able to sort the dictionary keys as well with the sorted function?

    for key in sorted( months.keys() ):
    What is now proved was once only imagined!
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Because you are sorting it alphabetically? And not by the value number as you probably want?

    python Code:
    months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
    	   'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
     
    for item in sorted(months.items(),key=lambda num : num[1]):
        print(item[0])
    Code:
    >>> 
    Ιανουάριος
    Φεβρουάριος
    Μάρτιος
    Απρίλιος
    Μάϊος
    Ιούνιος
    Ιούλιος
    Αύγουστος
    Σεπτέμβριος
    Οκτώβριος
    Νοέμβριος
    Δεκέμβριος
    >>>
    No clue,
    -Mek
  26. #14
  27. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    What if i wanted to srt if alphabetically and not by the values?

    Also why use

    for item in sorted(months.items(),key=lambda num : num[1]):

    and not

    for item in sorted(months.items()):
    What is now proved was once only imagined!
  28. #15
  29. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    Trying this:
    Code:
    months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
    	   'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
    
    for key in sorted( months.values() ):
    	print('''
    		<option value="%s"> %s </option>
    	''' % (months[key], key) )
    output this:

    Code:
    [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]   File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>, referer: http://superhost.gr/
    [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]     ''' % (months[key], key) ), referer: http://superhost.gr/
    [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: http://superhost.gr/
    KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.
    Last edited by Nik; June 10th, 2013 at 04:30 AM.
    What is now proved was once only imagined!
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo