472,992 Members | 3,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

search and replace

ironmonkey69
is there a way that I can search for every nth term sucha as a number ranging from 0-2500 and replacing them with zeroes?
Jul 19 '07 #1
4 1355
bvdet
2,851 Expert Mod 2GB
is there a way that I can search for every nth term sucha as a number ranging from 0-2500 and replacing them with zeroes?
In a list of numbers in sequence:
Expand|Select|Wrap|Line Numbers
  1. >>> def nthzero(numList, nth):
  2. ...     outList = []
  3. ...     for num in numList:
  4. ...         if not num%nth:
  5. ...             outList.append(0)
  6. ...         else:
  7. ...             outList.append(num)
  8. ...     return outList
  9. ... 
  10. >>> nthzero(range(1,50),7)
  11. [1, 2, 3, 4, 5, 6, 0, 8, 9, 10, 11, 12, 13, 0, 15, 16, 17, 18, 19, 20, 0, 22, 23, 24, 25, 26, 27, 0, 29, 30, 31, 32, 33, 34, 0, 36, 37, 38, 39, 40, 41, 0, 43, 44, 45, 46, 47, 48, 0]
  12. >>> 
OR any list:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(numList, nth):
  2.     outList = []
  3.     for i, num in enumerate(numList):
  4.         if not (i+1)%nth:
  5.             outList.append(0)
  6.         else:
  7.             outList.append(num)
  8.     return outList
  9.  
  10.  
  11. numList = range(6,400,12)
  12. nth = 7
  13.  
  14. print nthzero(numList, nth)
  15. print nthzero(['x']*30, nth)
  16.  
  17. >>> [6, 18, 30, 42, 54, 66, 0, 90, 102, 114, 126, 138, 150, 0, 174, 186, 198, 210, 222, 234, 0, 258, 270, 282, 294, 306, 318, 0, 342, 354, 366, 378, 390]
  18. ['x', 'x', 'x', 'x', 'x', 'x', 0, 'x', 'x', 'x', 'x', 'x', 'x', 0, 'x', 'x', 'x', 'x', 'x', 'x', 0, 'x', 'x', 'x', 'x', 'x', 'x', 0, 'x', 'x']
  19. >>>
Jul 19 '07 #2
what can I do if the numbers are not separated by commas?
Jul 19 '07 #3
bartonc
6,596 Expert 4TB
what can I do if the numbers are not separated by commas?
Your question lacks specificity. Perhaps you are wanting to work with text?
Maybe that text is broken up with newline character, maybe not.
You may even need help reading a text file into memory.

Please be specific in your questions.
Thank you.
Jul 19 '07 #4
cnobile
6 Nibble
You can use a for loop like this.

Expand|Select|Wrap|Line Numbers
  1. numbers = range(0,2500) # Make list of numbers
  2. step = 5
  3.  
  4. for num in range(numbers[0], numbers[-1], step):
  5.     numbers[num] = 0 # set to zero
  6.  
  7. print numbers
  8.  
This will work with text or numbers. First put your objects in a list. In the case of text use 0 instead of numbers[0] and the result of len(text) for numbers[-1].
Jul 19 '07 #5

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

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
9
by: tomjones75 | last post by:
dear community, i want to search the content of all fields in one table in a access database. it already works for the content of one field in the table. please take a look at the code in...
14
by: Simon Gare | last post by:
Hi, have a search.asp page with results.asp page drawing data from an SQL db, problem is the user has to type the whole field value into the search box to retrieve the value on results.asp, what...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.