473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

converting to an integer

54 New Member
hey everyone, i'm having a mental block can anyone help me out

if i have a list like this
num = ['1;3;2;3;2;1;4', '\n', '30;2;13;2;1;4;2', '2;3;2;1;4;2;4', '3;2;1;4;2;4;1', '2;1;4;2;4;1;4', '1;4;2;4;1;4;2']

how might i get python to recognize each elemant as an integer so that i can sum each list

that is have it print
16
\n
54
18

etc

thanks
Feb 19 '08 #1
4 940
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. >>> num = ['1;3;2;3;2;1;4', '\n', '30;2;13;2;1;4;2', '2;3;2;1;4;2;4', '3;2;1;4;2;4;1', '2;1;4;2;4;1;4', '1;4;2;4;1;4;2']
  2. >>> s = '\n'.join([str(result) for result in [sum([int(i) for i in item.split(';')]) for item in num if item != '\n']])
  3. >>> s
  4. '16\n54\n18\n17\n18\n18'
  5. >>> print s
  6. 16
  7. 54
  8. 18
  9. 17
  10. 18
  11. 18
  12. >>> 
Feb 19 '08 #2
Patrick C
54 New Member
that works well, but i'm trying to keep a blank line if the '\n' was there to begin with, how can i tweak that so the solution would have been

'16\n\n54\n18\n17\n18\n18'

thanks
Feb 19 '08 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. results = []
  2. for item in num:
  3.     if item == '\n':
  4.         results.append('')
  5.     else:
  6.         results.append(str(sum([int(i) for i in item.split(';')])))
  7. print '\n'.join(results)
Expand|Select|Wrap|Line Numbers
  1. >>> 16
  2.  
  3. 54
  4. 18
  5. 17
  6. 18
  7. 18
  8. >>> print repr('\n'.join(results))
  9. '16\n\n54\n18\n17\n18\n18'
  10. >>> 
I got carried away with the list comprehension in my previous post. s could be:
Expand|Select|Wrap|Line Numbers
  1. s = '\n'.join([str(sum([int(i) for i in item.split(';')])) for item in num if item != '\n'])
Feb 19 '08 #4
Patrick C
54 New Member
hey thanks for breaking it up onto many lines, i'm so fresh at this that when it's all squeezed into one line my mind breaks
Feb 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
0
by: Mark Allen | last post by:
Hello, I am creating an RTF document server side for a report. However I am having problems converting images into the required RTF format. I am converting the image into a string (binary)...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
7
by: tragic54 | last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
0
by: ling2000 | last post by:
Hello all, I'm trying to upgrade a VB6 project into VB.net, and the problem I had is in converting 'address of' to 'delegate'. I had the error "Value of type 'DelegateIDccManSink_OnLogIpAddr'...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.