如何拆分字符串来创建嵌套列表?

时间:2018-01-19 21:52:46

标签: python string list dictionary

如何在文本文件中拆分这些字符串行并创建嵌套列表或字典?:

Ali/22/Bankacı
Ahmet/28/Kasap
Mehmet/31/Bakkal

这样您就拥有一个包含以下值的列表:

my_data['Ali'][1]= 'Bankacı'

哪种方法最为蟒蛇?

2 个答案:

答案 0 :(得分:3)

一种方法是这个:

FILE = 'file.txt'

result = {}
with open(FILE, encoding='utf-8') as my_file:
    for line in my_file:
        line = line.strip().split('/')
        result[line[0]] = line[1:]

print(result['Ali'][1])

或者,很好地放在一个函数中:

def data_to_dict(file_path):
    """
    Some docstring here which tells the user what the function does
    """

    result = {}
    with open(file_path, encoding='utf-8') as my_file:
        for line in my_file:
            line = line.strip().split('/')
            result[line[0]] = line[1:]
    return result


if __name__ == '__main__':
    d = data_to_dict('file.txt')
    print(d['Ali'][1])

我上面做的是:

  • 逐行读取文件(我从中删除了\n
  • 对于每一行我通过在/字符后分割字符串来创建列表(此时,一行将如下所示:['Ali', '22', 'Bankacı']
  • 使用列表中的第一个元素作为键,将列表的其余部分用作值

如果两个(或更多)列表中的第一个元素相同,则无法获得正确的结果(您将始终获得最后一个元素的值)一个)

如果您还想摆脱重复键问题,替代方案可能如下所示:

def data_to_dict(file_path):
    """
    Some docstring here which tells the user what the function does
    """

    result = {}
    with open(file_path, encoding='utf-8') as my_file:
        for i, line in enumerate(my_file):
            line = line.strip().split('/')
            key, val = line[0], line[1:]

            if key not in result:
                result[key] = val
            else:
                new_key = '{}_{}'.format(key, i)
                result[new_key] = val
    return result


if __name__ == '__main__':
    d = data_to_dict('file.txt')
    print(d)
    print(d['Ali'][1])

对于此文件:

Ali/22/Bankacı
Ali/28/Kasap
Mehmet/31/Bakkal

输出将如下所示:

>> {'Ali': ['22', 'Bankacı'], 'Ali_1': ['28', 'Kasap'], 'Mehmet': ['31', 'Bakkal']}
>> Bankacı

答案 1 :(得分:2)

通常我不会在没有尝试的情况下回答问题,但这可以通过单行{l[:l.index('/')]:l.split('/')[1:] for l in open('f.txt').read().split('\n')[:-1]} 轻松完成:

>>> d['Mehmet'][1]
'Bakkal'

然后允许以下列格式访问:

2d list

或者,您可以使用另一个comprehension创建[l.split('/') for l in open('f.txt').read().split('\n')[:-1]]

[['Ali', '22', 'Bankacı'], ['Ahmet', '28', 'Kasap'], ['Mehmet', '31', 'Bakkal']]

给出:

df.isgood=='True'
Out[420]: 
0     True
1    False
2    False
Name: isgood, dtype: bool