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?
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: - >>> def nthzero(numList, nth):
-
... outList = []
-
... for num in numList:
-
... if not num%nth:
-
... outList.append(0)
-
... else:
-
... outList.append(num)
-
... return outList
-
...
-
>>> nthzero(range(1,50),7)
-
[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]
-
>>>
OR any list: - def nthzero(numList, nth):
-
outList = []
-
for i, num in enumerate(numList):
-
if not (i+1)%nth:
-
outList.append(0)
-
else:
-
outList.append(num)
-
return outList
-
-
-
numList = range(6,400,12)
-
nth = 7
-
-
print nthzero(numList, nth)
-
print nthzero(['x']*30, nth)
-
-
>>> [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]
-
['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']
-
>>>
what can I do if the numbers are not separated by commas?
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.
You can use a for loop like this. -
numbers = range(0,2500) # Make list of numbers
-
step = 5
-
-
for num in range(numbers[0], numbers[-1], step):
-
numbers[num] = 0 # set to zero
-
-
print numbers
-
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].
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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";...
|
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...
|
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*...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |