Python AttributeError: 'tuple' 对象没有属性 'split'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31527693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:10:24  来源:igfitidea点击:

AttributeError: 'tuple' object has no attribute 'split'

pythonsplitchartuplesfrequency

提问by

I'm trying to write some Python to take a cipher and split it into the letters and their frequency in the cipher. The issue I'm having is that I can get the code to print the frequency of the letters in the cipher, but I'm trying to get them individually so I can turn them into percentages to compare them against the common values found in a book, for example. E.g. I could compare the count of E's in my cipher to the average value of 12.7% in the English language. The issue I'm having is when I try and split the list of letters and their frequency, I get the AttributeError: 'tuple' object has no attribute 'split'. Not sure what I can do so some help would be greatly appreciated. Here is my code: import collections import string

我正在尝试编写一些 Python 来获取密码并将其拆分为字母及其在密码中的频率。我遇到的问题是我可以获得代码来打印密码中字母的频率,但我试图单独获取它们,以便我可以将它们转换为百分比,以便将它们与在书,例如。例如,我可以将密码中 E 的计数与英语中 12.7% 的平均值进行比较。我遇到的问题是,当我尝试拆分字母列表及其频率时,出现 AttributeError: 'tuple' object has no attribute 'split'。不知道我能做什么,所以一些帮助将不胜感激。这是我的代码:导入集合导入字符串

def freq():
    info = input("File Name")
    filehandle = open(info, "r")
    data = filehandle.read().upper()
    char_counter = collections.Counter(data)
    for char, count in char_counter.most_common():
        if char in string.ascii_uppercase:
            print(char, count)
            s = (char, count)
            frequency = s.split(",")
            for freq in frequency:
                print(freq)

freq()

This is the direct copy of the shell

这是shell的直接拷贝

File Nametest.rtf
E 59
Traceback (most recent call last):
  File "/Users/x/Desktop/Frequency.py", line 17, in <module>
    freq()
  File "/Users/x/Desktop/Frequency.py", line 13, in freq
    frequency = s.split(",")
 AttributeError: 'tuple' object has no attribute 'split'

And here is what it was doing before:

这是它之前所做的:

File Nametest.rtf
E 59
A 43
T 39
R 37
O 29
F 25
P 25
S 23
L 23
I 22
N 19
D 18
B 17
C 15
H 14
M 12
G 9 
U 8
W 8
V 6
Y 4
X 3
K 3

Im looking for individual things of E 12% A 10% etc. or something similar if its possible.

如果可能的话,我正在寻找 E 12% A 10% 等的个别东西或类似的东西。

Any help would be greatly appreciated. Thanks a lot in advance :)

任何帮助将不胜感激。提前非常感谢:)

采纳答案by Geeocode

You should alter your code:

你应该改变你的代码:

def freq():
    info = input("File Name")
    filehandle = open(info, "r")
    data = filehandle.read().upper()
    char_counter = collections.Counter(data)
    for char, count in char_counter.most_common():
        if char in string.ascii_uppercase:
            print(char, count)
            frequency = (char + str(count))
            for freq in frequency:
                print(freq)

The fixed rows:

固定行:

    s = (char, count)
    frequency = s.split(",")

From so tiny information hard to tell what is the exact functionality of those rows, but the solution would be something like that.

从如此微小的信息中很难判断这些行的确切功能是什么,但解决方案就是这样。

回答by Javeed

You are making s a tuple, tuples do not have split as the comma is not an actual part of the tuple it's a way of delimiting the different entries in code. Split only works for some types, i.e strings, and would only work if a comma was a part of the string/type instance.

您正在制作 sa 元组,元组没有拆分,因为逗号不是元组的实际部分,它是分隔代码中不同条目的一种方式。拆分仅适用于某些类型,即字符串,并且仅当逗号是字符串/类型实例的一部分时才有效。

I am not sure what you intended to get out of that part of your code, but you are telling it to create a tuple (char, count), then split that tuple at the comma (not possible, even if it did happen, you would have an array [char, count]) then for each of the values in that split tuple, print the value, which would print char, count.

我不确定您打算从代码的那部分中得到什么,但是您告诉它创建一个元组(字符,计数),然后在逗号处拆分该元组(不可能,即使它确实发生了,您将有一个数组 [char, count]) 然后对于该拆分元组中的每个值,打印该值,这将打印 char, count。

Maybe go reexamine the way you went about this, you won't get any useful information from that split that you didn't already display in the prior print statement. If your intention was just the Letter and frequency, then you've already got that done, if you are trying to get proportions, add all the frequencies and then use each individual freq to get the proportion of the total it represents?

也许重新检查一下您处理此问题的方式,您不会从之前的打印语句中尚未显示的拆分中获得任何有用的信息。如果您的目的只是字母和频率,那么您已经完成了,如果您想获得比例,请将所有频率相加,然后使用每个单独的频率来获得它所代表的总数的比例?