Getting the number of "lists" returned by split() in python -


so in order different task wanted number of lists returned split() function file of commands. file, command.txt has below entries:

ps -a free 

and code came convert list , number of list is:

with open('command.txt', 'r') file:      #for i, v in enumerate(file): #i can line counts.      #    pass      #print i+1       line in file:          word = line.split()          print word          print len(word) 

the output of code is:

['ps', '-a']

2

['free']

1

rather want output 2. since word has 2 lists, ['ps', '-a'] , ['free']. please suggest how can modify or come appropriate code.

as stated in comments above, sounds asking line count. since you're looping through file, add counter:

n = 0 open('command.txt', 'r') file:     line in file:         word = line.split()         print word         n += 1 print n 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -