我正在尝试将列表转换为数组并获取相同的数据格式问题。以下代码段:
f=open('crime_rates.csv','r')
data=f.read()
data_row = data.split('\n')
print(data_row[0:5])
['Albuquerque,749', 'Anaheim,371', 'Anchorage,828', 'Arlington,503', 'Atlanta,1379']
int_crime_rates=[]
for rows in data_row:
crime_rates=rows.split(',')
int_crime_rates.append(crime_rates)
print(int_crime_rates)
[['Albuquerque', '749'], ['Anaheim', '371'], ['Anchorage', '828'], ['Arlington', '503'], ['Atlanta', '1379']]
import numpy as np
arr=np.array(int_crime_rates)
print(arr)
[['Albuquerque' '749']
['Anaheim' '371']
['Anchorage' '828']
['Arlington' '503']
['Atlanta' '1379' ]]
元素之间缺少逗号,例如我期待以下结果集
[['Albuquerque', '749'],
['Anaheim', '371'],
['Anchorage', '828'],
['Arlington', '503'],
['Atlanta', '1379' ]]
删除逗号是什么?如何保留它?
答案 0 :(得分:0)
将其转换为列表: - )
newArr = list(map(list, arr))
newArr 应为 逗号列表。