Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 29th, 2013, 02:45 PM
 marqudiego
Registered User

Join Date: Jun 2013
Posts: 1
Time spent in forums: 13 m 36 sec
Reputation 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
June 29th, 2013, 03:18 PM
 zxq9
Contributing User

Join Date: May 2013
Location: Usually Japan when not on contract
Posts: 240
Time spent in forums: 2 Days 11 h 54 m
Reputation Power: 11
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.

#3
July 4th, 2013, 11:52 AM
 leonnaley2
Contributing User

Join Date: Jan 2013
Posts: 67
Time spent in forums: 23 h 24 m 55 sec
Reputation Power: 1
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)

#4
July 4th, 2013, 02:07 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 574
Time spent in forums: 5 Days 4 h 8 m
Reputation Power: 63
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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to manipulate strings