模拟x次,并获取CSV文件

时间:2017-01-03 00:01:14

标签: python python-2.7

如何运行此代码1000次,并获得一个包含两列(l1,l2)和8行(ABCDEFGH)的csv文件,模拟中出现字母频率。

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor=preferences.edit();
  editor.putString("Name","Harneet");
  editor.commit();

1 个答案:

答案 0 :(得分:0)

我想学习随机数据,所以我提出了一个强力解决方案。 它应该让你对所需的步骤有所了解,这样你就可以尝试使它更加pythonic(我知道我会因为这个答案而被焚烧!)

import random
f = open('output.csv','w')

l1 = []
l2 = []

#create 1000 choices for l1 and l2

l = "ABCDEFGH"

for i in range (1000):
    l1.append(random.choice(l))
    l2.append(random.choice(l))

#count the number of times a letter was picked
L1={}
L2={}

for x in l1:
    if x not in L1:
        L1[x] = 1
    else:
        L1[x] += 1

for x in l2:
    if x not in L2:
        L2[x] = 1
    else:
        L2[x] += 1

#format data for output        
f.write((''+','+'l1'+','+'l2'+'\n'))
for x in list(l):
    f.write(x+',')
    val = str(L1[x])
    f.write(val+',')
    val2 = str(L2[x])
    f.write(val2+'\n')

f.close()

它在excel打开好了,但我不知道它是否适用于大熊猫等。