[英]Two lists from list comprehension in python language
我有檔案
#Comment
Name1;Value1
Name2;Value2
我想讀取文件,第一行應該通過,然后在結果中有兩個列表。 其中一個只包含名稱,如(Name1,Name2),另一個只包含值,如(Value1,Value2)。
也許這里可以使用列表理解。 是否有可能從python語言中的一個list comprehension命令中獲取兩個列表?
謝謝。
您希望將數據從行轉換為列。 使用zip()
,然后使用csv
模塊讀取文件:
import csv
with open(filename, 'rb') as inputfile:
reader = csv.reader(inputfile, delimiter=';')
next(reader) # skip first line
names, values = zip(*reader)
不需要列表理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.