Problem statement: If you have a list, how do you count the

  • Thread starter jumbogala
  • Start date
  • Tags
    Count List
In summary, to count the number of characters in a list, you can use the map function to get a list of lengths, then use the sum function to add them up and return the total number of characters. Another option is to concatenate the list into a single string and then use the len function to get the length of the string.
  • #1
jumbogala
423
4
Problem statement:If you have a list, how do you count the

Problem statement:
If you have a list, how do you count the number of characters in that list?

For example, if the list is list=['happy', 'sad', 'angry'], how do I get the program to tell me there are 13 characters in the list?


Attempt at a solution:

I tried doing for char in list:
print char

But that only printed each item in the list one by one...

I can't think of what else to try. Any ideas?
 
Last edited:
Physics news on Phys.org
  • #2


Recall that len(Obj) returns the length of a list or string.

First use the map function to get a list of lengths...
map(len,List)
returns [5,3,5]

Then (if you have version 2.3 or higher) you can sum a list of addable objects:
so
sum(map(len,List))
should return the sum of the character lengths.

But there's a simpler way. Since character stings "add" by concatenation you can simply build a single string with sum(list) then get its length:
len(sum(List))

I have an old version right now (didn't realize it) so I can't test the sum function. I'm about to update my version and check that this works.
 
  • #3


Oops, I tried sum(List) on python 2.6.1 and it did not like it.

But sum(map(len,List)) works fine.
 

What is a problem statement and why is it important?

A problem statement is a clear and concise description of an issue or challenge that needs to be addressed. It is important because it helps to define the scope of a problem and guides the research process towards finding a solution.

How do you create a problem statement?

To create a problem statement, you should first identify the problem or issue that needs to be addressed. Then, clearly define the problem and its impact. Finally, explain why it is important to find a solution to the problem.

What is the purpose of a problem statement in scientific research?

The purpose of a problem statement in scientific research is to provide a clear and specific description of the problem that the research aims to solve. It helps to focus the research and guide the development of a hypothesis and methodology.

What are the key elements of a problem statement?

A problem statement should include a clear description of the problem, its impact, and the motivation for finding a solution. It should also define the scope and boundaries of the problem, and provide a context for why it is important to address.

How does a problem statement help in finding a solution?

A problem statement helps in finding a solution by providing a clear understanding of the problem and its impact. It also guides the research process towards finding a solution by defining the scope of the problem and providing context for why it is important to address. It can also help to identify potential barriers and limitations that need to be considered when finding a solution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
43
Views
3K
Back
Top