473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

converting to an integer

I think the elements in my list must not be being read as integers because when I do something like
Expand|Select|Wrap|Line Numbers
  1. >>> for i in stuff:
  2. ...     if i > 10:
  3. ...         print i
  4. ...         
  5. 45.53
  6. 6.41
  7. 21.76
  8. 12.19
  9. 1.47
  10.  
So I tried this:
Expand|Select|Wrap|Line Numbers
  1. >>> newints = int(stuff)
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in <module>
  4. TypeError: int() argument must be a string or a number, not 'list'
  5.  

So if I need to convert the elements to integers...how could I do that?

Same question if I already had thigns in a dictionary, say

dd = {'damon': '5', 'cano' : '8', 'abreu' : '23', 'arod' : '49'}

thanks
Aug 15 '07 #1
2 1189
bartonc
6,596 Expert 4TB
I think the elements in my list must not be being read as integers because when I do something like
Expand|Select|Wrap|Line Numbers
  1. >>> for i in stuff:
  2. ...     if i > 10:
  3. ...         print i
  4. ...         
  5. 45.53
  6. 6.41
  7. 21.76
  8. 12.19
  9. 1.47
  10.  
So I tried this:
Expand|Select|Wrap|Line Numbers
  1. >>> newints = int(stuff)
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in <module>
  4. TypeError: int() argument must be a string or a number, not 'list'
  5.  

So if I need to convert the elements to integers...how could I do that?

Same question if I already had thigns in a dictionary, say

dd = {'damon': '5', 'cano' : '8', 'abreu' : '23', 'arod' : '49'}

thanks
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'damon': '5', 'cano' : '8', 'abreu' : '23', 'arod' : '49'}
  2. >>> for key, value in dd.items():
  3. ...     dd[key] = int(value)
  4. ...     
  5. >>> dd
  6. {'damon': 5, 'cano': 8, 'abreu': 23, 'arod': 49}
  7. >>> 
Aug 15 '07 #2
bvdet
2,851 Expert Mod 2GB
For a list:
Expand|Select|Wrap|Line Numbers
  1. >>> alist = [12.1, 25.6, 33.9]
  2. >>> alist = [int(r) for r in alist]
  3. >>> alist
  4. [12, 25, 33]
  5. >>> 
Aug 16 '07 #3

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.