This question already has answers here:
String to list in Python
(5 answers)
Closed 5 years ago.
Given:
['1 -1 1 1 1 1 1 1 1']
How can I convert it (efficiently) to be a vector of integers something like:
[1 -1 1 1 1 1 1 1 1]
Thank you.
return [int(n)
for n in s.split()]
Related
This question already has answers here:
Creation of numpy array from two arrays, such that alternate indices contain elements from different arrays [duplicate]
(2 answers)
Pythonic way to combine (interleave, interlace, intertwine) two lists in an alternating fashion?
(26 answers)
Closed 10 days ago.
With Matlab, I can run this easily :
a = [0 0 0 0 0]
b = [1 1 1 1]
a(1:1:end) = b
To obtain : a = [0 1 0 1 0 1 0 1 0]
How can I implement this using Python ?
This question already has answers here:
Pascal's Triangle for Python
(12 answers)
Closed 2 years ago.
How do I make a triangle using a recursive function like this:
def triangle(3):
And the triangle should look like this:
1
1 1
1 2 1
And so on.
You can do something like this.
n=4
def triangle(n):
if n==0:
return
num=11**(triangle.n-n)
print "{}{}".format(" "*n, " ".join(list(str(num))))
triangle(n-1)
triangle.n = n
triangle(n)
Output:
1
1 1
1 2 1
1 3 3 1
This question already has answers here:
Conditional Replace Pandas
(7 answers)
Closed 2 years ago.
I want to convert all values < 100 to 0 in column ODOMETER_FW. I have below DF:
When I use pandas:
stupid_values = fuel_GB['ODOMETER_FW'].replace(fuel_GB['ODOMETER_FW']<100,0)
fuel_GB['ODOMETER_FW'] = stupid_values
fuel_GB.head(13)
And the result as you can see, has some error and I really do not know why.
Use lambda function to convert values less than 100 to 0:
df['ODOMETER_FW'] = df['ODOMETER_FW'].apply(lambda x: 0 if x <100 else x)
print(df)
ODOMETER_FW
0 11833
1 0
2 9080
3 8878
4 0
5 14578
6 14351
7 0
8 13456
9 0
10 0
11 0
12 0
Just ask the modification for the relevant lines:
fuel_GB.loc[fuel_GB['ODOMETER_FW'] < 100, 'ODOMETER_FW'] = 0
Use this pandas code:
fuel_GB[fuel_GB['ODOMETER_FW'] < 100] = 0
This question already has answers here:
Count all values in a matrix less than a value
(6 answers)
Closed 5 years ago.
Data = matrix R
First I wanted to count elements of each row
countR = np.count_nonzero(R, axis=1)
Then, I could get matrix countR.
[25 2 1 2 2 55 1 2 1 2 1 1 2 2 1 1 1 1 2 2 1 2 14 1 3 ..
Second, I want to count elements in matrix
"if element>1 "
So what I did is here
countR1 = pd.value_counts(countR.values, sort>1)
But there was an error.
How can i count elements?
you can do it easily like this:
y=np.array(countR)
len(y[y>1])
if I understand correctly you want to count all the elements that are larger than 1 in the matrix R.
you can filter the data-frame by doing so(to dispose of the elements that are larger than 1):
biggerThanOne = R[R<1]
you can then get the size of the array and get the number of elements:
biggerThanOne.size
if you mean you want to count the elements of countR you can practically do the same thing
This question already has answers here:
2D array of lists in python
(6 answers)
Closed 6 years ago.
I have a list 2D
a = {}
a[0,0] = 1
a[0,1] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
...
a[1,n] = 6
Now, I want to access the element a[1,x] (x = is 0 to n)
How can I do?
You'll need a nested loop if I understand what your asking for.
somearray = [[0,1],[2,3],[4,5]]
for row in range(len(somearray)):
for col in range(len(somearray[row])):
print(somearray[row][col], end =" ")
will output:
0 1 2 3 4 5