简体   繁体   English

如何在元组列表中将字符串转换为整数?

[英]How to convert String to Int, in List of Tuples?

My list is like this and following is my code but it does not work.我的列表是这样的,以下是我的代码,但它不起作用。

 templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
 for i in range(len(tempList)):
     tempList[i][1] = int(float(tempList[i][1]))
     tempList[i][2] = int(float(tempList[i][2]))
     tempList[i][3] = int(float(tempList[i][3]))

Using list comprehension with unpacking:使用带解包的列表理解:

[(s, *map(float, fs)) for s, *fs in templist]

Output:输出:

[('375ml Bott 24', 6.0, 10.0, 60.0), ('CHINA WINE', 4.0, 16.0, 64.0)]

Explanation:解释:

  • for s, *fs in ... : for the elements of tuples in templist , take first item ( str ) as s and rest as fs . for s, *fs in ... :对于templist组的元素,将第一项( str )作为s其余作为fs
  • (s, *map(float, fs)) : keep the first item as it is, and convert fs into float then unpacking into a new tuple. (s, *map(float, fs)) :保持第一项不变,并将fs转换为float然后解包为一个新的元组。

You're trying to change a tuple but you can't since it is immutable.您正在尝试更改元组,但您不能更改,因为它是不可变的。 What you can do instead is convert to a list, change the values and convert back to a tuple:您可以做的是转换为列表,更改值并转换回元组:

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]

for i,tup in enumerate(templist):
     l = list(tup)
     l[1] = int(float(l[1]))
     l[2] = int(float(l[2]))
     l[3] = int(float(l[3]))
     templist[i] = tuple(l)

print(templist)

Output:输出:

[('375ml Bott 24', 6, 10, 60), ('CHINA WINE', 4, 16, 64)]

You could also use a list comprehension to do the same:你也可以使用列表理解来做同样的事情:

for i,tup in enumerate(templist):
     l = list(tup)
     l[1:] = [int(float(x)) for x in l[1:]]
     templist[i] = tuple(l)

try :尝试 :

tempList = [['375ml Bott 24', '6.0', '10.0', '60.0'], ['CHINA WINE', '4.0', '16.0', '64.0']]
for i in range(len(tempList)):
    tempList[i][1] = int(float(tempList[i][1]))
    tempList[i][2] = int(float(tempList[i][2]))
    tempList[i][3] = int(float(tempList[i][3]))
print(tempList)

You have a list of tuples.你有一个元组列表。 Elements of a list can be changed, it can have different tuple but tuple is immutable.列表的元素可以改变,它可以有不同的元组,但元组是不可变的。 you should have list of lists as mentioned below :你应该有如下提到的列表:

tempList = [['375ml Bott 24', '6.0', '10.0', '60.0'], ['CHINA WINE', '4.0', '16.0', '64.0']]


In this case :
templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]

tempList[0] = tempList[1] # can be done because it edit the list which is mutable
tempList[0][1] = tempList[1][1] # can't be done because it edit the tuple which is immutable

Do this: Convert the Tuple inside the List into Lists and convert string to Float.这样做:将列表中的元组转换为列表并将字符串转换为浮点数。

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
result =[]
for i in range(len(templist)):
    lis=list(templist[i])
    lis[1] = float(lis[1])
    lis[2] = float(lis[2])
    lis[3] = float(lis[3])
    result.append(lis)
print(result)

Your new list is result你的新名单是结果

We cant change the type inside a tuple but we can create a new tuple based on the old templist.我们不能改变元组内的类型,但我们可以基于旧的临时列表创建一个新的元组。 Try this:尝试这个:

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
newlist = []
for item in templist:
    z = []
    for i in item:
        try:
            z.append(int(float(i)))
        except:
            z.append(i)
    newlist.append(tuple(z))

print(newlist)

OUTPUT:输出:

[('375ml Bott 24', 6, 10, 60), ('CHINA WINE', 4, 16, 64)]

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

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