Python | Bigram formation from given list


When we are dealing with text classification, sometimes we need to do certain kind of natural language processing and hence sometimes require to form bigrams of words for processing. In case of absence of appropriate library, its difficult and having to do the same is always quite useful. Let’s discuss certain ways in which this can be achieved.

Method #1 : Using list comprehension + enumerate() + split()
The combination of above three functions can be used to achieve this particular task. The enumerate function performs the possible iteration, split function is used to make pairs and list comprehension is used to combine the logic.

   

test_list = ['geeksforgeeks is best', 'I love it']

  

print ("The original list is : " + str(test_list))

  

res = [(x, i.split()[j + 1]) for i in test_list 

       for j, x in enumerate(i.split()) if j < len(i.split()) - 1]

  

print ("The formed bigrams are : " + str(res))

Output :

The original list is : [‘geeksforgeeks is best’, ‘I love it’]
The formed bigrams are : [(‘geeksforgeeks’, ‘is’), (‘is’, ‘best’), (‘I’, ‘love’), (‘love’, ‘it’)]

 
Method #2 : Using zip() + split() + list comprehension
The task that enumerate performed in the above method can also be performed by the zip function by using the iterator and hence in a faster way. Let’s discuss certain ways in which this can be done.

   

test_list = ['geeksforgeeks is best', 'I love it']

  

print ("The original list is : " + str(test_list))

  

res = [i for j in test_list 

       for i in zip(j.split(" ")[:-1], j.split(" ")[1:])]

  

print ("The formed bigrams are : " + str(res))

Output :

The original list is : [‘geeksforgeeks is best’, ‘I love it’]
The formed bigrams are : [(‘geeksforgeeks’, ‘is’), (‘is’, ‘best’), (‘I’, ‘love’), (‘love’, ‘it’)]




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :



Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.








Source link

Python | Convert case of elements in a list of strings


Given a list of strings, write a Python program to convert all string from lowercase/uppercase to uppercase/lowercase.

Input : ['GeEk', 'FOR', 'gEEKS']
Output: ['geeks', 'for', 'geeks']

Input : ['fun', 'Foo', 'BaR']
Output: ['FUN', 'FOO', 'BAR']

 
Method #1 : Convert Uppercase to Lowercase using map function

  

out = map(lambda x:x.lower(), ['GeEk', 'FOR', 'gEEKS'])

  

output = list(out)

  

print(output)

Output:


['geek', 'for', 'geeks']

 
Method #2: Convert Lowercase to Uppercase using List comprehension

  

input = ['fun', 'Foo', 'BaR']

  

lst = [x.upper() for x in input]

  

print(lst)

Output:


['FUN', 'FOO', 'BAR']




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :



Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.











Source link

Python | Convert a list of multiple integers into a single integer


Given a list of integers, write a Python program to convert the given list into a single integer.

Examples:

There are multiple approaches possible to convert the given list into a single integer. Let’s see each one by one.

Approach #1 : Naive Method
Simply iterate each element in the list and print them without space in between.

  

lst = [12, 15, 17]

  

for i in lst:

    print(i, end="")

Output:

 
Approach #2 : Using join()

Use the join() method of Python. First convert the list of integer into a list of strings( as join() works with strings only). Then, simply join them using join() method. It takes a time complexity of O(n).

def convert(list):

      

    

    s = [str(i) for i in list]

      

    

    res = int("".join(s))

      

    return(res)

  

list = [1, 2, 3]

print(convert(list))

Output:

 
Approach #3 : Using map()

Another approach to convert a list of multiple integers into a single integer is to use map() function of Python with str function to convert the Integer list to string list. After this, join them on the empty string and then cast back to integer.

def convert(list):

      

    

    

    res = int("".join(map(str, list)))

      

    return res

  

list = [1, 2, 3]

print(convert(list))

Output:

 
Approach #4 : Multiplying by corresponding power of 10

A more mathematical way, which does not require to convert the integer list to string list is, to multiply each integer element with its corresponding power of 10, and then summing it up. It takes a time complexity of O(n).

def convert(list):

      

    

    

  

    res = sum(d * 10**i for i, d in enumerate(list[::-1]))

      

    return(res)

  

list = [1, 2, 3]

print(convert(list))

Output:

A small variation to this program leads to less computation in calculation of sum, i.e. using reduce(). This makes use of Horner’s rule, which factors the polynomial representing the number to reduce the number of multiplications.

res = functools.reduce(lambda total, d: 10 * total + d, list, 0)




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :



Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.








Source link