【问题标题】:Concatenate strings and integers in a list based on conditions根据条件连接列表中的字符串和整数
【发布时间】:2018-02-14 02:03:55
【问题描述】:

我正在处理一个包含字符串和整数的列表,我想创建一个函数,根据不同的条件将新元素连接到这些字符串和整数。例如,如果列表中的元素是一个整数,我想给它加 100;如果元素是一个字符串,我想添加“是名称”。我尝试使用列表理解,但无法弄清楚如何解释列表中都存在的字符串和整数(所以不确定这是否可行)。这是我正在使用的基本示例:

sample_list = ['buford', 1, 'henley', 2, 'emi', 3]

输出如下所示:

sample_list = ['buford is the name', 101, 'henley is the name', 102, 'emi is the name', 103]

我尝试使用这样的东西:

def concat_func():
    sample_list = ['buford', 1, 'henley', 2, 'emi', 3]
    [element + 100 for element in sample_list if type(element) == int]

我也尝试使用基本的 for 循环,但不确定这是否是正确的方法:

def concat_func():
    sample_list = ['buford', 1, 'henley', 2, 'emi', 3]
    for element in sample_list:
        if type(element) == str:
            element + " is the name"
        elif type(element) == int:
            element + 100
    return sample_list

【问题讨论】:

    标签: python list string-concatenation


    【解决方案1】:

    普通 LC:

    >>> ['{} is the name'.format(x) if isinstance(x,str) else x+100 for x in sample_list]
    ['buford is the name', 101, 'henley is the name', 102, 'emi is the name', 103]
    

    【讨论】:

      【解决方案2】:

      list comprehension 是一种方式:

      sample_list = ['buford', 1, 'henley', 2, 'emi', 3]
      
      result = [k+' is the name' if isinstance(k, str) \
                else k+100 if isinstance(k, int) \
                else k for k in sample_list]
      
      # ['buford is the name', 101, 'henley is the name', 102, 'emi is the name', 103]
      

      【讨论】:

        【解决方案3】:

        你很亲密。不要检查类型是否相等,而是使用'is'。您还可以按照 cmets 中的说明执行 isinstance() 来检查 str/int 的继承和子类。

        sample_list = ['buford', 1, 'henley', 2, 'emi', 3]
        newlist = []
        
        for s in sample_list:
            if type(s) is int:
                newlist.append(s + 100)
            elif type(s) is str:
                newlist.append(s + ' is the name')
            else:
                newlist.append(s)
        
        newlist2 = []
        
        for s in sample_list:
            if isinstance(s, int):
                newlist2.append(s + 100)
            elif isinstance(s, str):
                newlist2.append(s + ' is the name')
            else:
                newlist2.append(s)
        
        print(newlist)
        print(newlist2)
        

        【讨论】:

        • 最好使用isinstance
        【解决方案4】:

        只需更改 if 条件的位置并为其添加“else”条件即可。就像这样:

        [element + (100 if type(element) == int else " is the name") for element in sample_list]
        

        【讨论】:

          【解决方案5】:

          您可以创建一个映射dict,其中键作为映射,值作为需要连接的值

          >>> d = {'str':"is the name", "int": 100}
          

          接下来,您可以进行简单的列表理解,并对每个列表元素和映射字典中的值使用 + 运算符。您需要生成两个列表元素及其类型的元组。这可以使用zipmap 来实现

          >>> [k+d[t] for k,t in zip(l,map(lambda x: type(x).__name__,l))]
          >>> ['bufordis the name', 101, 'henleyis the name', 102, 'emiis the name', 103]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-11-28
            • 2015-06-25
            • 2021-08-20
            • 2022-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多