简体   繁体   English

无法弄清楚为什么此赋值代码会引发错误

[英]Can't figure out why this assignment code throws an error

NOTE: I have googled this and have read the many posts about the same thing but I'm just not understanding the explanations. 注意:我已经在Google上搜索了此书,并阅读了许多有关同一内容的文章,但我只是不理解这些解释。 I know the answer that string variables are immutable, but I can't understand how I can get the values and split them into separate variables without changing the contents of the variables I'm putting the data in. If someone could show me how to write this code in a legal way I'd greatly appreciate it. 我知道答案是字符串变量是不可变的,但是我不明白如何在不更改变量内容的情况下获取值并将其拆分为单独的变量。如果有人可以向我展示如何以合法的方式编写此代码,我将不胜感激。

I'm new to python with experience in a couple other languages. 我是python新手,具有其他几种语言的经验。 Code is at the end of the post. 代码在帖子末尾。

The file input.txt contains several lines like: 文件input.txt包含几行,例如:

50,100 50,100

20,110 20110

30,70 30,70

I am trying to open the file, read the lines into a list variable, then split each value into separate variables at the comma. 我正在尝试打开文件,将行读入列表变量,然后在逗号处将每个值拆分为单独的变量。

I get the error: 我得到错误:

TypeError: 'str' object does not support item assignment TypeError:'str'对象不支持项目分配

at the line that reads: degrees[i] = current[0] 在读取的行上:度[i] =当前[0]

I have spent a couple hours now reading posts about how strings are immutable but I'm just not understanding why this shouldn't work (or how to write it so it does work). 我现在已经花了几个小时阅读有关如何字符串不可变的文章,但是我只是不明白为什么它不起作用(或如何编写使其起作用)。 The variable degrees starts empty. 可变度数开始为空。 I don't know how else to create it in a way that I'm not changing anything or how to do this differently. 我不知道该如何以不改变任何内容的方式来创建它,或者如何以不同的方式进行操作。 I tried casting the values from current as int and putting them in the degrees variable and still get the TypeError. 我尝试将current中的值强制转换为int并将其放在degree变量中,但仍然得到TypeError。

I also don't understand why I need the line degrees = "" to begin with. 我也不明白为什么我需要线degrees = ""开始。 My understanding of python is you don't have to declare variables, you just make up a name and use it when you need it. 我对python的理解是,您不必声明变量,只需组成一个名称并在需要时使用它。 But if I leave out the line degrees = "" I get an error saying 但是,如果我忽略线degrees = ""则会出现错误提示

NameError: name 'degrees' is not defined NameError:未定义名称“学位”

but that would seem to mean that I somehow have to declare the variable before using it. 但这似乎意味着我必须以某种方式声明该变量,然后再使用它。

Thanks for any help. 谢谢你的帮助。

f = open('input.txt')

content_list = f.readlines()

f.close()

degrees=""

volume=""

for i in range(len(content_list)):

    content_list[i] = content_list[i].strip('\n')

    current = content_list[i].split(",")

    print(content_list[i])

    degrees[i] = current[0]

    volume[i] = current[1]

My understanding is that you want to store each column of your file in a different variable. 我的理解是,您想将文件的每一列存储在不同的变量中。 In Python, you can store data using many different variable types, but they do not all work the same way. 在Python中,您可以使用许多不同的变量类型存储数据,但是它们的工作方式并不完全相同。 As the TypeError points out, you cannot use item assignment with string-type variables. 正如TypeError指出的那样,您不能将项目分配与字符串类型的变量一起使用。 However you can perform operations on strings such as: 但是,您可以对字符串执行以下操作:

degrees = ''
string1 = 'abc'
degrees += string1

And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string. 然后,您可以在存储的字符串之间使用分隔符(例如,斜杠),以便能够标识每个字符串的开头和结尾。

string2 = 'def'
degrees += '/' + string2

Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. 将度数“”分配给度数仅表示度数是字符串类型的变量,不包含任何变量。 Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to. 此外,它定义了变量度数,以便以后再调用时,解释器就会知道您要引用的内容。

However it is way more convenient to use variable types which support item assignment, such as lists. 但是,使用支持项目分配的变量类型(例如列表)更为方便。 You can then use the append method to store new values at each iteration. 然后,您可以使用append方法在每次迭代时存储新值。

f = open('input.txt')
content_list = f.readlines()
f.close()
degrees = []
volume = []
for i in range(len(content_list)):
    content_list[i] = content_list[i].strip('\n')
    current = content_list[i].split(",")
    degrees.append(current[0])
    volume.append(current[1])

Python strings are immutable. Python字符串是不可变的。

This simply means that once you create a string in python, you can not change it. 这只是意味着一旦在python中创建了字符串,就无法更改它。

a = "Hello"
a[0] = 'J' # This is not allowed by python because you are updating contents of a string

I can see how this can be frustrating coming from other languages where this is supported. 我可以看到来自支持此功能的其他语言如何令人沮丧。 You can take a look at bytearrays in python which are mutable 您可以看一下python中可变的字节数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM