如何更改元组列表中每个项目的数据类型

huangapple go评论117阅读模式
英文:

How to change datatype of each item in a list of tuples

问题

I've generated something that looks like this:

list = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0'),....]

I'd like to format this object so that Index 0 of each is a str, Index 1 of each is a str, and Index 2 is an int or a float. And have that propagate down the whole object so that it turns into this:

list = [("94028", "Evening", 2.23), ("94028", "Night", 1.16), ("94028", "Morning", 2.0),....]

How can I do this?

Tried:

for d in list:
  d[1] = int(d[1])

Doesn't work.

英文:

I've generated something that looks like this:

list = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0'),....

I'd like to format this object so that Index 0 of each is a str, Index 1 of each is a str, and Index 2 is an int or a float. And have that propagate down the whole object so that it turns into this:

list = [("94028", "Evening", 2.23), ("94028", "Night", 1.16), ("94028", "Morning", 2.0),....

How can I do this?

Tried:

for d in list:
  d[1] = int(d[1])

Doesn't work.

答案1

得分: 2

不能修改元组。它们是不可变的。你唯一能做的是创建一个新元组并将其赋值给旧元组,或者只需使用列表推导重新创建列表并根据需要编辑元素。

l = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0')]

l = [(el[0], el[1], float(el[2])) for el in l]
英文:

You can't modify tuples. They are immutable. Best you can do is create a new tuple and assign it in place of an old one or just recreate the list with comprehension editing elements as necessary.

l = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0')]

l = [(el[0], el[1], float(el[2])) for el in l]

答案2

得分: 1

你无法修改元组。但你可以创建新的元组并将其放在原来的位置:

for i, d in enumerate(list):
    list[i] = (*d[:2], float(d[2]))
英文:

You can't modify the tuples. But you can make new tuples and put them in their place:

for i, d in enumerate(list): 
    list[i] = (*d[:2], float(d[2]))

答案3

得分: 0

您的初始尝试使用循环来尝试修改现有元组,这是不可能的,因为元组是不可变的。相反,您需要创建一个带有所需修改的新列表。

original_list = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0')]

formatted_list = []

for item in original_list:
    formatted_item = (item[0], item[1], float(item[2]))
    formatted_list.append(formatted_item)

print(formatted_list)
英文:

Your original attempt used a loop to try to modify the existing tuples, which isn't possible because tuples are immutable. Instead, you need to create new list with the desired modifications.

original_list = [('94028', 'Evening', '2.23'), ('94028', 'Night', '1.16'), ('94028', 'Morning', '2.0')]

formatted_list = []

for item in original_list:
    formatted_item = (item[0], item[1], float(item[2]))
    formatted_list.append(formatted_item)

print(formatted_list)

huangapple
  • 本文由 发表于 2023年8月10日 20:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定