私信  •  关注

Paul M.

Paul M. 最近创建的主题
Paul M. 最近回复了
3 年前
回复了 Paul M. 创建的主题 » Python拼写检查功能-替换独立单词,但不替换子字符串

看起来你想要正则表达式。在本例中,模式(要查找的对象)是字符串 apple 被单词边界包围 \\b :

import re

pattern = "\\bapple\\b"

phrase = "apple pineapple apples and apple."

print(re.sub(pattern, "orange", phrase))

输出:

orange pineapple apples and orange.
>>> 

注意 苹果 apple. 被替换为 orange orange. 但是 pineapple apples 保持不变。

3 年前
回复了 Paul M. 创建的主题 » 重复一个Python函数——执行次数与列表中的条目相同

如果我理解正确,你可以 zip ,甚至只是一个简单的基于范围的for循环:

for current_repo_id, current_repo_name, current_ips in zip(repo_id, repo_name, ip_list):
    x = sc.repositories.edit(
        repository_id=current_repo_id,
        name=current_repo_name,
        allowed_ips=current_ips
    )

我知道你可能更感兴趣的是找出你的特定方法不起作用的原因。然而,如果我了解你想要的行为,我也许能提供一个替代的解决方案。在发布我的答案后,我会看看你的尝试。

random.sample 让你取样 k 来自某个应用程序的项目数 population (收集、列表等等。)如果集合中没有重复元素,则保证随机样本中没有重复:

from random import sample

pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

num_samples = 4

print(sample(pool, k=num_samples))

可能的输出:

[9, 11, 8, 7]
>>> 

不管你运行这个片段多少次,你的随机样本中永远不会有重复的元素。这是因为 随机的样品 它不会生成随机对象,只是随机选取集合中已经存在的项目。例如,当你从一副卡片中随机抽取卡片,或者抽取彩票号码时,你会采用同样的方法。

就你而言, pool 是可以从中选择样本的唯一编号池。您想要的输出似乎是一个包含三个列表的列表,其中每个子列表中有两个样本。而不是打电话 随机的样品 三次,每个子列表一次,我们应该用 k=num_sublists * num_samples_per_sublist :

from random import sample

pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

num_sublists = 3
samples_per_sublist = 2

num_samples = num_sublists * samples_per_sublist

assert num_samples <= len(pool)

print(sample(pool, k=num_samples))

可能的输出:

[14, 10, 1, 8, 6, 3]
>>> 

好的,我们有六个样本,而不是四个。还没有子列表。现在,您可以简单地将这个包含六个样本的列表拆分为三个子列表,每个子列表包含两个样本:

from random import sample

pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

num_sublists = 3
samples_per_sublist = 2

num_samples = num_sublists * samples_per_sublist

assert num_samples <= len(pool)

def pairwise(iterable):
    yield from zip(*[iter(iterable)]*samples_per_sublist)

print(list(pairwise(sample(pool, num_samples))))

可能的输出:

[(4, 11), (12, 13), (8, 15)]
>>> 

或者如果你真的想要子列表,而不是元组:

def pairwise(iterable):
    yield from map(list, zip(*[iter(iterable)]*samples_per_sublist))

编辑——刚刚意识到你其实并不想要一个列表,而是一本字典。像这样的吗?抱歉,我痴迷于发电机,这真的不容易读:

keys = ["1stkey"]
subkeys = ["1stsubkey", "2ndsubkey"]
num_lists_per_subkey = 3
num_samples_per_list = 5
num_samples = num_lists_per_subkey * num_samples_per_list

min_sample = 1
max_sample = 50

pool = list(range(min_sample, max_sample + 1))

def generate_items():

    def generate_sub_items():
        from random import sample

        samples = sample(pool, k=num_samples)

        def generate_sub_sub_items():

            def chunkwise(iterable, n=num_samples_per_list):
                yield from map(list, zip(*[iter(iterable)]*n))
        
            for list_num, chunk in enumerate(chunkwise(samples), start=1):
                key = f"list{list_num}"
                yield key, chunk

        for subkey in subkeys:
            yield subkey, dict(generate_sub_sub_items())
    
    for key in keys:
        yield key, dict(generate_sub_items())

print(dict(generate_items()))

可能的输出:

{'1stkey': {'1stsubkey': {'list1': [43, 20, 4, 27, 2], 'list2': [49, 44, 18, 8, 37], 'list3': [19, 40, 9, 17, 6]}, '2ndsubkey': {'list1': [43, 20, 4, 27, 2], 'list2': [49, 44, 18, 8, 37], 'list3': [19, 40, 9, 17, 6]}}}
>>> 
4 年前
回复了 Paul M. 创建的主题 » 如何使用tkinter、eyed3和python编辑要更改的音频元数据?

好的,有几件事:

改变:

def change_artist():
    audio_file.tag.genre = artist_name
    audio_file.tag.save()
    return

def change_artist():
    audio_file.tag.album_artist = artist_name.get()
    audio_file.tag.save()
    return

然后,改变:

artist_entry = tkinter.Entry(root, textvariable="artist_name")

致:

artist_entry = tkinter.Entry(root, textvariable=artist_name)

告诉我进展如何。


编辑,我想试试。请换衣服 change_artist 功能是:

def change_artist():
    try:
        audio_file.tag.album_artist = artist_name.get()
        audio_file.tag.save()
    except AttributeError as error:
        print(type(audio_file), audio_file)
        print(type(audio_file.tag))
        raise error
    

让我知道打印了什么。


编辑,再试一次:

def change_artist():
    audio_file.initTag().album_artist = artist_name.get()
    audio_file.tag.save()

正如评论所指出的,你的 contains_alphabetic contains_numeric 函数不会做你认为它们正在做的事情,因为它们在第一次迭代中过早终止。启动一个循环,检查当前字符(在循环的第一次迭代中,它将是字符串的第一个字符),然后立即基于该单个字符从函数返回一些内容,这当然会终止循环和函数。

其他建议:没有必要从 curses .字符串已经有了 isalpha isdigit 谓词可用。此外,让函数接受字符串参数进行迭代可能是个好主意。

如果你想回来 True 如果字符串中的任何字符满足条件/谓词,以及 False 否则(如果所有字符都不满足条件),则以下将是一个有效的实现:

def contains_alpha(string):
    for char in string:
        if char.isalpha():
            return True # return True as soon as we see a character that satisfies the condition
    return False # Notice the indentation - we only return False if we managed to iterate over every character without returning True

或者:

def contains_alpha(string):
    found = False
    for char in string:
        if char.isalpha():
            found = True
            break
    return found

或者:

def contains_alpha(string):
    for char in string:
        if char.isalpha():
            break
    else: # Notice indentation - special for-else syntax: If we didn't break out of the loop, execute the else
        return False
    return True

或者:

def contains_alpha(string):
    return any(char.isalpha() for char in string)