私信  •  关注

Carcigenicate

Carcigenicate 最近创建的主题
Carcigenicate 最近回复了
5 年前
回复了 Carcigenicate 创建的主题 » Python:无法在第一个函数的结果之后移动到第二个函数

您的第二个功能正在进入。如果上面的通话结束了,那一定是。

def function_search_search_key():
    with open('scan_dcn.yaml', 'r') as yamlfile:
        search_search_key = ['{search_key:']
        for line in yamlfile.readlines():
            for word in search_search_key:
                if word in line:
                    print(line)


def function_search_event_type():
    with open('scan_dcn.yaml', 'r') as yamlfile:  # Read the file again
        search_event_type = ['event_type:']
        for line in yamlfile.readlines():
            for word in search_event_type:
                if word in line:
                    print(line)
5 年前
回复了 Carcigenicate 创建的主题 » python切片的奇怪行为

a[3:8:-1]

切片的开始和停止位置不会根据步骤进行调整。如果步骤为负,则从3开始倒转,但是没有索引在3到8之间的元素从3开始倒数,因此得到一个空列表。

您需要相应地设置开始和停止:

a[8:3:-1]

从8到4。

6 年前
回复了 Carcigenicate 创建的主题 » python 3中打印next()的问题

Calling next advances the given iterator ( fasta 在这种情况下)。多次调用它将多次前进并导致跳过元素。如果要在不同的位置使用数据,则需要将返回值保存在变量中:

if item in line:
    data = next(fasta) # Save it here
    item = item.strip()
    print('Line:', line, '\nNext line:', data) # Then use it multiple times here and below
    print(line, data, file=open('finalList.fa', "a"))
5 年前
回复了 Carcigenicate 创建的主题 » 在线程安全对象上使用包装器的python线程

不要使用集合来传递参数。在许多python版本中,集合是无序的。你不能保证在迭代时它们的顺序和字面的顺序是一样的。似乎是 1 q 在迭代时交换位置。我可以在我的python版本中半复制它:

q = queueWrapper()
lock = threading.Lock()

print(*{1, q, lock}) 

1 <unlocked _thread.lock object at 0xe7560830> <__main__.queueWrapper object at 0xe7552f50>

注意队列和锁是如何交换位置的。

改用列表或元组。两者都能妥善维持秩序:

producer1 = threading.Thread(target = producer, name = 'Add 1', args = (1, q, lock)) 
6 年前
回复了 Carcigenicate 创建的主题 » 有没有一种pythonic方法可以将函数应用于多个现有变量?[重复]

那不行,因为 x = change_value(x) 正在重新分配本地 x 变量。这样重新分配本地值不会影响正在迭代的集合。

如果您有一组具有类似用途的变量,那么将它们放在一个结构中可能是合适的。即时的好处是您可以在集合上迭代并轻松地转换值。

如果这些值实际上不需要命名,则可以将它们粘贴到列表中并映射列表:

xs = [1, 2, 3]

changed_xs_comp = [change_value(x) for x in xs]
changed_xs_map = list( map(change_value, xs) )

print(changed_xs_comp, changed_xs_map)) # Prints [2, 4, 6] [2, 4, 6]

或者,如果你想要名字,使用字典:

d = {"a":1, "b":2, "c":3}

changed_d = {k:change_value(d[k]) for k in d}

print(changed_d) # Prints {'a': 2, 'b': 4, 'c': 6}

对它们进行分组是否合适需要更多的上下文来说明,但如果它们都需要由同一个函数映射,这对我来说是一个很强的指标,表明它们很可能应该被分组。