【问题标题】:How do I iterate this list to create a matrix?如何迭代此列表以创建矩阵?
【发布时间】:2018-10-05 05:21:24
【问题描述】:

在线课程中,部分练习是创建矩阵。

我们得到了以下数据:

KobeBryant_FT = [696,667,623,483,439,483,381,525,18,196]
JoeJohnson_FT = [261,235,316,299,220,195,158,132,159,141]
LeBronJames_FT = [601,489,549,594,593,503,387,403,439,375]
CarmeloAnthony_FT = [573,459,464,371,508,507,295,425,459,189]
DwightHoward_FT = [356,390,529,504,483,546,281,355,349,143]
ChrisBosh_FT = [474,463,472,504,470,384,229,241,223,179]
ChrisPaul_FT = [394,292,332,455,161,337,260,286,295,289]
KevinDurant_FT = [209,209,391,452,756,594,431,679,703,146]
DerrickRose_FT = [146,146,146,197,259,476,194,0,27,152]
DwayneWade_FT = [629,432,354,590,534,494,235,308,189,284]

为了创建一个矩阵,代码如下:

FT = np.array([KobeBryant_FT, JoeJohnson_FT, LeBronJames_FT, 
               CarmeloAnthony_FT, DwightHoward_FT, ChrisBosh_FT, ChrisPaul_FT,
               KevinDurant_FT, DerrickRose_FT, DwayneWade_FT])

我想知道是否有一种方法可以使用“for”循环来模拟上述代码的输出,而无需手动输入 KobeBryant_FT、JoeJohnson_FT、LeBronJames_FT 等。

【问题讨论】:

  • 你提到的解决方案很好。您不会在这里使用 for 循环,但如果您想尝试,请查看 np.append()np.vstack() 并遍历列表:for x in [KobeBryant_FT, JoeJohnson_FT, LeBronJames_FT, CarmeloAnthony_FT, DwightHoward_FT, ChrisBosh_FT, ChrisPaul_FT, KevinDurant_FT, DerrickRose_FT, DwayneWade_FT]:

标签: python arrays for-loop


【解决方案1】:

这取决于您将如何接收这些数据。如果它在 *.txt 文件中,那么您可以阅读它line by line(如果您想在列表中同时指定名称)或一次阅读所有内容,然后使用 regular expression 查找正方形内的任何内容括号。

import re

file = open("data.txt","r").read()

lists_txt = re.finditer('[\[].*[\]]$', file, flags=re.MULTILINE)
#item in lists_txt = anything found inside [] brackets.

for item in lists_txt:
    txt = str(item.group(0))
    print(txt[1:-1])

程序输出:

696,667,623,483,439,483,381,525,18,196
261,235,316,299,220,195,158,132,159,141
601,489,549,594,593,503,387,403,439,375
573,459,464,371,508,507,295,425,459,189
356,390,529,504,483,546,281,355,349,143
474,463,472,504,470,384,229,241,223,179
394,292,332,455,161,337,260,286,295,289
209,209,391,452,756,594,431,679,703,146
146,146,146,197,259,476,194,0,27,152
629,432,354,590,534,494,235,308,189,284

你应该得到 [] 内的任何东西的字符串。现在通过using .split(","),您可以将它们转换为列表。

希望对你有所帮助:)

【讨论】:

    【解决方案2】:

    试试这个,

    import pandas as pd
    df = pd.DataFrame(FT)
    

    【讨论】:

      【解决方案3】:

      创建列表时,可以将它们放入字典中,以便更轻松地引用它们:

      d = {"KB":[696,667,623,483,439,483,381,525,18,196], "JJ":[261,235,316,299,220,195,158,132,159,141]}
      np.stack(d.values())
      # Out[32]: 
      # array([[261, 235, 316, 299, 220, 195, 158, 132, 159, 141],
      #        [696, 667, 623, 483, 439, 483, 381, 525,  18, 196]])
      

      如果您希望数组按特定顺序排列,您可以使用 Ordereddict:

      from collections import OrderedDict
      d = OrderedDict({"KB":[696,667,623,483,439,483,381,525,18,196], "JJ":[261,235,316,299,220,195,158,132,159,141]})
      np.stack(d.values())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 2020-11-24
        • 1970-01-01
        相关资源
        最近更新 更多