Ask Your Question
2

What is the process for counting sequences in Python?

asked 2023-06-10 10:53:17 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-10 11:05:02 +0000

devzero gravatar image

The process for counting sequences in Python depends on the type of sequence you want to count. Here are some examples:

  1. Counting the number of elements in a list:
my_list = [1, 2, 3, 4, 5]
count = len(my_list)
print(count) # Output: 5
  1. Counting the number of characters in a string:
my_string = "hello world"
count = len(my_string)
print(count) # Output: 11
  1. Counting the number of occurrences of a specific element in a list:
my_list = [1, 2, 3, 4, 5, 2, 2]
count = my_list.count(2)
print(count) # Output: 3
  1. Counting the number of occurrences of a specific substring in a string:
my_string = "hello world"
count = my_string.count("o")
print(count) # Output: 2
  1. Counting the number of unique elements in a list:
my_list = [1, 2, 3, 4, 5, 1, 2, 3]
count = len(set(my_list))
print(count) # Output: 5
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-10 10:53:17 +0000

Seen: 14 times

Last updated: Jun 10 '23