Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 如何删除'';围绕着我列表中的数字,而不是字符串_Python_String_Multidimensional Array_Indexing_Int - Fatal编程技术网

Python 如何删除'';围绕着我列表中的数字,而不是字符串

Python 如何删除'';围绕着我列表中的数字,而不是字符串,python,string,multidimensional-array,indexing,int,Python,String,Multidimensional Array,Indexing,Int,所以我有一个多维列表,其中包含字符串和整数。但是我需要按数字的递增顺序来组织列表。问题是我的数字周围有“”,例如'181'。我不想从字符串中删除“”,我只想从int中删除它 我的列表如下所示: [['"Detective Pikachu"', '104', 'PG'], ['"The Secret Life of Pets 2"', '86', 'PG'], ['"Deadpool 2"', '119', 'R'], ['"Godzilla: King of the Monsters"', '1

所以我有一个多维列表,其中包含字符串和整数。但是我需要按数字的递增顺序来组织列表。问题是我的数字周围有“”,例如
'181'
。我不想从字符串中删除“”,我只想从int中删除它

我的列表如下所示:

[['"Detective Pikachu"', '104', 'PG'], ['"The Secret Life of Pets 2"', '86', 'PG'], ['"Deadpool 2"', '119', 'R'], ['"Godzilla: King of the Monsters"', '132', 'PG-13
'], ['"Avengers: Endgame"', '181', 'PG-13'], ['"The Lion King(1994)"', '88', 'G']]
我只想要这个:

[['"Detective Pikachu"', 104, 'PG'], ['"The Secret Life of Pets 2"', 86, 'PG'], ['"Deadpool 2"', 119, 'R'], ['"Godzilla: King of the Monsters"', 132, 'PG-13
'], ['"Avengers: Endgame"', 181, 'PG-13'], ['"The Lion King(1994)"', 88, 'G']]

整数周围有引号,因为它们实际上不是整数,它们是字符串——因此,为了重申这个问题,您需要在可能的情况下将所有字符串转换为整数,而不使用其他字符串

我认为Python中没有内置的“maybe convert to int”函数,因此我首先制作一个:

def maybe_convert_to_int(value: str) -> Union[int, str]
    try:
        return int(value)
    except ValueError:
        return value
然后将该函数映射到每个电影列表:

[movie.map(maybe_convert_to_int) for movie in movie_list]

它是否始终是内部列表中的第二个元素,并且内部列表是否始终有三项长度?是的,int始终是内部列表中的第二项,并且每个内部列表都有三项长度。
[movie.map(maybe_convert_to_int) for movie in movie_list]