私信  •  关注

Arkistarvh Kltzuonstev

Arkistarvh Kltzuonstev 最近创建的主题
Arkistarvh Kltzuonstev 最近回复了
6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » Python中最接近的素数

即使我没有调试您的代码,以下代码也应该能够找到最接近的素数:

n = int(input("Enter n: "))

def chk_prime(n):
    if n>1:
        for i in range(2, n//2+1):
            if n%i==0:
                return False
                break
        else:
            return True
    else:
        return False

if chk_prime(n):
    print(f"{n} is itself a prime.")
else:
    count = 1
    while count<n:
        holder1 = n-count
        holder2 = n+count
        holder1_chk = chk_prime(holder1)
        holder2_chk = chk_prime(holder2)
        if holder1_chk and holder2_chk:
            print(f"closest primes are {holder1}, {holder2}")
            break
        elif holder1_chk and not holder2_chk:
            print(f"closest prime is {holder1}")
            break
        elif holder2_chk and not holder1_chk:
            print(f"closest prime is {holder2}")
            break
        else:
            count = count + 1

首先,我们定义了一个函数来检查一个数是否是素数。接下来我们开始 count = 1 count

6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » 如何在python 3中从嵌套列表中删除多个项?

你可以使用总是使用列表理解在单独的列表中删除所有要删除的单词,然后尝试以下操作:

>>> split_list =[["a","b","c"],["SUB","d","e",],["f","Billing"]]
>>> rem_word = ['SUB', 'Billing', 'Independent', 'DR']
>>> output = [[sub_itm for sub_itm in sub_list if sub_itm not in rem_word] for sub_list in split_list]
[['a', 'b', 'c'], ['d', 'e'], ['f']]

如果要在不理解列表的情况下执行此操作,则需要声明一个空列表以附加每个新的子列表,还需要声明一个新的空子列表以附加所有新的子项。检查这个:

output2 = []
for sub_list in split_list:
    new_sub_list = []
    for sub_itm in sub_list:
        if sub_itm not in rem_word:
            new_sub_list.append(sub_itm)
    output2.append(new_sub_list)

输出相同:

[['a', 'b', 'c'], ['d', 'e'], ['f']]
6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » 列表中实例数相同的python max

试试这个:

from collections import Counter
a = [('dog'),('dog'),('cat'),('cat'),('fish'),('frog'),('frog')]
ca = Counter(a).most_common()
print([i[0] for i in ca if i[1] == max([i[1] for i in ca])])
# Should print : ['dog', 'cat', 'frog']
6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » 在python中获取日期介于两个日期之间的周天数

试试这个:

start_date = '01-03-2019'   # Considering 03 is the month, 01 is the day
end_date = '15-03-2019'    # Considering 03 is the month, 15 is the day
start_date = [int(i) for i in start_date.split("-")]
end_date = [int(i) for i in end_date.split("-")]
days = 'monday' , 'tuesday'
from datetime import timedelta, date
start_date = date(start_date[-1], start_date[1], start_date[0])
end_date = date(end_date[-1], end_date[1], end_date[0])
# Now check in the range of start_date and end_date with condition .weekday() Monday is 0 and Tuesday is 1.
def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)
for single_date in daterange(start_date, end_date):
    if single_date.weekday() ==0:
        print("Monday : ", single_date)
    if single_date.weekday() == 1:
        print("Tuesday : ", single_date)
6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » 在Python中,如何将字符串中的数字作为单个元素提取到列表中?

试试这个:

list_ = ['25 birds, 1 cat, 4 dogs, 101 ants']
import re
list(map(int, re.findall('\d+', list_[0])))

产量 :

[25, 1, 4, 101]

另外,避免将变量名指定为 list .

6 年前
回复了 Arkistarvh Kltzuonstev 创建的主题 » python 3中列表中的特定模式字符串

另一个解决方案:

import re
exp = []
for i in ZTon:
    reg1 = re.search(r'.*?\--(.*)\--.*', i)
    reg2 = re.search(r'.*?\*(.*)\*.*', i)
    if reg1:
        exp.append(reg1[1])
    elif reg2:
        exp.append(reg2[1])

产量 :

[' and preferably only one ', 'right']