第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將列表中的項(xiàng)目重新格式化為不同的類型和子列表

將列表中的項(xiàng)目重新格式化為不同的類型和子列表

阿晨1998 2021-08-02 17:11:08
UNCLEANED = [['1 -  32/', 'Highway', '403', '43.167233', '-80.275567', '1965', '2014', '2009', '4', 'Total=64  (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '', '72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9', ''],['1 -  43/', 'WEST', '403', '43.164531', '-80.251582', '1963', '2014', '2007', '4', 'Total=60.4  (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', ''],['2 -   4/', 'STOKES', '6', '45.036739', '-81.33579', '1958', '2013', '', '1', 'Total=16  (1)=16;', '18.4', '08/28/2013', '85.1', '85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '', '90.1', '']]上面是一個(gè)包含三個(gè)子列表的列表的未清理版本......我需要將它轉(zhuǎn)換成一個(gè)更清晰的版本,可能看起來像這樣:CLEANED = [[1, 'Highway', '403', 43.167233,              -80.275567, '1965', '2014', '2009', 4,              [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012',              [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]],             [2, 'WEST', '403', 43.164531, -80.251582,              '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0,               '04/13/2012', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3,                             73.3]],             [3, 'STOKES', '6', 45.036739, -81.33579, '1958',              '2013', '', 1, [16.0], 18.4, '08/28/2013',              [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]]            ]我認(rèn)為該模式用于未清理版本中的 index[0],我只保留第一個(gè)字符。index[1],[2]保持相同的,轉(zhuǎn)index[3]和[4]轉(zhuǎn)換成int .....然后到達(dá)index[9],我必須忽略總數(shù),只提取其余的數(shù)字,然后放入子列表中.....最后一件事是將日期之后的數(shù)字放入子列表中,同時(shí)排除第一個(gè)數(shù)字。我對(duì)如何不斷循環(huán)直到它完成“清理” UNCLEANED 中的所有內(nèi)容感到非常困惑?如果UNCLEANED不只是這三個(gè)元素呢?如果它很長(zhǎng),我將如何遍歷它?非常感謝您的幫助
查看完整描述

3 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是進(jìn)行上述轉(zhuǎn)換的解決方案。這是一個(gè)簡(jiǎn)單的for循環(huán):


UNCLEANED = [

['1 -  32/', 'Highway', '403', '43.167233',

 '-80.275567', '1965', '2014', '2009', '4',

 'Total=64  (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '',

 '72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9',

 ''],

['1 -  43/', 'WEST', '403', '43.164531', '-80.251582',

 '1963', '2014', '2007', '4',

 'Total=60.4  (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012',

 '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '',

 '70.3', '73.3', ''],

['2 -   4/', 'STOKES', '6', '45.036739', '-81.33579', '1958',

 '2013', '', '1', 'Total=16  (1)=16;', '18.4', '08/28/2013', '85.1',

 '85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '',

 '90.1', '']

]


# Function that performs the conversion described above.

def cleanElement(elem):

    elem[0] = elem[0].split(' - ')[0]

    elem[3] = float(elem[3])

    elem[4] = float(elem[4])


    elem[8] = int(elem[8])


    tempList = elem[9].split('  ')[1].split(';')

    tempList = [float(i.split('=')[1]) for i in tempList if not i=='']

    elem[9] = tempList


    elem[10] = float(elem[10])


    elem[13] = [float(i) for i in elem[13:] if not i=='']

    elem.pop(12)


    return elem[:13]


# Function that loops in the uncleaned list and performs the conversion for each element.

def cleanList(uncleaned):

    return [cleanElement(elem) for elem in uncleaned]


cleaned = cleanList(UNCLEANED)


for i in cleaned:

    print(i)

輸出:


['1', 'Highway', '403', 43.167233, -80.275567, '1965', '2014', '2009', 4, [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012', [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]]

['1', 'WEST', '403', 43.164531, -80.251582, '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0, '04/13/2012', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]

['2', 'STOKES', '6', 45.036739, -81.33579, '1958', '2013', '', 1, [16.0], 18.4, '08/28/2013', [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]]



查看完整回答
反對(duì) 回復(fù) 2021-08-03
?
三國(guó)紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是使用函數(shù)集合清理列表列表的另一種方法。


棘手的部分是對(duì)列表的最后一部分進(jìn)行切片,其中必須將交替字符串收集到數(shù)組中并過濾空字符串。


我假設(shè)每個(gè)子數(shù)組尾部的前 3 項(xiàng)中的非空字符串值是所需的值。arrange處理按返回一致值的順序放置前 3 個(gè)項(xiàng)目。


恕我直言,這種方式的優(yōu)點(diǎn)是,如果您想對(duì)任何特定項(xiàng)目做任何不同的事情,更改代碼會(huì)更容易。


import itertools as it


def get_first_char_int(item):

    first_char, *_ = item

    return int(first_char)


def identity(item):

    return item


def get_floats(item):

    tokens = ''.join(item.split(' ')[2:]).split('=')[1:]

    return [float(token.split(';')[0]) for token in tokens]


def get_float(item):

    return float(item) if item else item


UNCLEANED = [

    ['1 -  32/', 'Highway', '403', '43.167233',

     '-80.275567', '1965', '2014', '2009', '4',

     'Total=64  (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '',

     '72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9',

     ''],

    ['1 -  43/', 'WEST', '403', '43.164531', '-80.251582',

     '1963', '2014', '2007', '4',

     'Total=60.4  (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012',

     '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '',

     '70.3', '73.3', ''],

    ['2 -   4/', 'STOKES', '6', '45.036739', '-81.33579', '1958',

     '2013', '', '1', 'Total=16  (1)=16;', '18.4', '08/28/2013', '85.1',

     '85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '',

     '90.1', ''],

]


functions = [ # 1:1 mapping of functions to items in each list in UNCLEANED.

    get_first_char_int,

    identity,

    identity,

    float,

    float,

    identity,

    identity,

    identity,

    int,

    get_floats,

    float,

    identity,

]

end = len(functions)

item_length, = {len(items) for items in UNCLEANED}

# Calculate argument to pass to it.islice

extra_count = item_length - end

# Extend functions by extra_count times with get_float

functions.extend(list(it.repeat(get_float, extra_count)))

#

# Handle items up to start of alternating strings and empty strings.

head_results = (

    [f(item)

     for f, item

     in zip(functions[0:end], collection[0:end])]

    for collection in UNCLEANED

)


def arrange(items):

    """Handle varying order of first 3 items of items."""

    item, *_ = items

    items[0:3] = [item, '', item]

    return items

#

# Apply arrange to the tail of each sublist

collection_ = it.chain.from_iterable(arrange(collection[end:])

                                     for collection in UNCLEANED)

#

# Handle items starting with alternating strings and empty strings.

tail_results = (

    [f(item)

     for f, item

     in it.islice(zip(functions[end:], collection_), 2, item_length)]

    for collection in UNCLEANED

)


results = [[head, [item for item in tail if item]]

            for head, tail in zip(head_results, tail_results)]


for item in results:

    print(item)

輸出:


[[1, 'Highway', '403', 43.167233, -80.275567, '1965', '2014', '2009', 4, [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012'], [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]]

[[1, 'WEST', '403', 43.164531, -80.251582, '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0, '04/13/2012'], [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]

[[2, 'STOKES', '6', 45.036739, -81.33579, '1958', '2013', '', 1, [16.0], 18.4, '08/28/2013'], [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]]


查看完整回答
反對(duì) 回復(fù) 2021-08-03
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

創(chuàng)建一個(gè) clean_row(row) 函數(shù),然后所有的“清理規(guī)則”都應(yīng)該從這里調(diào)用。那你就可以了CLEANED = [clean_row(uncleaned) for uncleaned in UNCLEANED]


查看完整回答
反對(duì) 回復(fù) 2021-08-03
  • 3 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)