簡體   English   中英

如何將 for 循環的結果轉換為列表?

[英]How can I turn the results of a for loop into a list?

如何更改此 for 循環以將其轉換為列表? 提前致謝。

for x in column_list:
    des, res=rp.ttest(group1= df[x][df['patients'] == 1], group1_name= "Patients",
         group2= df[x][df['patients'] == 0], group2_name= "Controls")
    res1=res.set_index('Independent t-test').T
    y=res1['Two side test p value = '].values
    if y < 0.005:
        print (x,y)

我記得你之前的問題。 我認為以下內容可能會有所幫助:

outputList = []
for x in column_list:
    des, res=rp.ttest(group1= df[x][df['patients'] == 1], group1_name= "Patients",
         group2= df[x][df['patients'] == 0], group2_name= "Controls")
    res1=res.set_index('Independent t-test').T
    y=res1['Two side test p value = '].values
    
    #collect the values into a dictionary and append that dictionary to the outputList
    outputList.append({"x":x, "y":y})

#Now loop the list and only print the ones with appropriate p-level
for pair in outputList:
    if pair["y"] < 0.005:
        print (pair["x"],pair["y"])

這是在將所有x,y對收集到列表中之后將print()向下移動到新的代碼部分。 使用字典會使這一切變得更干凈。

如果你想看看發生了什么,你可以在第二個for循環之前折騰print(outputList) ,看看 output 是什么樣子的。 它可能有助於連接點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM