#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    How to manipulate strings


    Hi, I must work with strings and i have this problem:

    I have a file with variables like this.
    a = 3234
    b = 4545
    c = 2343
    d = 7653
    e = 9237
    f = 6545
    g = 5697
    h = 1248

    And I have to put theses variables in rage of values.

    Is there anyone who can help me?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    Go to this link here (link) and read that page first. It will teach you the absolute basics and fix your vocabulary to the point you at least sound like you know what you're asking.

    Then come back and rephrase the question (or edit the original). The only part you might not understand from that page is how to open and close the file -- but that's easy; I'll explain once you rephrase the question.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    when you say string in a range, do you actually mean integers(numbers) in a list?

    To make a list with numbers, just do
    MyList = [123, 456, 001, 002, 45589]

    A list can contain all sorts of data, even other lists
    MyList = [123, "hello", True, 12.3]

    to append data to a list you made earlier, just do:
    Mylist.append("hey")

    to access the data in the list, do:
    MyList[0]
    this will this will come back as 123, because 123 in this case is at index 0(the first thing inside your list)
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Let's assume you want this ...
    Code:
    # data string read from a file
    data_str = '''\
    a = 3234
    b = 4545
    c = 2343
    d = 7653
    e = 9237
    f = 6545
    g = 5697
    h = 1248'''
    
    mydict = {}
    for line in data_str.split('\n'):
        key, val = line.split(' = ')
        mydict[key] = eval(val)
    
    # test printing
    print(mydict)  
    print('-'*70)
    print(sorted(mydict.values()))
    
    ''' my output ...
    {'a': 3234, 'c': 2343, 'b': 4545, 'e': 9237, 'd': 7653, 'g': 5697, 'f': 6545, 'h': 1248}
    ----------------------------------------------------------------------
    [1248, 2343, 3234, 4545, 5697, 6545, 7653, 9237]
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo