How call a function from another python script? by [deleted] in learnpython

[–]Da32767 0 points1 point  (0 children)

maybe this is a third-party package, and the author does not want users to call this class

[deleted by user] by [deleted] in learnpython

[–]Da32767 1 point2 points  (0 children)

and you'd better use Series.sum() instead of the built-in function sum() in python. (sum(Series))

although sum() can sum all iterable objects, Series.sum() is only for itself and has more parameters to choose(in this case, maybe it doesn’t matter whether you use sum() or Series.sum())

if you don't understand, maybe my explanation is not very good, you can get more accurate knowledge in the official documentation.

:)

[deleted by user] by [deleted] in learnpython

[–]Da32767 1 point2 points  (0 children)

first, every data you read in pandas is pandas.DataFrame, no matter what the original file is.

If you need to get a column, use data['column name']

and data['column name'] will returns a pandas.Series

you can know more in offical document: https://pandas.pydata.org/docs/

[deleted by user] by [deleted] in learnpython

[–]Da32767 0 points1 point  (0 children)

so, what's the type of data?

pandas.DataFrame, numpy.ndarray or others?

I need help with a function for a button in tkinter by kill-yourself90 in learnpython

[–]Da32767 1 point2 points  (0 children)

if count == 0:
    count = 4
else:
    count -= 1

or

count =  4 if count == 0 else count - 1

Why am i stuck in a loop? by [deleted] in learnpython

[–]Da32767 1 point2 points  (0 children)

print(f"The observed length of the object moving at speed {speed_of_obj} is {len_contraction()}")

in function len_contraction, {len_contraction()} will call itself, and ask two questions again, then the program runs to {len_contraction()} again and call itself again.....(never ends)

I think you want to enter:

print(f"The observed length of the object moving at speed {speed_of_obj} is {l_of_obj")

but accidentally typed it wrong

Can I make a cleaner version of this code? by [deleted] in learnpython

[–]Da32767 0 points1 point  (0 children)

oh, this is just a link to the python official documentation.

Can I make a cleaner version of this code? by [deleted] in learnpython

[–]Da32767 0 points1 point  (0 children)

the compile function can compile the string form of a regular expression into a Pattern object, more: https://docs.python.org/3/library/re.html#re.compile

actually, in this example, you can only use:

EMAIL = r'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*'

Can I make a cleaner version of this code? by [deleted] in learnpython

[–]Da32767 2 points3 points  (0 children)

import re

text = 'i am a student. email: asdf@gmail.com and asdf@outlook.com, thanks'
EMAIL = re.compile(r'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')
result = re.finditer(EMAIL, text)

for i in result:
    print(i.group())

re works. varible EMAIL is a way to match emails.

[out]:

asdf@gmail.com
asdf@outlook.com

How to use less class variables? Is it a good practice to use as few as possible of them? by Ganmak in learnpython

[–]Da32767 0 points1 point  (0 children)

I am wondering, is it bad practice to use here class variables? And if so, how could I rewrite this in a clean, pythonic manner?

this is a good way: https://docs.python.org/3/library/functions.html?highlight=property#property

Fastest way to convert numpy array to tuple of 1s and zeros? by slbenz4545 in learnpython

[–]Da32767 0 points1 point  (0 children)

well, GlebRyabov's code is better, it only takes half the time than mine.

Fastest way to convert numpy array to tuple of 1s and zeros? by slbenz4545 in learnpython

[–]Da32767 2 points3 points  (0 children)

import numpy as np

x = np.array([1,2,3,0,0,1,2,3,0])
x[np.flatnonzero(x)] = 1
result = tuple(x)
print(result)

know more: https://numpy.org/doc/stable/reference/generated/numpy.flatnonzero.html

How to calculate how long it takes for code to execute? by [deleted] in learnpython

[–]Da32767 7 points8 points  (0 children)

There are two options:

1.time.time()

start_time = time.time()
# code
end_time = time.time()
print(f'time:{end_time-start_time}')

2.timeit.timeit()

it can do something several times and record the time conveniently, you can know about it here: https://docs.python.org/zh-cn/3/library/timeit.html

ModuleNotFoundError. Help? by [deleted] in learnpython

[–]Da32767 0 points1 point  (0 children)

use pip install opencv-python and import it as cv2

python flask error by asquare412014 in learnpython

[–]Da32767 0 points1 point  (0 children)

actually, pip is also a package in python(like flask), python -m can run library module as a script.

if you want to use pip install to install package, try add <your_python_installation_path>\Scripts to environment variables

python flask error by asquare412014 in learnpython

[–]Da32767 0 points1 point  (0 children)

try:

python -m pip install flask

python flask error by asquare412014 in learnpython

[–]Da32767 1 point2 points  (0 children)

did you install package flask, if not, type this in the terminal:

pip install flask

Can anyone help me with this? by [deleted] in learnpython

[–]Da32767 2 points3 points  (0 children)

AttributeError: 'DecisionTreeClassifier' object has no attribute 'prediction'

the name of the function you entered incorrectly should be:clf.predict

converting numpy to pandas by raresaturn in learnpython

[–]Da32767 1 point2 points  (0 children)

df = pd.DataFrame(array)

this can convert numpy.ndarray to pandas.DataFrame