Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:45+00:00 2026-05-16T17:25:45+00:00

I’m interested in not having to write map the int function to the tuple

  • 0

I’m interested in not having to write map the int function to the tuple of strings where I currently have it. See the last part of my example:

import os
import csv

filepath =  os.path.normpath("c:/temp/test.csv")

individualFile = open(filepath,'rb')
dialect = csv.Sniffer().sniff(individualFile.read(1000))

individualFile.seek(0)
reader = csv.reader(individualFile,dialect)

names = reader.next()

print names

def buildTree(arityList):
    if arityList == []:
        return 0            
    else:
        tree = {}
        for i in xrange(arityList[0][0],arityList[0][1]+1):
            tree[i] = buildTree(arityList[1:])
        return tree


census = buildTree([(1,12),(1,6),(1,4),(1,2),(0,85),(0,14)])

for m, f, s, g, a, c, t in reader:
    try:
        m,f,s,g,a,c,t = map(int,(m,f,s,g,a,c,t))
        census[m][f][s][g][a][c] += t
    except:
        print "error"
        print m, f, s, g, a, c, t
        break   

What I want to do is something like this:

for m, f, s, g, a, c, t in map(int,reader):
    try:
        census[m][f][s][g][a][c] += t
    except:
        print "error"
        print m, f, s, g, a, c, t
        break    

I try this and I get the following error:

TypeError: int() argument must be a string or a number, not 'list'

I’m having trouble understand this error message. I thought reader was an iterable object – not a list. It returns a list for each iteration, but it itself is not a list, correct? I guess that is more of a side question. What I really want to know is if there is a way to do what I am trying to do. Sorry for the code that doesn’t really relate, but I thought I would include my whole example. Feel free to tear it to bits! 🙂 I’m wondering if it might be better to just have one dict where the key is a tuple instead of this nested dictionary stuff, but even so, I’m still interested in figuring out my question.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:25:45+00:00Added an answer on May 16, 2026 at 5:25 pm

    what you want is something like:

    def int_wrapper(reader):
        for v in reader:
            yield map(int, v)
    

    Your code would then look like:

    reader = csv.reader(individualFile,dialect)
    reader = int_wrapper(reader)
    
    # all that other stuff
    
    for m, f, s, g, a, c, t in reader:
        try:
            census[m][f][s][g][a][c] += t
        except:
            print "error"
            print m, f, s, g, a, c, t
            break    
    

    This is just using a generator function to wrap the reader and convert the input to integers.

    The origin of the TypeError is that reader is a generator function which yields lists of values. When you apply map to it, you are applying map to a ‘list’ of lists. This is different than applying map to a list of values which you do when you write it out the long way.

    For illustration, another way to do it is

    for m, f, s, g, a, c, t in (map(int, v) for v in reader):
        # code
    

    This just uses an in situ generator expression instead of defining a function. It’s a matter of taste.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.