python

Why is adding to or removing from the middle of a collections.deque slower than lookup there?

无人久伴 提交于 2021-02-20 06:30:47
问题 This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object: A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. Two questions: 1) Is adding to the middle of a deque even possible? I don't see any method to

Why is adding to or removing from the middle of a collections.deque slower than lookup there?

做~自己de王妃 提交于 2021-02-20 06:29:53
问题 This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object: A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. Two questions: 1) Is adding to the middle of a deque even possible? I don't see any method to

List Comprehensions in Python to compute minimum and maximum values of a list

此生再无相见时 提交于 2021-02-20 06:25:26
问题 i have the following code to compute minimum and maximum values of a list in order to save memory efficiency x_min = float('+inf') x_max = float('-inf') for p in points_in_list: x_min = min(x_min, p) x_max = max(x_max, p) where points_in_list is a (large) list of numbers. I wish to know if there is a method to compute with a List Comprehensions the min and max value and saving the memory. 回答1: I'm a big fan of generators and comprehensions, but in this case it seems they are not the right way

Pandas. How to read Excel file from ZIP archive

只谈情不闲聊 提交于 2021-02-20 06:18:16
问题 I have .zip archive with filename.xlsx inside it and I want to parse Excel sheet line by line. How to proper pass filename into pandas.read_excel in this case? I tried: import zipfile import pandas myzip=zipfile.ZipFile(filename.zip) for fname in myzip.namelist(): with myzip.open(fname) as from_archive: with pandas.read_excel(from_archive) as fin: for line in fin: .... but it doesn't seem to work, and the result was: AttributeError: __exit__ 回答1: You can extract your zip-file into a variable

plot dataframe with two y-axes

ⅰ亾dé卋堺 提交于 2021-02-20 06:16:57
问题 I have the following dataframe: land_cover 1 2 3 4 5 6 size 0 20 19.558872 6.856950 3.882243 1.743048 1.361306 1.026382 16.520265 1 30 9.499454 3.513521 1.849498 0.836386 0.659660 0.442690 8.652517 2 40 10.173790 3.123167 1.677257 0.860317 0.762718 0.560290 11.925280 3 50 10.098777 1.564575 1.280729 0.894287 0.884028 0.887448 12.647710 4 60 6.166109 1.588687 0.667839 0.230659 0.143044 0.070628 2.160922 5 110 17.846565 3.884678 2.202129 1.040551 0.843709 0.673298 30.406541 I want to plot the

plot dataframe with two y-axes

懵懂的女人 提交于 2021-02-20 06:16:18
问题 I have the following dataframe: land_cover 1 2 3 4 5 6 size 0 20 19.558872 6.856950 3.882243 1.743048 1.361306 1.026382 16.520265 1 30 9.499454 3.513521 1.849498 0.836386 0.659660 0.442690 8.652517 2 40 10.173790 3.123167 1.677257 0.860317 0.762718 0.560290 11.925280 3 50 10.098777 1.564575 1.280729 0.894287 0.884028 0.887448 12.647710 4 60 6.166109 1.588687 0.667839 0.230659 0.143044 0.070628 2.160922 5 110 17.846565 3.884678 2.202129 1.040551 0.843709 0.673298 30.406541 I want to plot the

Python Subprocess Security

百般思念 提交于 2021-02-20 06:16:03
问题 I understand why using 'shell=True' can be a security risk if you have untrusted input. However, I don't understand how 'shell=False' avoids the same risks. Presumably if I wanted to allow a user to provide an input he might input: var="rm -rf /" My code might simply: subprocess.call(var,shell=True) # bad stuff Or I might do: varParts=var.split() subprocess.call(varParts,shell=False) # also bad, right? It would seem that the assumption is one wouldn't go through the trouble of processing the

Pandas. How to read Excel file from ZIP archive

…衆ロ難τιáo~ 提交于 2021-02-20 06:13:27
问题 I have .zip archive with filename.xlsx inside it and I want to parse Excel sheet line by line. How to proper pass filename into pandas.read_excel in this case? I tried: import zipfile import pandas myzip=zipfile.ZipFile(filename.zip) for fname in myzip.namelist(): with myzip.open(fname) as from_archive: with pandas.read_excel(from_archive) as fin: for line in fin: .... but it doesn't seem to work, and the result was: AttributeError: __exit__ 回答1: You can extract your zip-file into a variable

How to extract dependencies information from a setup.py

左心房为你撑大大i 提交于 2021-02-20 06:12:38
问题 I have a python project, let's call it foobar , there is a setup.py script in project root directory like all Python projects. For example foobar setup.py setup.py file content: from ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages setup( name='foobar', version='0.0.0', packages=find_packages(), install_requires=[ 'spam==1.2.3', 'eggs>=4.5.6', ], ) I need to get dependencies information from that setup.py file using Python. The part I want would be [

Python Subprocess Security

只谈情不闲聊 提交于 2021-02-20 06:11:18
问题 I understand why using 'shell=True' can be a security risk if you have untrusted input. However, I don't understand how 'shell=False' avoids the same risks. Presumably if I wanted to allow a user to provide an input he might input: var="rm -rf /" My code might simply: subprocess.call(var,shell=True) # bad stuff Or I might do: varParts=var.split() subprocess.call(varParts,shell=False) # also bad, right? It would seem that the assumption is one wouldn't go through the trouble of processing the