鱼C论坛

 找回密码
 立即注册
查看: 2291|回复: 6

[已解决]py文件idle打不开,求助

[复制链接]
发表于 2020-1-12 02:31:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

我想请问一下,我这个代码使用pycharm写的,在pycharm上也能跑。但是为什么用python3.7自带的idle却打不开
代码在附件,因为有一个csv文件,所以只能打包发 呜呜呜
最佳答案
2020-1-12 08:37:05
在 main.py 中的注释中,有些看起来像斜体的字母,在 idle 中不支持,我把他们替换后就可以打开了

  1. import random
  2. from copy import deepcopy
  3. from itertools import groupby
  4. from statistics import median

  5. # This function should have one parameter, a file name (including, if necessary, its path).
  6. # The function should read this CSV file and return a matrix (list of lists) in which a row
  7. # in the file is a row in the matrix. If the file has N rows, the matrix should have N
  8. # elements (each element is a list). Notice that in CSV files a comma separates columns
  9. # (CSV = comma separated values). You can assume the file will contain solely numeric values
  10. # (and commas, of course) with no quotes.
  11. def load_from_csv(fileName):
  12.     # Use list to save matrix
  13.     res = []
  14.     # Use a loop structure to read each line in the data file.
  15.     with open(fileName) as f:
  16.         lines = f.readlines()
  17.         for l in lines:
  18.             res.append(l.strip().split(','))
  19.     return res


  20. # This function should have two parameters, both of them lists. It should return the Manhattan
  21. # distance between the two lists. For details about this distance, read the appendix.
  22. def get_distance(l1, l2):
  23.     # Use a judgment structure to determine whether the length of list1 and list2 are the same
  24.     if len(l1) != len(l2):
  25.         print('Error! List1 and List2 are not the same length!')
  26.         return -1
  27.     else:
  28.         d = 0
  29.         # Use a loop structure look into each index of two lists
  30.         for i in range(len(l1)):
  31.             d += abs(l1[i] - l2[i])
  32.         return d


  33. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  34. # look into all the elements of the data matrix in this column number, and return the highest value.
  35. def get_max(m, c):
  36.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  37.     if c >= len(m[0]):
  38.         print('Error! The column number should be smaller than the number of columns in the matrix')
  39.         return -1
  40.     else:
  41.         # Loop through each row of a matrix using a loop structure
  42.         maxValue = m[0][c]
  43.         for r in m:
  44.             # Determine if the current value is greater than the maximum
  45.             if r[c] > maxValue:
  46.                 maxValue = r[c]
  47.         return maxValue

  48. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  49. # look into all the elements of the data matrix in this column number, and return the lowest value.
  50. def get_min(m, c):
  51.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  52.     if c >= len(m[0]):
  53.         print('Error! The column number should be smaller than the number of columns in the matrix')
  54.         return -1
  55.     else:
  56.         # Loop through each row of a matrix using a loop structure
  57.         minValue = m[0][c]
  58.         for r in m:
  59.             # Determines whether the current value is smaller than the minimum value
  60.             if r[c] < minValue:
  61.                 minValue = r[c]
  62.         return minValue

  63. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  64. # look into all the elements of the data matrix in this column number, and return the average of this column number.
  65. def get_mean(m, c):
  66.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  67.     if c >= len(m[0]):
  68.         print('Error! The column number should be smaller than the number of columns in the matrix')
  69.         return -1
  70.     else:
  71.         # Loop through each row of a matrix using a loop structure
  72.         mean = 0
  73.         for r in m:
  74.             mean += float(r[c])
  75.         return mean/len(m)

  76. # This function should take one parameter, a matrix (list of lists). It should return a matrix containing
  77. # the standardised version of the matrix passed as a parameter. This function should somehow use the get_max
  78. # and get_min functions. For details on how to standardise a matrix, read the appendix.
  79. def get_standardised_matrix(m):
  80.     maxList = [ get_max(m,c) for c in range(len(m[0]))]
  81.     minList = [ get_min(m,c) for c in range(len(m[0]))]
  82.     meanList = [ get_mean(m,c) for c in range(len(m[0]))]
  83.     lists = []
  84.     for r in m:
  85.         l = []
  86.         for c, i in enumerate(r):
  87.             l.append((float(i) - float(meanList[c]))/(float(maxList[c])-float(minList[c])))
  88.         lists.append(l)
  89.     return lists


  90. # This function should have two parameters: a matrix (list of lists), and a column number. It should return
  91. #  the median of the values in the data matrix at the column number passed as a parameter. Details about
  92. # median can be easily found online, eg. https://en.wikipedia.org/wiki/Median.
  93. def get_median(m, c):
  94.     l = [float(r[c]) for r in m]
  95.     return median(l)


  96. # This function should have three parameters: (i) a matrix (list of lists), (iii) the list S, (iii) the value
  97. # of K. This function should implement the Step 6 of the algorithm described in the appendix. It should return
  98. # a list containing K elements, c1, c2, ... , cK. Clearly, each of these elements is also a list.
  99. def get_centroids(m, S, K):
  100.     res = []
  101.     for i in range(K):
  102.         c = []
  103.         # select those rows that have been assigned to cluster k.
  104.         m_i = [x for index, x in enumerate(m) if S[index] == i]
  105.         # Each element j of ckshould be equal to the median of the column D'j
  106.         for j in range(len(m[0])):
  107.             c.append(get_median(m_i, j))
  108.         # Update ck.
  109.         res.append(c)
  110.     return res


  111. # This function should have two parameters: a data matrix (list of lists) and the number of groups to be
  112. # created (K). This function follows the algorithm described in the appendix. It should return a list
  113. # S (defined in the appendix). This function should use the other functions you wrote as much as possible.
  114. # Do not keep repeating code you already wrote.
  115. def get_groups(D, K):
  116.     S = []
  117.     # Select K different rows from the data matrix at random.
  118.     c_list = deepcopy(random.sample(D, K))
  119.     while True:
  120.         S_temp = []
  121.         for i in range(len(D)):
  122.             # Calculate the Manhattan distance between data row D'i and each of the lists c1, c2, ... , cK. cK
  123.             dist = get_distance(D[i], c_list[0])
  124.             s_i = 0
  125.             for index, c_i in enumerate(c_list):
  126.                 if get_distance(D[i], c_i) < dist:
  127.                     dist = get_distance(D[i], c_i)
  128.                     s_i = index
  129.             # Assign the row D'i to the cluster of the nearest c
  130.             S_temp.append(s_i)
  131.         #  If the previous step does not change S, stop
  132.         if S_temp == S:
  133.             break
  134.         else:
  135.             S = deepcopy(S_temp)
  136.         c_list = get_centroids(D, S, K)
  137.     return S


  138. def run_test():
  139.     # load data file
  140.     fileName = 'Data(1).csv'
  141.     m = load_from_csv(fileName)
  142.     # get standardised matrix
  143.     D = get_standardised_matrix(m)
  144.     res = []
  145.     # run experiments with K = 2, 3, 4, 5, 6.
  146.     for K in range(2,7):
  147.         res.append(get_groups(D, K))
  148.     # print the result
  149.     for K in range(2, 7):
  150.         print("="*100)
  151.         print("K = " + str(K) + ":")
  152.         print(res[K-2])
  153.         # show how many entities (wines) have been assigned to each group
  154.         for key, group in groupby(sorted(res[K-2])):
  155.             print("group " + str(key) + ": " + str(len(list(group))))
  156.                
  157. if __name__ == "__main__":
  158.     # test::load_from_csv(fileName)
  159.     # fileName = 'Data(1).csv'
  160.     # m = load_from_csv(fileName)

  161.     # # test::get_distance(l1, l2)
  162.     # l1 = [1,2,3]
  163.     # l2 = [4,5,6]
  164.     # print(get_distance(l1, l2))

  165.     # c = 1
  166.     # # test::get_max(m, c)
  167.     # maxValue = get_max(m, c)
  168.     # print(maxValue)

  169.     # # test::get_min(m, c)
  170.     # minValue = get_min(m, c)
  171.     # print(minValue)
  172.    
  173.     # # test::get_mean(m, c)
  174.     # meanValue = get_mean(m, c)
  175.     # print(meanValue)

  176.     # # test::get_standardised_matrix(m)
  177.     # D = get_standardised_matrix(m)
  178.     # print(D)

  179.     # # test::get_median(m, c)
  180.     # print(get_median(m, c))

  181.     # K = 2
  182.     # S = [0, 0, 0, 1, 1, 1]
  183.     # # test::get_centroids(m, S, K)
  184.     # c_k = get_centroids(m, S, K)
  185.     # print(c_k)

  186.     # # test::get_groups(D, K)
  187.     # S = get_groups(m, K)
  188.     # print(S)

  189.     # test::run_test()
  190.     run_test()
复制代码

49752 2.zip

8.7 KB, 下载次数: 3

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-1-12 02:54:47 | 显示全部楼层

回帖奖励 +1 鱼币

本帖最后由 Judie 于 2020-1-11 14:03 编辑

哇 我unpack出来有很多个file诶?
202001111355.PNG
哇...
202001111400.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-12 03:50:25 | 显示全部楼层
Judie 发表于 2020-1-12 02:54
哇 我unpack出来有很多个file诶?

哇...

我打开只有两个诶,就那个data和main,你能跑main.py的脚本吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-12 03:56:42 | 显示全部楼层
linkz 发表于 2020-1-11 14:50
我打开只有两个诶,就那个data和main,你能跑main.py的脚本吗?

不可以 但我用notepad++可以打开那个main
._main的那个打开就是那个specify file encoding页面 我点ok就说是fail什么的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-12 05:05:14 | 显示全部楼层
Judie 发表于 2020-1-12 03:56
不可以 但我用notepad++可以打开那个main
._main的那个打开就是那个specify file encoding页面 我点ok ...

我用pycharm也可以跑那个main,但是用idle就不能跑,是代码有问题吗。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-12 08:37:05 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +1 鱼币

在 main.py 中的注释中,有些看起来像斜体的字母,在 idle 中不支持,我把他们替换后就可以打开了

  1. import random
  2. from copy import deepcopy
  3. from itertools import groupby
  4. from statistics import median

  5. # This function should have one parameter, a file name (including, if necessary, its path).
  6. # The function should read this CSV file and return a matrix (list of lists) in which a row
  7. # in the file is a row in the matrix. If the file has N rows, the matrix should have N
  8. # elements (each element is a list). Notice that in CSV files a comma separates columns
  9. # (CSV = comma separated values). You can assume the file will contain solely numeric values
  10. # (and commas, of course) with no quotes.
  11. def load_from_csv(fileName):
  12.     # Use list to save matrix
  13.     res = []
  14.     # Use a loop structure to read each line in the data file.
  15.     with open(fileName) as f:
  16.         lines = f.readlines()
  17.         for l in lines:
  18.             res.append(l.strip().split(','))
  19.     return res


  20. # This function should have two parameters, both of them lists. It should return the Manhattan
  21. # distance between the two lists. For details about this distance, read the appendix.
  22. def get_distance(l1, l2):
  23.     # Use a judgment structure to determine whether the length of list1 and list2 are the same
  24.     if len(l1) != len(l2):
  25.         print('Error! List1 and List2 are not the same length!')
  26.         return -1
  27.     else:
  28.         d = 0
  29.         # Use a loop structure look into each index of two lists
  30.         for i in range(len(l1)):
  31.             d += abs(l1[i] - l2[i])
  32.         return d


  33. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  34. # look into all the elements of the data matrix in this column number, and return the highest value.
  35. def get_max(m, c):
  36.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  37.     if c >= len(m[0]):
  38.         print('Error! The column number should be smaller than the number of columns in the matrix')
  39.         return -1
  40.     else:
  41.         # Loop through each row of a matrix using a loop structure
  42.         maxValue = m[0][c]
  43.         for r in m:
  44.             # Determine if the current value is greater than the maximum
  45.             if r[c] > maxValue:
  46.                 maxValue = r[c]
  47.         return maxValue

  48. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  49. # look into all the elements of the data matrix in this column number, and return the lowest value.
  50. def get_min(m, c):
  51.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  52.     if c >= len(m[0]):
  53.         print('Error! The column number should be smaller than the number of columns in the matrix')
  54.         return -1
  55.     else:
  56.         # Loop through each row of a matrix using a loop structure
  57.         minValue = m[0][c]
  58.         for r in m:
  59.             # Determines whether the current value is smaller than the minimum value
  60.             if r[c] < minValue:
  61.                 minValue = r[c]
  62.         return minValue

  63. # This function should have two parameters, a matrix (list of lists) and a column number. It should
  64. # look into all the elements of the data matrix in this column number, and return the average of this column number.
  65. def get_mean(m, c):
  66.     # Use a judgment structure to determine if the column number is less than the number of columns in the matrix
  67.     if c >= len(m[0]):
  68.         print('Error! The column number should be smaller than the number of columns in the matrix')
  69.         return -1
  70.     else:
  71.         # Loop through each row of a matrix using a loop structure
  72.         mean = 0
  73.         for r in m:
  74.             mean += float(r[c])
  75.         return mean/len(m)

  76. # This function should take one parameter, a matrix (list of lists). It should return a matrix containing
  77. # the standardised version of the matrix passed as a parameter. This function should somehow use the get_max
  78. # and get_min functions. For details on how to standardise a matrix, read the appendix.
  79. def get_standardised_matrix(m):
  80.     maxList = [ get_max(m,c) for c in range(len(m[0]))]
  81.     minList = [ get_min(m,c) for c in range(len(m[0]))]
  82.     meanList = [ get_mean(m,c) for c in range(len(m[0]))]
  83.     lists = []
  84.     for r in m:
  85.         l = []
  86.         for c, i in enumerate(r):
  87.             l.append((float(i) - float(meanList[c]))/(float(maxList[c])-float(minList[c])))
  88.         lists.append(l)
  89.     return lists


  90. # This function should have two parameters: a matrix (list of lists), and a column number. It should return
  91. #  the median of the values in the data matrix at the column number passed as a parameter. Details about
  92. # median can be easily found online, eg. https://en.wikipedia.org/wiki/Median.
  93. def get_median(m, c):
  94.     l = [float(r[c]) for r in m]
  95.     return median(l)


  96. # This function should have three parameters: (i) a matrix (list of lists), (iii) the list S, (iii) the value
  97. # of K. This function should implement the Step 6 of the algorithm described in the appendix. It should return
  98. # a list containing K elements, c1, c2, ... , cK. Clearly, each of these elements is also a list.
  99. def get_centroids(m, S, K):
  100.     res = []
  101.     for i in range(K):
  102.         c = []
  103.         # select those rows that have been assigned to cluster k.
  104.         m_i = [x for index, x in enumerate(m) if S[index] == i]
  105.         # Each element j of ckshould be equal to the median of the column D'j
  106.         for j in range(len(m[0])):
  107.             c.append(get_median(m_i, j))
  108.         # Update ck.
  109.         res.append(c)
  110.     return res


  111. # This function should have two parameters: a data matrix (list of lists) and the number of groups to be
  112. # created (K). This function follows the algorithm described in the appendix. It should return a list
  113. # S (defined in the appendix). This function should use the other functions you wrote as much as possible.
  114. # Do not keep repeating code you already wrote.
  115. def get_groups(D, K):
  116.     S = []
  117.     # Select K different rows from the data matrix at random.
  118.     c_list = deepcopy(random.sample(D, K))
  119.     while True:
  120.         S_temp = []
  121.         for i in range(len(D)):
  122.             # Calculate the Manhattan distance between data row D'i and each of the lists c1, c2, ... , cK. cK
  123.             dist = get_distance(D[i], c_list[0])
  124.             s_i = 0
  125.             for index, c_i in enumerate(c_list):
  126.                 if get_distance(D[i], c_i) < dist:
  127.                     dist = get_distance(D[i], c_i)
  128.                     s_i = index
  129.             # Assign the row D'i to the cluster of the nearest c
  130.             S_temp.append(s_i)
  131.         #  If the previous step does not change S, stop
  132.         if S_temp == S:
  133.             break
  134.         else:
  135.             S = deepcopy(S_temp)
  136.         c_list = get_centroids(D, S, K)
  137.     return S


  138. def run_test():
  139.     # load data file
  140.     fileName = 'Data(1).csv'
  141.     m = load_from_csv(fileName)
  142.     # get standardised matrix
  143.     D = get_standardised_matrix(m)
  144.     res = []
  145.     # run experiments with K = 2, 3, 4, 5, 6.
  146.     for K in range(2,7):
  147.         res.append(get_groups(D, K))
  148.     # print the result
  149.     for K in range(2, 7):
  150.         print("="*100)
  151.         print("K = " + str(K) + ":")
  152.         print(res[K-2])
  153.         # show how many entities (wines) have been assigned to each group
  154.         for key, group in groupby(sorted(res[K-2])):
  155.             print("group " + str(key) + ": " + str(len(list(group))))
  156.                
  157. if __name__ == "__main__":
  158.     # test::load_from_csv(fileName)
  159.     # fileName = 'Data(1).csv'
  160.     # m = load_from_csv(fileName)

  161.     # # test::get_distance(l1, l2)
  162.     # l1 = [1,2,3]
  163.     # l2 = [4,5,6]
  164.     # print(get_distance(l1, l2))

  165.     # c = 1
  166.     # # test::get_max(m, c)
  167.     # maxValue = get_max(m, c)
  168.     # print(maxValue)

  169.     # # test::get_min(m, c)
  170.     # minValue = get_min(m, c)
  171.     # print(minValue)
  172.    
  173.     # # test::get_mean(m, c)
  174.     # meanValue = get_mean(m, c)
  175.     # print(meanValue)

  176.     # # test::get_standardised_matrix(m)
  177.     # D = get_standardised_matrix(m)
  178.     # print(D)

  179.     # # test::get_median(m, c)
  180.     # print(get_median(m, c))

  181.     # K = 2
  182.     # S = [0, 0, 0, 1, 1, 1]
  183.     # # test::get_centroids(m, S, K)
  184.     # c_k = get_centroids(m, S, K)
  185.     # print(c_k)

  186.     # # test::get_groups(D, K)
  187.     # S = get_groups(m, K)
  188.     # print(S)

  189.     # test::run_test()
  190.     run_test()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-21 10:25:10 | 显示全部楼层

回帖奖励 +1 鱼币

python能写.exe可执行程序吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-30 01:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表