Write a program in Python to generate five random even index lowercase alphabets in a series

PythonServer Side ProgrammingProgramming

Solution 1

  • Define lowercase alphabets in a list.

  • Create for loop and find the even index elements and add it to another list. It is defined below

   ls = []
   for i in l:
      if(l.index(i)%2==0):
         ls.append(i)
  • Finally, apply random sample 5 values to the list and generate a series.

Example

import pandas as pd
import string
import random as r
chars = string.ascii_lowercase
print("lowercase alphabets are:-",chars)
chars_list = list(chars)
data = r.sample(chars_list[::2],5)
print("random even index char's are:-",data)
result = pd.Series(data)
print("Series:\n",result)

Output

lowercase alphabets are:- abcdefghijklmnopqrstuvwxyz
random even index char's are:- ['w', 'k', 'i', 'u', 'm']
Series:
0    w
1    k
2    i
3    u
4    m
dtype: object

Solution 2

Example

import string
import pandas as pd
import random as r
l = list(string.ascii_lowercase)
ls = []
for i in l:
   if(l.index(i)%2==0):
      ls.append(i)
data = pd.Series(r.sample(ls,5))
print(“Series:\n”, data)

Output

Series:
0    o
1    i
2    q
3    c
4    a
dtype: object
raja
Published on 10-Feb-2021 12:53:10
Advertisements