473,735 Members | 3,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using the split command in a list

26 New Member
If you have a list how do you use the split command to split each line into word, and freqency?
Jun 10 '07 #1
14 39963
Smygis
126 New Member
If you have a list how do you use the split command to split each line into word, and freqency?
What?

Like:
Expand|Select|Wrap|Line Numbers
  1. >>> ListOWords = ["Hello Wold", "I am a", "list of words"]
  2. >>> Stuff = [i.split() for i in ListOWords]
  3. >>> Stuff
  4. [['Hello', 'Wold'], ['I', 'am', 'a'], ['list', 'of', 'words']]
  5. >>> 
  6.  
I guess its totaly offbase but i have no idea what you want.
Jun 10 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
If you have a list how do you use the split command to split each line into word, and freqency?
Do you have a list, a line, or a list of lines? Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> sentences = 'This is a sentence that we are going to split. We will also determine the frequency of each word. This sentence is here just for the heck of it.'
  2. >>> wordList = [s.lower() for s in sentences.split()]
  3. >>> wordCnt = [wordList.count(w) for w in wordList]
  4. >>> dd = dict(zip(wordList,wordCnt))
  5. >>> for item in dd:
  6. ...     print "Word '%s' occurs %d times." % (item, dd[item])
  7. ...     
  8. Word 'just' occurs 1 times.
  9. Word 'sentence' occurs 2 times.
  10. Word 'is' occurs 2 times.
  11. Word 'word.' occurs 1 times.
  12. Word 'frequency' occurs 1 times.
  13. Word 'are' occurs 1 times.
  14. Word 'determine' occurs 1 times.
  15. Word 'for' occurs 1 times.
  16. Word 'to' occurs 1 times.
  17. Word 'also' occurs 1 times.
  18. Word 'going' occurs 1 times.
  19. Word 'split.' occurs 1 times.
  20. Word 'it.' occurs 1 times.
  21. Word 'we' occurs 2 times.
  22. Word 'that' occurs 1 times.
  23. Word 'here' occurs 1 times.
  24. Word 'a' occurs 1 times.
  25. Word 'this' occurs 2 times.
  26. Word 'of' occurs 2 times.
  27. Word 'will' occurs 1 times.
  28. Word 'heck' occurs 1 times.
  29. Word 'each' occurs 1 times.
  30. Word 'the' occurs 2 times.
  31. >>>
Here's a way to get a list of words from lines from a file without using split:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. lineList = open(r'X:/path/subdir/name_of_file').readlines()
  4. pat = "\w+"
  5. wordList = []
  6.  
  7. for line in lineList:
  8.     wordList += [w.lower() for w in re.findall(pat,line)]
  9.  
  10. wordCnt = [wordList.count(w) for w in wordList]
  11.  
  12. dd = dict(zip(wordList,wordCnt))
  13.  
  14. for item in dd:
  15.     print "Word '%s' occurs %d times." % (item, dd[item])
This way will exclude any punctuation. If you have a list of lines and you don't care about possible punctuation:
Expand|Select|Wrap|Line Numbers
  1. >>> wordList = []
  2. >>> for line in lineList:
  3. ...     wordList += [s.lower() for s in line.strip().split()]
Jun 11 '07 #3
bartonc
6,596 Recognized Expert Expert
I alway like to add this little touch of elegance:
Expand|Select|Wrap|Line Numbers
  1. >>> for item in dd:
  2. ...     i = dd[item]
  3. ...     print "Word '%s' occurs %d time%s." % (item, i, ('s', '')[int(i == 1)])
Jun 11 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
I alway like to add this little touch of elegance:
Expand|Select|Wrap|Line Numbers
  1. >>> for item in dd:
  2. ...     i = dd[item]
  3. ...     print "Word '%s' occurs %d time%s." % (item, i, ('s', '')[int(i == 1)])
Barton,

You have posted something similar to this before. I am beginning to catch on. Thanks! :)

BV
Jun 11 '07 #5
bartonc
6,596 Recognized Expert Expert
Barton,

You have posted something similar to this before. I am beginning to catch on. Thanks! :)

BV
Nope. I think that this is the first opportunity. It comes up often in GUI programming where (say) you have a RadioButton and you want the screen to reflect its state elsewhere, as in:
Expand|Select|Wrap|Line Numbers
  1. flag = aRadioButton.GetState() # actually an int, not bool
  2. stateStr = ("Off", "On")[flag]  # tuples require int indexes so there is often a cast from bool to int
I've always felt that software should be smart enough to know if it is relaying data about a thing or several things. To me it's a glaring omission on the part of the programmer when the user is told that he has 1 things.
Jun 11 '07 #6
bvdet
2,851 Recognized Expert Moderator Specialist
Nope. I think that this is the first opportunity. It comes up often in GUI programming where (say) you have a RadioButton and you want the screen to reflect its state elsewhere, as in:
Expand|Select|Wrap|Line Numbers
  1. flag = aRadioButton.GetState() # actually an int, not bool
  2. stateStr = ("Off", "On")[flag]  # tuples require int indexes so there is often a cast from bool to int
I've always felt that software should be smart enough to know if it is relaying data about a thing or several things. To me it's a glaring omission on the part of the programmer when the user is told that he has 1 things.
This is the snippet I was referring to. I had never thought of supplying a sliced tuple or list as an argument to a string format character.
Expand|Select|Wrap|Line Numbers
  1. # test utility functions and rules
  2. for i in range(20):
  3.     RoleDice(dice)
  4.     PrintDice(dice)
  5.     print "All dice are%sequal" % [" not ", " "][AllEqual(dice)]
  6.     print
Jun 11 '07 #7
ilikepython
844 Recognized Expert Contributor
I alway like to add this little touch of elegance:
Expand|Select|Wrap|Line Numbers
  1. >>> for item in dd:
  2. ...     i = dd[item]
  3. ...     print "Word '%s' occurs %d time%s." % (item, i, ('s', '')[int(i == 1)])
Hmm, that's a clever way of doing it, never would have thought of it.
Jun 11 '07 #8
bartonc
6,596 Recognized Expert Expert
This is the snippet I was referring to. I had never thought of supplying a sliced tuple or list as an argument to a string format character.
Expand|Select|Wrap|Line Numbers
  1. # test utility functions and rules
  2. for i in range(20):
  3.     RoleDice(dice)
  4.     PrintDice(dice)
  5.     print "All dice are%sequal" % [" not ", " "][AllEqual(dice)]
  6.     print
Yep. I figured that. As soon as I submitted, I thought "that's not on point", but what's done is done (sort of).
Jun 11 '07 #9
texas22
26 New Member
Ok, this is kind of making sense so once I have pulled out say the three longest, shortest, and middle words what syntax do I use to tell it to take those words and split each word onto a line listing the frequency or number of times each of the words occurs in the list.
Jun 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
22725
by: Kevin | last post by:
Hi All, I am new to MS SQL Server.I am using MS SQL 2000.I have a problem in creating a table by using Select command.I have table called "test" and i want to create another table with the same structure and rows.I tried with the following command create Table test1 as select * from test; But it give an syntax error.I have tried the same command in Oracle
0
1639
by: Nitin Gaur | last post by:
Hi, I am developing an application using Visual Studio .NET in C++. I am using following command to automate the debugging of this executable: devenv -command Debug.StepInto myProg.cpp -debugexe myProg.exe The switch "-command" with option "Debug.StepInto" actually opens up the "Debug" toolbar menu and clicks on "Step Into" item in it. It opens up a "Save File As" dialog for saving "myprog.sln" file. I want to get rid of
3
14616
by: NK | last post by:
Hello, I am a beginner of DB2 and I am in a trouble that I can't access to sample database. Please see below. ENV : OS AIX-5.2/ DB2 8.1.2 SYMPTOM: I don't have any DB2 client terminal so far and all the troubles
1
12844
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
1
3737
by: Mike P | last post by:
I am using Split to split get a number of items from a multi line text box. But I need to be able to get the number of elements within the text box (e.g. if the text box has 2,4,8 in it, I want to be able to count that this text box has 3 elements entered into it). My code is below : for (int p=0; p < dgNational.Items.Count; p++) { strQuantity = ((TextBox)dgNational.Items.FindControl("txtAccount")).Text +"\r\n";
0
6039
by: den 2005 | last post by:
Hi everybody, I am not familiar using Gridview, I created a Gridview with Edit Command Button, but when I click the update Command button this error message below is displayed, what and where is the problem? I am confused. Can anyone show me exactly to do edit process of Gridview using Edit Command button properly? Thanks. Error Message: Invalid postback or callback argument. Event validation is enabled using
10
6577
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
1
10500
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
0
3137
debasisdas
by: debasisdas | last post by:
Using Merge command (supported version 9 onwards) ======================================== Used to go for insert/update in a single command based on some condition. Example #1 merge into emp5 e using (select empno,ename from emp) e1 on (e.empno=e1.empno)
0
8957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8194
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6043
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.