<i id='klUtr'><tr id='klUtr'><dt id='klUtr'><q id='klUtr'><span id='klUtr'><b id='klUtr'><form id='klUtr'><ins id='klUtr'></ins><ul id='klUtr'></ul><sub id='klUtr'></sub></form><legend id='klUtr'></legend><bdo id='klUtr'><pre id='klUtr'><center id='klUtr'></center></pre></bdo></b><th id='klUtr'></th></span></q></dt></tr></i><div id='klUtr'><tfoot id='klUtr'></tfoot><dl id='klUtr'><fieldset id='klUtr'></fieldset></dl></div>

    • <bdo id='klUtr'></bdo><ul id='klUtr'></ul>
      <tfoot id='klUtr'></tfoot>
    1. <legend id='klUtr'><style id='klUtr'><dir id='klUtr'><q id='klUtr'></q></dir></style></legend>

      <small id='klUtr'></small><noframes id='klUtr'>

    2. 如何将 intable 字符串列表转换为 int

      时间:2023-09-11
    3. <i id='K9pAw'><tr id='K9pAw'><dt id='K9pAw'><q id='K9pAw'><span id='K9pAw'><b id='K9pAw'><form id='K9pAw'><ins id='K9pAw'></ins><ul id='K9pAw'></ul><sub id='K9pAw'></sub></form><legend id='K9pAw'></legend><bdo id='K9pAw'><pre id='K9pAw'><center id='K9pAw'></center></pre></bdo></b><th id='K9pAw'></th></span></q></dt></tr></i><div id='K9pAw'><tfoot id='K9pAw'></tfoot><dl id='K9pAw'><fieldset id='K9pAw'></fieldset></dl></div>

        <legend id='K9pAw'><style id='K9pAw'><dir id='K9pAw'><q id='K9pAw'></q></dir></style></legend>
            <tbody id='K9pAw'></tbody>

          • <bdo id='K9pAw'></bdo><ul id='K9pAw'></ul>

            <small id='K9pAw'></small><noframes id='K9pAw'>

            1. <tfoot id='K9pAw'></tfoot>
                本文介绍了如何将 intable 字符串列表转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在 Python 中,我想转换一个字符串列表:

                l = ['sam','1','dad','21']

                并将整数转换为整数类型,如下所示:

                t = ['sam',1,'dad',21]

                我试过了:

                t = [map(int, x) for x in l]

                但显示错误.

                如何将列表中的所有 intable 字符串转换为 int,而将其他元素保留为字符串?

                我的列表可能是多维的.一种适用于通用列表的方法会更好:

                l=[['aa','2'],['bb','3']]

                解决方案

                我会使用自定义函数:

                def try_int(x):尝试:返回整数(x)除了ValueError:返回 x

                例子:

                >>>[try_int(x) for x in ['sam', '1', 'dad', '21']]['山姆',1,'爸爸',21]

                <小时>

                如果您需要将上述内容应用于列表列表,为什么在构建嵌套列表时不将这些字符串转换为 int?p>

                无论如何,如果需要,只是选择如何遍历这样的嵌套列表并应用上述方法.

                这样做的一种方法可能是:

                >>>list_of_lists = [['aa', '2'], ['bb', '3']]>>>[[try_int(x) for x in lst] for lst in list_of_lists][['aa', 2], ['bb', 3]]

                您显然可以将其重新分配给 list_of_lists:

                >>>list_of_lists = [[try_int(x) for x in lst] for lst in list_of_lists]

                In Python, I want to convert a list of strings:

                l = ['sam','1','dad','21']
                

                and convert the integers to integer types like this:

                t = ['sam',1,'dad',21]
                

                I tried:

                t = [map(int, x) for x in l]
                

                but is showing an error.

                How could I convert all intable strings in a list to int, leaving other elements as strings?

                My list might be multi-dimensional. A method which works for a generic list would be preferable:

                l=[['aa','2'],['bb','3']]

                解决方案

                I'd use a custom function:

                def try_int(x):
                    try:
                        return int(x)
                    except ValueError:
                        return x
                

                Example:

                >>> [try_int(x) for x in  ['sam', '1', 'dad', '21']]
                ['sam', 1, 'dad', 21]
                


                Edit: If you need to apply the above to a list of lists, why didn't you converted those strings to int while building the nested list?

                Anyway, if you need to, it's just a matter of choice on how to iterate over such nested list and apply the method above.

                One way for doing that, might be:

                >>> list_of_lists = [['aa', '2'], ['bb', '3']]
                >>> [[try_int(x) for x in lst] for lst in list_of_lists]
                [['aa', 2], ['bb', 3]]
                

                You can obviusly reassign that to list_of_lists:

                >>> list_of_lists = [[try_int(x) for x in lst] for lst in list_of_lists]
                

                这篇关于如何将 intable 字符串列表转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python - 包含字符串和整数的拆分列表 下一篇:如何在 Python 中检查字符串中是否包含数值?

                相关文章

                最新文章

                    • <bdo id='V2cM6'></bdo><ul id='V2cM6'></ul>
                    <i id='V2cM6'><tr id='V2cM6'><dt id='V2cM6'><q id='V2cM6'><span id='V2cM6'><b id='V2cM6'><form id='V2cM6'><ins id='V2cM6'></ins><ul id='V2cM6'></ul><sub id='V2cM6'></sub></form><legend id='V2cM6'></legend><bdo id='V2cM6'><pre id='V2cM6'><center id='V2cM6'></center></pre></bdo></b><th id='V2cM6'></th></span></q></dt></tr></i><div id='V2cM6'><tfoot id='V2cM6'></tfoot><dl id='V2cM6'><fieldset id='V2cM6'></fieldset></dl></div>

                    <tfoot id='V2cM6'></tfoot>
                  1. <small id='V2cM6'></small><noframes id='V2cM6'>

                  2. <legend id='V2cM6'><style id='V2cM6'><dir id='V2cM6'><q id='V2cM6'></q></dir></style></legend>