python: convert “5,4,2,4,1,0” into [[5, 4], [2, 4], [1, 0]]

后端 未结 11 1066
梦如初夏
梦如初夏 2020-11-28 15:33

Is there a \"straightforward\" way to convert a str containing numbers into a list of [x,y] ints?

# from: \'5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5\'
# to: [[5, 4         


        
相关标签:
11条回答
  • 2020-11-28 15:52

    First, use split to make a list of numbers (as in all of the other answers).

    num_list = num_str.split(",")
    

    Then, convert to integers:

    num_list = [int(i) for i in num_list]
    

    Then, use the itertools groupby recipe:

    from itertools import izip_longest
    def grouper(n, iterable, fillvalue=None):
       "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
       args = [iter(iterable)] * n
       return izip_longest(fillvalue=fillvalue, *args)
    
    pair_list = grouper(2, num_list)
    

    Of course, you can compress this into a single line if you're frugal:

    pair_list = grouper(2, [int(i) for i in num_str.split(",")]
    
    0 讨论(0)
  • 2020-11-28 15:54

    EDIT: @drewk cleaned this up to handle even or odd length lists:

    >>> f = '5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5'
    >>> li = [int(n) for n in f.split(',')]
    >>> [li[i:i+2] for i in range(0, len(li), 2)]
    [[5, 4], [2, 4], [1, 0], [3, 0], [5, 1], [3, 3], [14, 32], [3, 5], [7]]
    
    0 讨论(0)
  • 2020-11-28 15:55
    #declare the string of numbers
    str_nums = '5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5'
    
    #zip two lists: the even elements with the odd elements, casting the strings to integers
    zip([int(str_nums.split(',')[i]) for i in range(0,len(str_nums.split(',')),2)],[int(str_nums.split(',')[i]) for i in range(1,len(str_nums.split(',')),2)])
    
    """
    Of course you would want to clean this up with some intermediate variables, but one liners like this is why I love Python :)
    """
    
    0 讨论(0)
  • 2020-11-28 16:00
    >>> num_str = '5,4,2,4,1,0,3,0,5,1,3,3,4,3,3,5'
    >>> inums = iter([int(x) for x in num_str.split(',')])
    >>> [[x, inums.next()] for x in inums]
    [[5, 4], [2, 4], [1, 0], [3, 0], [5, 1], [3, 3], [4, 3], [3, 5]]
    >>>
    
    0 讨论(0)
  • 2020-11-28 16:05

    One option:

    >>> num_str = '5,4,2,4,1,0,3,0,5,1,3,3,4,3,3,5'
    >>> l = num_str.split(',')
    >>> zip(l[::2], l[1::2])
    [('5', '4'), ('2', '4'), ('1', '0'), ('3', '0'), ('5', '1'), ('3', '3'), ('4', '3'), ('3', '5')]
    

    Reference: str.split(), zip(), General information about sequence types and slicing

    If you actually want integers, you could convert the list to integers first using map:

    >>> l = map(int, num_str.split(','))
    

    Explanation:

    split creates a list of the single elements. The trick is the slicing: the syntax is list[start:end:step]. l[::2] will return every second element starting from the first one (so the first, third,...), whereas the second slice l[1::2] returns every second element from the second one (so the second, forth, ...).

    Update: If you really want lists, you could use map again on the result list:

    >>> xy_list = map(list, xy_list)
    

    Note that @Johnsyweb's answer is probably faster as it seems to not do any unnecessary iterations. But the actual difference depends of course on the size of the list.

    0 讨论(0)
提交回复
热议问题