python-3.3

Python how to keep writing to a file without erasing what's already there

孤街浪徒 提交于 2019-12-20 03:05:37
问题 Writing my python 3.3 program in Windows, I've run into a little problem. I'm trying to write some lines of instructions to a file for the program to execute. But every time I file.write() the next line, it replaces the previous line. I want to be able to keep writing as many lines to this file as possible. NOTE: Using "\n" doesn't seem to work because you don't know how many lines there are going to be. Please help! Here is my code (being a loop, I do run this multiple times): menu = 0 while

List comprehension replacing items that are not float or int

只谈情不闲聊 提交于 2019-12-19 11:48:13
问题 I have a 2 item list. Sample inputs: ['19(1,B7)', '20(1,B8)'] ['16 Hyp', '16 Hyp'] ['< 3.2', '38.3302615548213'] ['18.6086945477694', '121.561539536844'] I need to look for anything that isn's a float or an int and remove it. So what I need the above list to look like is: ['19(1,B7)', '20(1,B8)'] ['16 Hyp', '16 Hyp'] ['3.2', '38.3302615548213'] ['18.6086945477694', '121.561539536844'] I wrote some code to find '> ' and split the first item but I am not sure how to have my 'new item' take the

Replace the content of dictionary with another dictionary in a function - Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 06:17:48
问题 I have a dictionary, and I want to pass it as argument to a function. After executing this function, I want the dictionary changed. Here is my try: def func(dict): dict = {'a': 5} So what I want to happen: dict = {'b': 3} func(dict) print(dict) # to be {'a': 5}, not {'b': 3} Is there a way to achieve this? Thank you very much in advance! :) 回答1: def func(dct): dct.clear() dct.update({'a': 5}) 回答2: You can mutate a dictionary within a method, because it is passed by reference, but in your case

Replace the content of dictionary with another dictionary in a function - Python

随声附和 提交于 2019-12-19 06:17:32
问题 I have a dictionary, and I want to pass it as argument to a function. After executing this function, I want the dictionary changed. Here is my try: def func(dict): dict = {'a': 5} So what I want to happen: dict = {'b': 3} func(dict) print(dict) # to be {'a': 5}, not {'b': 3} Is there a way to achieve this? Thank you very much in advance! :) 回答1: def func(dct): dct.clear() dct.update({'a': 5}) 回答2: You can mutate a dictionary within a method, because it is passed by reference, but in your case

Python3 - reload() can not be called on __import__ object?

家住魔仙堡 提交于 2019-12-18 18:35:35
问题 Ok so for a number of reasons, I've been using s = __import__('parse') for the longest time in Python2, now I sorta need to being my transitions to Python3 for certain projects (mainly due to SSL). But there's no reload() equivilant in Python3 as far as i've spotted. There's only one option: import parse parse.reload() #works But I really, really need this to work: parse = __import__('parse') parse.reload() or reload(parse) Any ideas on how to get it working in Python3? 回答1: The reload built

Psycopg2 fails to install on python 3 with pip issuing a fatal error

半城伤御伤魂 提交于 2019-12-18 06:53:56
问题 $ yum install python3 postgresql python-devel libpqxx-devel Loaded plugins: langpacks, refresh-packagekit Package python3-3.3.2-8.fc20.x86_64 already installed and latest version Package postgresql-9.3.2-2.fc20.x86_64 already installed and latest version Package python-devel-2.7.5-9.fc20.x86_64 already installed and latest version Package 1:libpqxx-devel-3.2-0.5.fc20.x86_64 already installed and latest version Nothing to do I am trying to install psycopg2 in my virtualenv to connect django

Simultaneous .replace functionality

邮差的信 提交于 2019-12-18 05:56:39
问题 I have already converted user input of DNA code (A,T,G,C) into RNA code (A,U,G,C) . This was fairly easy RNA_Code=DNA_Code.replace('T','U') Now the next thing I need to do is convert the RNA_Code into it's compliment strand. This means I need to replace A with U, U with A, G with C and C with G, but all simultaneously. if I say RNA_Code.replace('A','U') RNA_Code.replace('U','A') it converts all the As into Us then all the Us into As but I am left with all As for both. I need it to take

Accessing values in nested dictionary

偶尔善良 提交于 2019-12-17 23:44:28
问题 Nested dictionary has a length of 12, this is one of the records: {('ALEXANDER', 'MALE'): {'2010': ('2619', None), '2011': ('2494', None), '2009': ('2905', None)}, ... Main key = ('ALEXANDER', 'MALE') Main value (which is nested dictionary) = {'2010': ('2619', None), '2011': ('2494', None), '2009': ('2905', None)} Nested dictionary key/value = '2010': ('2619', None) ... How would one access the year '2010' and the value '2619' ? Is it possible to do this using variables? 回答1: This may point

sum of nested list in Python

霸气de小男生 提交于 2019-12-17 19:22:19
问题 I try to sum a list of nested elements e.g, numbers=[1,3,5,6,[7,8]] should produce sum=30 I wrote the following code : def nested_sum(L): sum=0 for i in range(len(L)): if (len(L[i])>1): sum=sum+nested_sum(L[i]) else: sum=sum+L[i] return sum The above code gives following error: object of type 'int' has no len() I also tried len([L[i]]) , still not working. Anyone can help? It is Python 3.3 回答1: You need to use isinstance to check whether an element is a list or not. Also, you might want to

Is python package virtualenv necessary when I use python 3.3?

空扰寡人 提交于 2019-12-17 18:25:39
问题 I was looking in Cristoph Gohlke's python packages and I noticed that there is a package Virtualenv for Python 3.3. Since there is a package venv in the standard python library v3.3, I was wondering if there is an advantage to install this package separately. Edit: From the documentation of both packages, virtualenv 1.8.2 and venv I can say that the venv standard library package lacks the functionality of: --no-site-packages option choice between setuptools or distribute inability to install