在Python中从CSV创建数组,同时限制使用的列

2024-04-19 15:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用以下格式的CSV文件

ST  1   2   3  4
WA  10  10  5  2
OR  0   7   3  9
CA  11  5   4  12
AZ  -999    0   0 11 

第一行表示第1-4天的#。我希望能够获取每个状态的数据,例如WA, 10, 10, 5, 2,并创建一个数组,其中只包含排序的行中的数字。如果我省略第一个索引WA,我可以使用。你知道吗

sorted(list, key=int) 

这样做会给我一个列表,[2,5,10,10]。你知道吗

我想做的是

  1. 阅读CSV的每一行。你知道吗
  2. 使用数字数据创建一个数字数组。你知道吗
  3. 使用数组运行一些计算(百分比秩)
  4. 将计算值与正确的状态字段相结合。例如,如果我想为WA的数组添加值3。你知道吗

    b.insert(list[4]), 3)
    

    得到

    [2,3,5,10,10] 
    

    所以我可以计算等级。(注意:我不能使用scipy,所以我必须使用我已经计算出的函数来计算排名。)

  5. 最后将State和rank值写入新的csv,类似于。你知道吗

    ST  Rank
    WA  30
    CA  26
    OR  55
    

    其中Rank是数组中给定值的秩。

我对python还很陌生,所以任何帮助或指导都将不胜感激。我还局限于使用基本的python模块

更新代码:

   with open(outputDir+"needy.csv", 'rb') as f:
   first = {row[0]: sorted(row[1:], key=int) for row in list(csv.reader(f))}

   for key, value in first.items():
        if addn in first:
            g= "yes"
            print key, addn, g
            #print d
        else:
            g= "no"
            print key, addn, g

        value.append(300)
        value.append(22)
        value = sorted(value, key=int)

        print "State:", key, value

当我这样做时,我附加的值将被优先添加,dict将被正确排序,但是当我将n定义为一个值时,它将不会被found。下面的例子。你知道吗

{'WA': ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5', '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11'}

如果我只是先打印出来,上面一行就是结果。 如果我使用for循环并将addn指定为11作为一个全局函数,我得到的结果是。你知道吗

WA 11 no
State: WA ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5',    '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11',..]

因为11是键的一部分,所以它应该返回yes等


Tags: csvkeyforvalue数字数组listint
1条回答
网友
1楼 · 发布于 2024-04-19 15:19:59

您可以使用简单的命令和字典来组织数据:

fid = open('out.txt')  # Just copy what you put in your question inside a file.
l = fid.readlines()  # Read the whole file into a list.
d = {}  # create a dictionary.
for i in l:
    s = i.split()  # split the list using spaces (default)
    d[s[0]] = [int(s[j]) for j in range(1,len(s))] # list comprehension to transform string into its for you number lists.

print(d)

,结果是:

{'CA': [11, 5, 4, 12], 'ST': [1, 2, 3, 4], 'OR': [0, 7, 3, 9], 'WA': [10, 10, 5, 2], 'AZ': [-999, 0, 0, 11]}

从这一点上,你可以做任何你想你的词条在字典中包括附加。你知道吗

 d['CA'].append(3)

编辑:@J.R.W.按照我推荐的方式建立字典,然后是你的代码(加上我给出的更正):

fid = open('out.txt')  # Just copy what you put in your question inside a file.
l = fid.readlines()  # Read the whole file into a list.
first = {}  # create a dictionary.
for i in l:
    s = i.split()  # split the list using spaces (default)
    first[s[0]] = [int(s[j]) for j in range(1,len(s))] # list comprehension to transform string into its for you number lists.

print(first)
addn = 11
for key, value in first.items():
    if addn in value:
        g= "yes"
        print(key, addn, g)
        #print d
    else:
        g= "no"
        print(key, addn, g)

    value.append(300)
    value.append(22)
    value = sorted(value, key=int)

    print("State:", key, value)

,结果是:

{'ST': [1, 2, 3, 4], 'CA': [11, 5, 4, 12], 'OR': [0, 7, 3, 9], 'AZ': [-999, 0, 0, 11], 'WA': [10, 10, 5, 2]}
ST 11 no
State: ST [1, 2, 3, 4, 22, 300]
CA 11 yes
State: CA [4, 5, 11, 12, 22, 300]
OR 11 no
State: OR [0, 3, 7, 9, 22, 300]
AZ 11 yes
State: AZ [-999, 0, 0, 11, 22, 300]
WA 11 no
State: WA [2, 5, 10, 10, 22, 300]

,当11存在时表示“是”(您自己的测试),当11不存在时表示“否”

相关问题 更多 >