社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Olivier Melançon

Olivier Melançon 最近创建的主题
Olivier Melançon 最近回复了
5 年前
回复了 Olivier Melançon 创建的主题 » python中的变量字符串替换

使用 MRNA.replace('C', 'G').replace('G', 'C') 将替换任何 'C' 'G' 立即用一个 'C'

不是多重的 str.replace 你应该使用一个翻译表 str.maketrans str.translate str.替换 增加。

def dna_to_rna(s):
    trans_table = str.maketrans('ATCG', 'UAGC')
    return s.translate(trans_table)

print(dna_to_rna('ACGTAC')) # 'UGCAUG'
5 年前
回复了 Olivier Melançon 创建的主题 » 如何在Python中从列表中移除sting元素

宣布你的名单 功能 choose_a_word_easy ,每次调用都会创建一个新列表。你希望每次通话都重复使用同一个列表。通过在函数的作用域之外创建列表并将其作为参数传递来执行此操作。

words = ['ant', 'bee', 'cat', 'dog', ...]

def pick_and_remove(lst):
    pick = random.choice(lst)
    lst.remove(pick)
    return pick

pick = pick_and_remove(words)

print(pick) # 'bee'
print(words) # ['ant', 'cat', 'dog', ...]

def pick_and_remove(lst):
    i = random.randrange(len(lst))
    return lst.pop(i)
6 年前
回复了 Olivier Melançon 创建的主题 » python高效的字典中的并行列表排序

你可以先分类 enumerate 以恢复所需的索引顺序,然后按该顺序重新排列每个列表。

my_dict = {
   'key_one': [1,6,2,3],
   'key_two': [4,1,9,7],
   'key_three': [1,2,4,3],
}


def parallel_sort(d, key):
    index_order = [i for i, _ in sorted(enumerate(d[key]), key=lambda x: x[1])]
    return {k: [v[i] for i in index_order] for k, v in d.items()}

print(parallel_sort(my_dict, 'key_three'))

产量

{'key_one': [1, 6, 3, 2],
 'key_two': [4, 1, 7, 9],
 'key_three': [1, 2, 3, 4]}