社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

rdas

rdas 最近创建的主题
rdas 最近回复了

DictWriter.writerows 需要大量的字典。你只给了它一本字典,所以它试图展开它。

test_dict 在列表中

writer.writerows([test_dict])

或者使用 writerow

使用香草蟒蛇:

test=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

my_lists = [[], [], []]
for mod in range(3):
    my_lists[mod] = [elem for i, elem in enumerate(test) if i % 3 == mod]

list0 = my_lists[0]
list1 = my_lists[1]
list2 = my_lists[2]

print(list0, list1, list2)

输出:

['a', 'd', 'g']
['b', 'e', 'h']
['c', 'f', 'i']
6 年前
回复了 rdas 创建的主题 » 将列表中的字符串值拆分为单个值,python

不是一行,而是两行:

>>> l = ["a",
     "b,c",
     "d,e,f"]
>>> ll =[]
>>> [ll.extend(x.split(',')) for x in l]
[None, None, None]
>>> ll
['a', 'b', 'c', 'd', 'e', 'f']

累加器需要单独创建,因为 x.split(',') 无法在理解中解包。