【问题标题】:Split Mac address in a list python在列表python中拆分Mac地址
【发布时间】:2019-11-04 08:03:49
【问题描述】:

我需要提取存储在 python 列表中的所有 Mac 地址的最后两位数字。

['00:00:00:00:00:02',
 '00:00:00:00:00:01',
 '00:00:00:00:00:03',
 '00:00:00:00:00:01',
 '00:00:00:00:00:04',
 '00:00:00:00:00:01']

我需要从这个列表中提取 [02,01,03,01,04,01] 要么 熊猫数据框中有这个方法吗?

【问题讨论】:

  • [x.rsplit(':')[-1] for x in L][x[-2:] for x in L]

标签: python pandas list dataframe split


【解决方案1】:

这将完成你的工作

data=['00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04', '00:00:00:00:00:01']
last_digits=[mac_address.split(':')[-1] for mac_address in data]
print(last_digits)

【讨论】:

    【解决方案2】:

    使用rsplit作为最后一个分隔符并按位置查找最后一个值:

    L = ['00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03',
         '00:00:00:00:00:01', '00:00:00:00:00:04', '00:00:00:00:00:01']
    
    out = [x.rsplit(':', 1)[-1] for x in L]
    #alternative
    #out = [x.split(':')[-1] for x in L]
    

    或者通过索引获取每个字符串值的最后 2 个值:

    out = [x[-2:] for x in L]
    

    print (out)
    ['02', '01', '03', '01', '04', '01']
    

    【讨论】:

      【解决方案3】:
      macs = ['00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04', '00:00:00:00:00:01']
      short_macs = [x[-2:] for x in macs]
      

      输出

      ['02', '01', '03', '01', '04', '01']
      

      【讨论】:

        猜你喜欢
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多