python屬性中str對象的用法
files=glob.glob('*_z1.dat')files.sort()for file in files: f=open(files[0], 'r') abundance=file.readlines() 你在循環瀏覽你glob編輯的文件列表,但是你只在每次迭代中打開第一個文件&甚至不使用打開的文件。 您可能希望打開每個文件并讀取它: files=glob.glob('*_z1.dat')files.sort()for file in files: f=open(file, 'r') # open the current file abundance=f.readlines() # read it 您遇到的錯誤是因為file是字符串-f是打開的file對象,它具有readlines()方法
用Python中的zip文件發送電子郵件
打開zip文件時不要傳遞編碼。這是二進制數據,應該這樣對待!open(..., 'b')
python中關于派生類用法語法的繼承
Python不關心提前檢查類型、繼承關系等。它只關心代碼運行時會發生什么。這允許所謂的“鴨子鍵入”: class Employee: def fired(self): print("Oh no, I have to look for a new job")class Gun: def fired(self): print("Bang! Did the bullet hit the target?")for thing in [Employee(), Gun()]: thing.fired() 類Employee和Gun彼此無關并不重要,fired的目的完全不同也不重要。列表中的兩個對象都有一個名為fired的方法,因此代碼可以正常工作。 在代碼中類似: class emp: def __init__(self,fname,lname,empno): self.empno=empno person.__init__(self,fname,lname) 如果我們調用person.__init__,那么這只是我們在person類中找到的一個函數,而不是我們在對象上查找的方法。這是因為我們在.的left-hand端使用了一個類,而不是一個實例。該函數將愉快地接受emp實例作為self的值-它不關心self是否是person實例。它是一個user-defined對象,所以設置fname和ln
Python Faster'If'用法
使用具有固定插入和檢索時間的集合。相比之下,in操作符在a每次檢查中執行線性搜索。 我不太清楚你的use-case是什么,但沒有看到你的大代碼。我假設您的use-case將a視為標志列表。因此,一套合算。 a = [1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12]a = set(a) # pass an iterable# or simplya = {1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12}# or built at runtimea = set()a.add(1)a.add(2)if 3 in a: some_work1 如果您想要一個更有效的switch語句,那么您已經找到了它。Python為此使用if..elif。這確保了每一個都是按short-circuit順序計算的。如果你能匹配多個結果,使用dict(例如{3: functor3, 4: functor4, ...})。函子是可調用的,即它定義了__call__()方法。lambda也滿足這一點。 集合是不允許重復的無序集合。它就像一個字典,但刪除了值,只留下鍵。如您所知,字典鍵是唯一的,同樣,集合的成員也是唯一的。在這里,我們只需要一套性能。
對于python中具有zip的循環
zip不是這里的問題。問題是for循環只會在字典中的鍵上循環,而不是鍵和值(參見此答案) 正確的做法是: birth_years = {"Alice": "1990", "Bob": "1990", "Carol": "1995", "Felix":"1995","Max":"1995","Chris":"1998","Lea":"1998","Linn":"1998","Julia":"1998"}books = {"Alice": "+1554", "Bob": "+1885", "Carol": "+1006", "Felix":"+1604", "Max":"+1369","Chris":"+1882","Lea":"+1518","Linn":"+1742","Julia":"+1707"} def test(birth_years, books): new_list = [] for [name, birth_year], [unused, number] in zip(birth_years.items(), books.items()): new_list.append(f'{name} is {2022 - int(birth_year)} years old, and they can be called at {number}') return new_lis
一些zip文件在Python中沒有存檔
您應該使用zipfile(用于zip存檔)或tarfile(用于tar存檔)
為什么我在python中使用zip后得到空列表?
zip返回一個迭代器對象。第一次將其轉換為列表時,迭代器將被使用,之后它將為空。如果立即將其轉換為列表,然后copy將其轉換為: result = list(zip(number_list, str_list))# Converting list to more listsresult_list = result.copy()result_list2 = result.copy()
python`zip`文檔中的內置行為不明確
實際上-this是(3)和(4)。此外,它實際上如預期的那樣工作:人們并不認為它有魔力,但希望zip能夠按照給定的順序在內部從迭代器中獲取元素。
如何在Python中下載zip文件?
如果您只想下載帶有url的文件并保存到特定位置,使用urlretrieve會更好: import urllib.requestprint('Beginning file download with urllib2...')url = 'http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg'urllib.request.urlretrieve(url, '/Users/scott/Downloads/cat.jpg')
與格式有關的問題。python中format()的用法
你可以把它看作是一種字符串替換。 {} part in the string -> string.format() content Definition: https://www.w3schools.com/python/ref_string_format.asp 一個實際的例子可以是這樣的: base_url = 'www.xxxx.com/test?page={}'for i in range(10): url = base_url.format(i) do sth
如何迭代python中zip存檔的zip存檔?
使用io.BytesIO()將內容提取到內存中 from zipfile import ZipFileimport iowith ZipFile('zip.zip', 'r') as zipObj: for fileName in zipObj.namelist(): if fileName.endswith('.zip'): content = io.BytesIO(zipObj.read(fileName)) # zip1.zip with ZipFile(content) as z: for txt in z.namelist(): if txt.endswith('.txt'): txt = z.read('0.txt') print (txt)
Python元組的zip列表
只需使用zip和list即可。 >>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]>>> [list(i) for i in zip(*tuples)][[1, 4, 7], [2, 5, 8], [3, 6, 9]] Or, >>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]>>> [[*i] for i in zip(*tuples)][[1, 4, 7], [2, 5, 8], [3, 6, 9]]
結合zip多次生成python引用
這是因為在g1, g2 = zip(*add_ten(nums))中發生了解包*add_ten(nums)。 add_tens生成器立即耗盡,以便創建解包列表。此列表將作為zip的參數,它將包含: (<your array>, <a new array containing 10>),(<your array>, <a new array containing 11>),(<your array>, <a new array containing 12>),(<your array>, <a new array containing 13>), 由add_ten產生。 一旦拉上拉鏈,您將擁有: (<your array>, <your array>, <your array>, <your array>),(<a new array containing 10>, ... <a new array containing 13>) 當您打印<your_array>的內容時,您將得到4個引用中每個引用的3,因為這是它的當前內容。 在fun2中,當您在迭代的每一步打印數組的內容時,您將得到它的連續內容,即0、1
在python中打開枚舉zip
你得到int和tuple。用括號將x,y,z括起來,使之成為tuple。 [f.write('mVtet0.row({}) = Eigen::Vector3d({}, {}, {})\n'.format(i, x,y,z) for i, (x, y, z) in enumerate(zip(x,y,z))] 也就是說,在我看來,這是濫用列表理解的唯一目的是使它one-liner。最好使用常規循環-它會更可讀。而且最好使用f-strings。 for i, (x, y, z) in enumerate(zip(x,y,z)): f.write(f'mVtet0.row({i}) = Eigen::Vector3d({x}, {y}, {z})\n')
使用python下載zip文件
您需要發送referer標頭: headers = {'Referer':'https://www1.nseindia.com'} ... r = requests.get(url, stream=True,headers=headers)