ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. I am trying to learn python. I have python 2.7 installed in my laptop with ubuntu 16.10 as operating system. I wrote a python script (just for the sake of learning) which is attached here. It reads a datafile (attached here) and prints a list. In the code there is a function chunk(1, n). Now what I knew was this function creates n-sized chunks and if we list it each of the chunk must contains 5 elements.
Now please look into the following code (its name is len.py, which is attached here with name len.py.txt).
Code:
#!/usr/bin/python
import os, sys, math
from astropy.io import ascii
import multiprocessing as mp
numProcess = 20
list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
print "Length of list1 = ", len(list1)
print "Length of list2 = ", len(list2)
for i in range(len(list1)):
print i, list1[i]
for var in list(range(len(list1))):
print var
q = raw_input("Please enter the input file name with its full path:\n")
print q
table1 = ascii.read(q, names=('Cand_no' ,'Freq', 'Period(ms)', 'SNR', 'DM', 'Filterbank_file', 'Location'))
print len(table1)
def chunk(List, n):
if len(List) < n:
#each list has only one element
output = []
for i in range(len(List)):
output.append([])
for i in range(len(List)):
output[i].append(List[i])
return output
if len(List) > n:
output=[]
for i in range(n):
output.append([])
for i in range(len(List)):
output[i%n].append(List[i])
return output
subTable = list(chunk(range(len(table1)), int(numProcess)))
for var in list(chunk(range(len(table1)), int(numProcess))):
print "subTable", var
But here if I put numProcess = 20, then I am getting an error
Quote:
Length of list1 = 3
Length of list2 = 2
0 123
1 xyz
2 zara
0
1
2
Please enter the input file name with its full path:
/home/biplabbijay/Desktop/script_test/dataset.fold
/home/biplabbijay/Desktop/script_test/dataset.fold
20
Traceback (most recent call last):
File "len.py", line 41, in <module>
subTable = list(chunk(range(len(table1)), int(numProcess)))
TypeError: 'NoneType' object is not iterable
Here is my data file "dataset.fold". I am also attaching it with .txt extension.
I would suggest testing the variables you are passing in to your recursive function as at some point you are passing in something of the wrong type (as per error message)
IMHO the greatest tool at the developers finger tips is the print / output to display option we all learn in the original hello world script you first write
I have done that too. I found that if len(table1) = numProcess, thios error comes out. I can bypass it. But I want to know why exactly it is happening. Please help me in this regard.
Well I have to say, after some investigation, for someone who is 'learning' python you have jumped in a deep end by using a non-standard library (astropy).
So this limits me as I am not prepared to install a library I don't need.
So here are some things to look at:
1. Confirm you are only running python 2.7 and not 3.X as you have only listed python as the interpreter which normally defaults to the higher version.
2. The error advises that one of the objects you are calling is of type None, hence you need to work out which that is?
3. Is the error actually on the first call to the function or is it inside the call?
Points to note (and can be taken with a grain of salt):
1. Try and have the code flow better by either having method definitions before or after all the individual code
2. (as not a python guru) I think you need to look over all your for loops as I am pretty sure you shouldn't need constructs like the following:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.