Creating a list using range function - range

I am looking to create a list using range function without having to type in 0-100. It is not clear to me how to use the range function in python as I am a beginner. Is there a way to do this? Thank you

It's very simple, range returns an iterable, you can specify a start and an end+1 like this:
an_iterable = range(0,101)
Then you can turn it into a list like this:
a_list = list(an_iterable)

Related

Check for list of substrings inside string column in PySpark

For checking if a single string is contained in rows of one column. (for example, "abc" is contained in "abcdef"), the following code is useful:
df_filtered = df.filter(df.columnName.contains('abc'))
The result would be for example "_wordabc","thisabce","2abc1".
How can I check for multiple strings (for example ['ab1','cd2','ef3']) at the same time?
I'm ideally searching for something like this:
df_filtered = df.filter(df.columnName.contains(['word1','word2','word3']))
The result would be for example "x_ab1","_cd2_","abef3".
Please, post scalable solutions (no for loops, for example) because the aim is to check a big list around 1000 elements.
All you need is isin
df_filtered = df.filter(df['columnName'].isin('word1','word2','word3')
Edit
You need rlike function to achieve your result
words="(aaa|bbb|ccc)"
df.filter(df['columnName'].rlike(words))

PSQL Apply mapping to an array

I have an array of an enum type order_options that gets passed through to a function and it is pretty simple, it looks like this:
'{master_date, e_name}'
I have 6 possible values and the enum type looks like this:
create type order_options as enum ('master_date','e_name','r_data','n_count','creation_date','last_updated_on');
In the function however, I want to apply a mapping to these values to change the value of sort_params which is the array passed through.
For each enum, I have an 'alternative name' that I want to use in the order by clause of a subsequent select statement. For example:
'master_date' = o.master_date
'e_name' = d.e_name
and so on.
I've looked in to doing a replace whereby I loop through each element of the array and attempt a replace with each mapping but it gets pretty messy and complex.
So for example, this works for one individual mapping:
select array_replace('{master_date}'::text[],'master_date','o.master_date');
I'd like my sort_params to look like this after mapping:
'{o.master_date, d.e_name}'
Is there any easier way to do this?
For now, this is what I have:
select array_replace(sort_params_u,'master_date','o.master_date') into sort_params_u;
select array_replace(sort_params_u,'e_name','d.e_name') into sort_params_u;
etc

How to assign value to a element in 2D list in python

For mentioned python code I am getting result as [[0,4,2],[0,4,2]]. Here '2', I am passing dynamically. I want result as [[0,1,2],[0,4,2]], how can I get this?
a = [[0,1,2]]*2
a[1][1] = 4
The issue is in a way you create 2d list a. It holds links to same strings in each row.
Proper way to create it:
a = [[0,1,2] for i in range(2)]
Found here.

JES: Implementing modification code to a Loop

I am using JES and am wondering what built in function I should use to make this effect work.
newG=(oldG+(abs(x*y*2.57901)%64))%256
So far I have this code
def forLoop():
picture = makeEmptyPicture(300,200)
show(picture)
for p in getPixels(picture):
setColor(p,black)
repaint(picture)
for p in getPixels(picture):
oldG=(p)
newG=(oldG+(abs(x*y*2.57901)%64))%256
repaint(picture)
The error I get is
The error was:x Name not found globally.
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
you'll need to define the local names for x and y to be able to get the newG color, hope this helps
coding is something like this:
x= getX(p)
y= getY(p)

JQuery Wildcard for using atttributes in selectors

I've research this topic extensibly and I'm asking as a last resort before assuming that there is no wildcard for what I want to do.
I need to pull up all the text input elements from the document and add it to an array. However, I only want to add the input elements that have an id.
I know you can use the \S* wildcard when using an id selector such as $(#\S*), however I can't use this because I need to filter the results by text type only as well, so I searching by attribute.
I currently have this:
values_inputs = $("input[type='text'][id^='a']");
This works how I want it to but it brings back only the text input elements that start with an 'a'. I want to get all the text input elements with an 'id' of anything.
I can't use:
values_inputs = $("input[type='text'][id^='']"); //or
values_inputs = $("input[type='text'][id^='*']"); //or
values_inputs = $("input[type='text'][id^='\\S*']"); //or
values_inputs = $("input[type='text'][id^=\\S*]");
//I either get no values returned or a syntax error for these
I guess I'm just looking for the equivalent of * in SQL for JQuery attribute selectors.
Is there no such thing, or am I just approaching this problem the wrong way?
Actually, it's quite simple:
var values_inputs = $("input[type=text][id]");
Your logic is a bit ambiguous. I believe you don't want elements with any id, but rather elements where id does not equal an empty string. Use this.
values_inputs = $("input[type='text']")
.filter(function() {
return this.id != '';
});
Try changing your selector to:
$("input[type='text'][id]")
I figured out another way to use wild cards very simply. This helped me a lot so I thought I'd share it.
You can use attribute wildcards in the selectors in the following way to emulate the use of '*'. Let's say you have dynamically generated form in which elements are created with the same naming convention except for dynamically changing digits representing the index:
id='part_x_name' //where x represents a digit
If you want to retrieve only the text input ones that have certain parts of the id name and element type you can do the following:
var inputs = $("input[type='text'][id^='part_'][id$='_name']");
and voila, it will retrieve all the text input elements that have "part_" in the beginning of the id string and "_name" at the end of the string. If you have something like
id='part_x_name_y' // again x and y representing digits
you could do:
var inputs = $("input[type='text'][id^='part_'][id*='_name_']"); //the *= operator means that it will retrieve this part of the string from anywhere where it appears in the string.
Depending on what the names of other id's are it may start to get a little trickier if other element id's have similar naming conventions in your document. You may have to get a little more creative in specifying your wildcards. In most common cases this will be enough to get what you need.