Computer Science, asked by samomi, 3 months ago

Write a Python program to find the frequency of elements in a list using a

dictionary.

E.g.: For the list

['apple','banana','apple','grapes','banana','guava']

the output should be

{'apple': 2, 'banana': 2, 'grapes': 1, 'guava': 1}

Answers

Answered by rashmisethi337
0

Answer:

The Python List is one of the most useful sequences in real-time. A Python list is a sequence of multiple values in an ordered sequence. Unlike Strings, Python List allows us to store different types of data such as integer, float, string, etc.

The following are examples of declaring a Python list of different data types.

Declare Python Lists

There are several ways to create a List in Python. The most straightforward way is to place the required list items within a square bracket [ ]

A list that contains no values or elements is an empty list. By placing an empty square bracket create an empty list.

List_Name = []

The first statement is an integer list of five integer values. The second statement is a string list that contains three String values or three words.

Integer_List = [1, 2, 3, 4, 5]

String_List = ['apple', 'Orange', 'Grape', 'Mango']

Python lists allow placing different data types in a single list. The following one is an example of a mixed list, which contains one integer, float, and two integer values.

Mixed_List = ['apple', 2, 3.50, 'Mango']

How to access Python List items?

Lists sequentially store data (ordered list). So, we can access the list elements with the help of indexes. Moreover, using indexes, we can access or alter/change each item present in the Lists separately. The syntax to access List items is

List_Name([Index_Number])

Index value starts at 0 and ends at n-1, where n is the list size. For example, if a list stores 5 elements, the index starts at 0 and ends with 4. To access or alter the first list value, use list_name[0], and to access the fifth list item, use list_name[4]. Let’s see the list example for better understanding:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Positive Indexing

print(x[0])

print(x[4])

print(x[8])

print('=======\n')

# Negative Indexing

print(x[-2])

print(x[-4])

print(x[-8])

Please use Negative numbers as the list index to access list items from right to left. It means access list items in reverse order.

# How to Access List Items

x = ['apple', 'Mango', 'banana', 'orange', 'cherry','kiwi']

# List Items using Positive Index

print("List Item at Index Position 0 = ", x[0])

print("List Item at Index Position 2 = ", x[2])

print("List Item at Index Position 4 = ", x[4])

print("List Item at Index Position 3 = ", x[3])

print('=======\n')

# List Items using Negative Index

print("List Item at Index Position -1 = ", x[-1])

print("List Item at Index Position -3 = ", x[-3])

print("List Item at Index Position -5 = ", x[-5])

print("List Item at Index Position -6 = ", x[-6])

Answered by vishuagarwaldec
0

Answer:

Write a Python program to create a list of strings by taking input from the user and then create  a dictionary containing each string along with their frequencies. (e.g. if the list is [‘apple’,  ‘banana’, ‘fig’, ‘apple’, ‘fig’, ‘banana’, ‘grapes’, ‘fig’, ‘grapes’, ‘apple’] then output should be  {'apple': 3, 'banana': 2, 'fig': 3, 'grapes': 2}.  

lst = []

d = dict()

print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")

while True:

   user = input('enter string element :: -- ')

   if user == "0":

       break

   else:

       lst.append(user)

print("LIST ELEMENR ARE :: ",lst)

l = len(lst)

for i in range(l) :

   c = 0

   for j in range(l) :

       if lst[i] == lst[j ]:

           c += 1

   d[lst[i]] = c

print("dictionary is  :: ",d)

Explanation:

Similar questions