小编Ror*_*ton的帖子

如何在Python中将函数参数设置为字符串?

def count(string,letter):
    count=0
    for each in string:
        if each==letter:
            count=count+1
    print count
Run Code Online (Sandbox Code Playgroud)

有没有办法设置参数以自动接受字符串形式的输入,这样用户就不必添加引号?

python string parameters

2
推荐指数
2
解决办法
12万
查看次数

图灵机欧几里得求最大公约数的算法 python

我的代码在底部:(如果有人有足够的耐心阅读全部内容,我会感到非常惊讶)

它输出了错误的数字,但我不知道为什么。我遵循了算法。输入必须是 2 个一元数字,例如 2 和 6 => 01101111110。如果有更简单的方法来编写此算法,请告诉我。这是算法(来自罗杰·彭罗斯的《皇帝的新思想》一书)

在此输入图像描述

我的代码是:

x = input("Dos numeros en sistema unario separado por un 0: ")
x = list(x)
pos = 0
valorMaq = 0
while (True):
    if valorMaq==0 and x[pos]=='0':
        valorMaq=0
        x[pos]='0'
        pos=pos+1
        print(x)

    elif valorMaq==0 and x[pos]=='1':
        valorMaq=11
        x[pos]='0'
        pos=pos-1
        print(x)

    elif valorMaq==1 and x[pos]=='0':
        valorMaq=10
        x[pos]='1'
        pos=pos+1
        print(x)

    elif valorMaq==1 and x[pos]=='1':
        valorMaq=1
        x[pos]='1'
        pos=pos-1
        print(x)
############################################
    elif valorMaq==10 and x[pos]=='0':
        valorMaq=1010
        x[pos]='0'
        pos=pos+1
        print(x)

    elif valorMaq==10 and x[pos]=='1':
        valorMaq=11
        x[pos]='0' …
Run Code Online (Sandbox Code Playgroud)

python math

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

plotly:如何在窗口中制作独立图?

有没有什么办法可以像matplotlib一样使用plotly,也就是使绘图显示在弹出窗口中?例如,是否有一个简单的等价物

plt.plot([1,2,3], [2, 3, 2.5])
plt.show()
Run Code Online (Sandbox Code Playgroud)

我尝试了各种功能,但是它们似乎都可以创建html文件或图像文件。

python matplotlib plotly

2
推荐指数
3
解决办法
406
查看次数

如何为Tkinter的python中的按钮添加边框?

b1 = Button(tk, text=" ", font=("Consolas 30 bold"), height = 1, width = 4,bg = "white", command=lambda:checker(b1))
Run Code Online (Sandbox Code Playgroud)

我要添加什么代码来为按钮添加边框,使其变粗?

python tkinter button

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

使用 .encode 使用 utf8 读取行

我从文件中读取行,例如:

\n\n
\n

小大事:163 Wege zur Spitzenleistung (Dein Leben)(德语版)(彼得斯、汤姆)

\n\n

Die madlle Katastrophe: So f\xc3\xbchren Sie Teams \xc3\xbcber Distanz zur\n Spitzenleistung (德语版) (Thomas, Gary)

\n
\n\n

我用以下方法读取/编码它们:

\n\n
title = line.encode(\'utf8\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

但输出是:

\n\n
\n

b\'Die 美德灾难:所以 f\\xc3\\xbchren Sie Teams \\xc3\\xbcber\n Distanz zur Spitzenleistung(德语版)(托马斯、加里)\'

\n\n

b\'The Little Big Things: 163 Wege zur Spitzenleistung (Dein Leben)\n(德语版)(Peters,Tom)\'

\n
\n\n

为什么总是添加“b\'”?\n如何正确读取文件以便保留“元音变音”?

\n\n

这是完整的相关代码片段:

\n\n
# Parse the clippings.txt file\nlines = [line.strip() for line in codecs.open(config[\'CLIPPINGS_FILE\'], \'r\', \'utf-8-sig\')]\nfor line in lines:\n    line_count = line_count …
Run Code Online (Sandbox Code Playgroud)

python unicode utf-8

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

更改 tkinter 中的按钮文本 - 'NoneType' 对象不可下标

我试图让 Button1 在按下时将其上的文本从“hi”更改为“bye”,并在第二次按下时再次更改回来。

这是我的代码:

from tkinter import *

def toggletext():
  if Button1["text"] == "hi":
    Button1["text"] = "bye"
    Game.update()
  else:
    Button1["text"] = "hi"
    Game.update()

Game = Tk()
Game.wm_title("title")
Button1 = Button(text="hi",fg="white",bg="purple",width=2,height=1,command=toggletext).grid(row=0,column=0)
Button2 = Button(fg="white",bg="purple",width=2,height=1).grid(row=1,column=0)
Button3 = Button(fg="white",bg="purple",width=2,height=1).grid(row=0,column=1)
Button4 = Button(fg="white",bg="purple",width=2,height=1).grid(row=1,column=1)

Game.mainloop()
Run Code Online (Sandbox Code Playgroud)

当我按下 Button1 时出现此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\User1\Desktop\gridtest2.py", line 4, in toggletext
    if Button1["text"] == "hi":
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

python tkinter

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

如何在Matlab中计算Jaccard Index (|A?B|) / (|AUB|)

我正在尝试在 Matlab 中执行以下代码我有两组 A 和 B,其中 A={1,2,5,6,10} 和 B={2,3,5,7,9,11} 当我尝试下面的代码给出了一个错误:

A =[1,2,5,6,10]
B =[2,3,5,7,9,11]
C = union( A,B )
D = intersect(A,B)
Result = D/C
Run Code Online (Sandbox Code Playgroud)

这是错误

Error using  / 
Matrix dimensions must agree.
Error in code (line 6)
Result = D/C
Run Code Online (Sandbox Code Playgroud)

谁能帮我先计算|A?B|然后|AUB| 在 Matlab 中。最后,我将把 (|A?B|) / (|AUB|) 都分开。

math matlab

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

Python编程空白输出

while True:
    num=raw_input("Please enter a number.")
    if (num == 1):
        print "Sunday"
    elif (num==2):
        print "Monday"
    elif (num==3):
        print "Tuesday"
    elif (num==4):
        print "Wednesday"
    elif (num==5):
        print "Thursday"
    elif (num==6):
        print "Friday"
    elif (num==7):
        print "Saturday"
    else:
        print "Invalid Choice!"

    option = raw_input("Would you like to continue playing?")
    if (option=="yes"):
        continue
    elif (option=="no"):
        break
Run Code Online (Sandbox Code Playgroud)

这是我的代码.当我由于某种原因运行它时,第一部分(一周的那一天)的输出作为"其他"选项出现,即"无效选择".当我删除else语句时,输出只是空白.稍微混淆了为什么会发生这种情况.

python

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

求数值曲线的曲线方程

我有一个二维图,它看起来是二次的。我从通过计算以数字方式获得的数据集中得到它。我知道可以通过拟合数据集来获得方程。Python 似乎自动根据数据点进行拟合。我需要打印拟合曲线的方程。

我对不同的 X 求解 Y 并获得两个数组 Y 和 X。然后我将它们绘制出来

plt.plot(X,Y)

plt.xlabel('X')
plt.ylabel('Y')
plt.savefig('YvsX.png', format='png', dpi=1000)

plt.show()
Run Code Online (Sandbox Code Playgroud)

并得到这个:

在此输入图像描述

需要打印该图的方程

python curve-fitting

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