小编Ann*_*Zen的帖子

从现有字典创建新字典

我想从旧字典(my_dict)创建一个新字典(new_dict),在 my_dict 的 new_dict 值将是键,键将是值。

预期输出:

{"name" : [1, 3, 5], "last_name": [2, 4, 6]}
Run Code Online (Sandbox Code Playgroud)

但是,我得到:

{"name" : [1, 2, 3, 4, 5, 6], "last_name": [1, 2, 3, 4, 5, 6]}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

def my_function(my_dict):
    # Creating new dictionary
    new_dict = dict.fromkeys(set(my_dict.values()), [])

    for key, value in my_dict.items():
        new_dict[value].append(key)

    return new_dict

# Existing dictionary
my_dict = {
    1: "name",
    2: "last_name",
    3: "name",
    4: "last_name",
    5: "name",
    6: "last_name"}
res = my_function(my_dict)
print(res)
Run Code Online (Sandbox Code Playgroud)

python dictionary

2
推荐指数
1
解决办法
268
查看次数

在Python中将大数除以10给出错误的结果

假设我想删除一个数字的最后一位数字n。为此,我使用代码int(n/10)

遗憾的是,这对于大量数据给出了错误的结果。例如n = 4474630975855204960除以10给出447463097585520512

这种行为的原因是什么?我该如何修复它?

python division

2
推荐指数
1
解决办法
1911
查看次数

Python递归函数不返回

我正在尝试编写一个递归函数,该函数返回单词在排序单词列表中的位置,或者None在找不到单词时返回。以下是代码:

def partition(t,kw,upper,lower):
    if ((upper-lower) > 1):
        pivot = lower + (upper-lower)//2
        print("pivot is now {}".format(pivot))
        if (kw >= t[pivot]):
            lower = pivot
            print("searching on the right",upper,lower)
            return(partition(t,kw,upper,lower))
        
        elif (kw <= t[pivot-1]):
            upper = pivot-1
            print("searching on the left",upper,lower)
            return(partition(t,kw,upper,lower))

        else:
            return None 
            #that means the keyword is between t[pivot-1] and t[pivot]
            #which means it doesnt exist in the list
    if (upper - lower) <= 1:
        if (kw == t[upper]):
            print("found element {} at upper bound {}".format(kw,upper))
            return(upper) …
Run Code Online (Sandbox Code Playgroud)

python recursion

2
推荐指数
1
解决办法
82
查看次数

无法使用 pytesseract.image_to_string 从 Image 读取文本

这就是验证码的样子。

这里的问题是我需要删除行并编写代码来识别字符。到目前为止,我已经看到了解决方案,其中 char 是实心的,但它具有带双边框的 char。

python captcha opencv python-imaging-library python-tesseract

2
推荐指数
1
解决办法
183
查看次数

如何从列表创建 x,y 对

我有一个包含k元素的列表。
我想用x,y对形成另一个列表,其中y值是x值右侧索引中的元素。

例如:

我有一个包含 4 个元素的列表:4, 8, 7, 1
我需要用这样的对创建一个对列表:
(4, 8), (4, 7), (4, 1), (8, 7), (8, 1) (7, 1)

我在这里使用 python 是我的代码:

list1 = list(map(int,input().strip().split()))[:k]
list2 = [(val,val1) for val in person1 for val1 in person2[1:]]
Run Code Online (Sandbox Code Playgroud)

python algorithm list

1
推荐指数
1
解决办法
158
查看次数

如何在python中加入枚举值?

如何在python中加入枚举值?

我正在制作这个密码生成器,但我无法加入值s来在这个基本上是字典的“秘密语言”中制作单词!


代码:

secret = input("What text do you want to generate as a  secret code? ")
secretlang = {
  'a':'m',
  'b':'t',
  'c':'i',
  ...
}
for i, v in enumerate(secret): 
  print(secretlang[v])
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
167
查看次数

如何计算 BigInt 的百分比?

在我的代码中,我有一个敌对对象,它有一个current_hpmax_hp。两者都是 类型BigInt。我希望将其转换为百分比,但在range(0, 100). 我怎样才能有效地做到这一点?

BigInt除法四舍五入为整数,所以我不能使用分数并做hp/max_hp.

javascript bigint

1
推荐指数
1
解决办法
263
查看次数

python以python方式编辑字符串元组列表

mysql 已输出形式的元组列表

a=[("1","2","3"),("4","5","6"),("7","8","9")]
Run Code Online (Sandbox Code Playgroud)

我想要一个表单元组列表

c=[("2","3"),("5","6"),("8","9")]
Run Code Online (Sandbox Code Playgroud)

即每个元组的最后两个元素,因此我可以将其放入我的 sql executemany 命令并获取下一条数据。

我的直觉是我可以使用命令获得所需的矩阵

c=[for b in a: b[1:]]
Run Code Online (Sandbox Code Playgroud)

但是蟒蛇,不喜欢那样。为什么这不起作用,获得所需元组列表的最pythonic方法是什么?

python list

1
推荐指数
1
解决办法
68
查看次数

有没有办法让函数从同一个变量中获得两次随机数

我正在尝试制作一个骰子,您可以询问您是否要生成一个新的随机数(这个想法来自https://knightlab.northwestern.edu/2014/06/05/five-mini-programming-projects- for-the-python-beginner/ )

第一次运行有效,但不会生成新数字,但会打印相同的数字。有没有办法做到这一点?

我的代码:

import random
number = random.randint(1,6)
def rollDie():
    print(number)
    answer = input("Do you want to roll the die again? Answer with Y or N")
    if answer == "Y":
        rollDie()
    elif answer == "N":
        print("bye!")

rollDie()
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
112
查看次数

如何根据列表绘制列表列表?

x = [2000,2001,2002,2003]
y = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
for i in range(len(y[0])):
    plt.plot(x,[pt[i] for pt in y])
plt.show()
Run Code Online (Sandbox Code Playgroud)

我得到一个ValueError4, 3。我知道这一点,x而且y必须是平等的。我以为len(y[0])会工作。

对于 中的每个子列表y,我想生成一行,其x值对应于2000, 2001, 2002, 2003

python matplotlib valueerror

0
推荐指数
1
解决办法
113
查看次数