• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games
You are here: Home / Archives for Python | Ways to check if element exists in list

Python | Ways to check if element exists in list

Python | Bigram formation from given list

Leave a Comment


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 [email protected]. 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 :


thumb_up
Be the First to upvote.

Please write to us at [email protected] to report any issue with the above content.


Post navigation


Previous

first_page Python | Unique values in Matrix







Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, About Us, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, All Algorithms, All Data Structures, Amazon product price tracker using Python, Analysis of Algorithms, Aptitude, Array, Backtracking, Best Python libraries for Machine Learning, Binary Search (bisect) in Python, Binary Search Tree, Binary Tree, Bit Algorithms, Branch & Bound, C, Campus Ambassador Program, Careers, Check for balanced parentheses in Python, Company Prep, Company-wise, Competitive Programming, Compiler Design, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Constructors in Python, Contact Us, Contests, contribute.geeksforgeeks.org, contributed articles, Core Subjects ►, Courses, CS Subjects, CS Subjects ▼, CS Subjectwise ►, CSS, Data Structures, DBMS, Dealing with Rows and Columns in Pandas DataFrame, Decorators in Python, Decorators with parameters in Python, Design Patterns, Difference between C and Python, Digital Electronics, Divide and Conquer, Dividing a Large file into Separate Modules in C/C++, DS ▼, Engg. Mathematics, Experienced Interviews, Face Detection using Python and OpenCV with webcam, Find K Closest Points to the Origin, Functions in Python, Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE Notes, GATE Official Papers, GBlog, Geek of the Month, Geek on the Top, Generating subarrays using recursion, Geometric Algorithms, Gradient Descent in Linear Regression, Graph, Graph Algorithms, Greedy Algorithms, Hashing, Heap, How to assign values to variables in Python and other languages, HTML, HTML & XML, ide.geeksforgeeks.org, Indentation and Comment in Python, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO CS Exam, Iterative Letter Combinations of a Phone Number, Java, Java and Python, JavaScript, jQuery, Languages, Languages ►, Languages ▼, Last Minute Notes, Linear Regression Using Tensorflow, LinkedList, Machine Learning, Mathematical Algorithms, Matrix, Memoization using decorators in Python, Microprocessor, ML | Cancer cell classification using Scikit-learn, ML | Linear Regression, ML | Unsupervised Face Clustering Pipeline, Multiple Choice Quizzes, Namespaces and Scope in Python, Natural-language-processing, Operating Systems, Operator Overloading in Python, Output of Python Programs | (Dictionary), Pattern Searching, PHP, Placement Course, Polymorphism in Python, Practice, Practice Company Questions, Privacy Policy, Program Output, Project, Puzzles, Python, Python | Catching the ball game, Python | Convert a nested list into a flat list, Python | Convert a string representation of list into list, Python | Convert list of string into sorted list of integer, Python | Convert list of string to list of list, Python | Convert list of tuples into list, Python | Count occurrences of a character in string, Python | Find maximum length sub-list in a nested list, Python | Generate QR Code using pyqrcode module, Python | Image Classification using keras, Python | Implementation of Movie Recommender System, Python | Implementation of Polynomial Regression, Python | Insert list in another list, Python | Maximum sum of elements of list in a list of lists, Python | NLP analysis of Restaurant reviews, Python | Output Formatting, Python | Output using print() function, Python | Pandas Split strings into two List/Columns using str.split(), Python | Program to generate one-time password (OTP), Python | range() method, Python | Remove all values from a list present in other list, Python | Sort list according to other list order, Python | Sort list of list by specified index, Python | Sort the values of first list using second list, Python | Unique values in Matrix, Python | Ways to check if element exists in list, Python | Ways to sum list of lists and return sum list, Python for Data Science, Python in Competitive Programming, Python List Comprehension | Segregate 0's and 1's in an array list, Python list sort(), Python list-programs, Python program to add two numbers, Python program to check whether a number is Prime or not, Python program to create a list of tuples from given list having number and its cube in each tuple, Python program to find day of the week for a given date, Python program to print all Prime numbers in an Interval, Python program to swap two elements in a list, Python Programs, Queue, Quizzes ▼, Randomized Algorithms, School Programming, Searching Algorithms, Skip to content, Software Engineering, Some rights reserved, Sorting Algorithms, SQL, Stack, Statement, Strings, Structuring Python Programs, Students ▼, Subjective Questions, Suggest an Article, Taking input from console in Python, Taking input in Python, Taking multiple inputs from user in Python, Testimonials, Theory of Computation, Top Topics, Topic-wise, Topicwise ►, Tree based DS ►, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, Video Tutorials, Videos, Web Technology, What’s Difference?, Write an Article, Write Interview Experience

Primary Sidebar

Recent Posts

  • 5 Women Programmers Who Changed The World!
  • Wwe network subscribers reached 2.1 million
  • Surveying Questions and Answers – City and Township Surveying
  • Qualcomm Interview Experience ( On-Campus Drive)
  • 10k. Advanced WebDriver – Using Log4j Part 1
  • Privacy Policy
  • About
  • Contact US

© 2019 ProProgramming
 Privacy Policy About Contact Us