Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何将带字符串的列表转换为带int的列表?_Python_List - Fatal编程技术网

在python中,如何将带字符串的列表转换为带int的列表?

在python中,如何将带字符串的列表转换为带int的列表?,python,list,Python,List,我尝试了多种方法来转换这个,但都没有成功。 例如,我的列表是 testscores= [['John', '99', '87'], ['Tyler', '43', '64'], ['Billy', '74', '64']] 我只想将数字转换成整数,因为我最终将平均实际分数,并将名称保留在字符串中 我希望我的结果看起来像 testscores = [['John', 99, 87], ['Tyler', 43, 64], ['Billy', 74, 64]] 我尝试了很多for循环,只对这些列

我尝试了多种方法来转换这个,但都没有成功。 例如,我的列表是

testscores= [['John', '99', '87'], ['Tyler', '43', '64'], ['Billy', '74', '64']]
我只想将数字转换成整数,因为我最终将平均实际分数,并将名称保留在字符串中

我希望我的结果看起来像

testscores = [['John', 99, 87], ['Tyler', 43, 64], ['Billy', 74, 64]]
我尝试了很多for循环,只对这些列表中的数字进行整型,但都没有成功。如果你们中有人需要我的一些测试代码,我可以添加。
谢谢。

如果所有嵌套列表的长度均为3(即每个学生2分),则简单如下:

result = [[name, int(s1), int(s2)] for name, s1, s2 in testscores]

如果所有嵌套列表的长度均为3(即每个学生2分),则简单如下:

result = [[name, int(s1), int(s2)] for name, s1, s2 in testscores]

在Python 2中,对于任意长度的子列表:

In [1]: testscores = [['John', '99', '87'], ['Tyler', '43', '64'],
   ...: ['Billy', '74', '64']]

In [2]: [[l[0]] + map(int, l[1:]) for l in testscores]
Out[2]: [['John', 99, 87], ['Tyler', 43, 64], ['Billy', 74, 64]]
在Python 3(或2)中:


在Python 2中,对于任意长度的子列表:

In [1]: testscores = [['John', '99', '87'], ['Tyler', '43', '64'],
   ...: ['Billy', '74', '64']]

In [2]: [[l[0]] + map(int, l[1:]) for l in testscores]
Out[2]: [['John', 99, 87], ['Tyler', 43, 64], ['Billy', 74, 64]]
在Python 3(或2)中:


已经发布了一些解决方案,但这里是我在这方面的尝试,不依赖于
try
except

newScores = []
for personData in testScores:
    newScores.append([])
    for score in personData:
        if score.isdigit(): # assuming all of the scores are ints, and non-negative
            score = int(score)
        elif score[:1] == '-' and score[1:].isdigit(): # using colons to prevent index errors, this checks for negative ints for good measure
            score = int(score)
    newScores[-1].append(score)
testscores = newScores

在一个旁注下,我建议您考虑使用Python <代码> DICT< /Cord>结构,它允许您这样做:

testScores = {} # or = dict()
testScores["John"] = [99,87]

已经发布了一些解决方案,但这里是我在这方面的尝试,不依赖于
try
except

newScores = []
for personData in testScores:
    newScores.append([])
    for score in personData:
        if score.isdigit(): # assuming all of the scores are ints, and non-negative
            score = int(score)
        elif score[:1] == '-' and score[1:].isdigit(): # using colons to prevent index errors, this checks for negative ints for good measure
            score = int(score)
    newScores[-1].append(score)
testscores = newScores

在一个旁注下,我建议您考虑使用Python <代码> DICT< /Cord>结构,它允许您这样做:

testScores = {} # or = dict()
testScores["John"] = [99,87]

所有内部列表的大小都是3吗?不,不一定。我希望代码能够正常工作,不管我现在有多少个内部列表,以防以后会有更多的学生添加。我的意思是,不是列表的数量,而是列表本身的长度,总是像
[name,score1,score2]
?但是是的,每个内部列表中只有3个元素。但是,内部列表的数量可能会发生变化。所有内部列表的大小是否都是相同的3?不,不一定。我希望代码能够正常工作,不管我现在有多少个内部列表,以防以后会有更多的学生添加。我的意思是,不是列表的数量,而是列表本身的长度,总是像
[name,score1,score2]
?但是是的,每个内部列表中只有3个元素。但是,内部列表的数量可以更改。