按打印天数对大型列表进行排序

时间:2016-11-16 01:35:03

标签: python list count

在我的CSC课程中,我们必须用.txt文件中的数字和一周的参考日来形成一个列表。我可以将所有数字转换为数天,但是我在添加和计算每天打印的次数时遇到了麻烦,它的打印形式是

Thursday
Friday
Monday
Thursday

我需要它打印像

 Thursday = 6
 Friday = 11
 Saturday = 3
对于整个列表,这样的想法是什么样的? 继承我的代码

def dayofmurder(date):
date = date%10000
month = date//100
date= date %100
day=date
monthlist = [0,31,59,90,120,151,181,212,243,273,304,334]
daysofweek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
startonday = 4
startonday = monthlist[month-1]+(day-1)+startonday
startonday %= 7
return daysofweek[startonday]


file = open("C:\\Users\\bh1337\\Documents\\2015HomicideLog_FINAL.txt" , "r")
lines=file.readlines()[1:]
file.close()
for line in lines:
    value=line.split()


listdays=(dayofmurder(int(value[0])))
print(listdays)

.TXT文件

Date   Event #  TIME    Victim Name     V R/G   V Age
150101 0685 2:03    Anderson, Kedral    BM  26
150103 0816 5:57    Shines, Kathryn     WF  54
150106 4417 22:06   Norton, Noella      HF  46
150107 4655 23:27   Speidel, Steven     WM  41
150110 1100 8:35    Orozco, Jose        HM  53
140813 2059 14:53   Liu, Kim Chunng     AF  74
150112 3425 18:40   Primm, Rodney       BM  43
150115 3106 16:59   Lee, Wonjae     AM  26
150209 4737 23:35   Espinoza, Jose      HM  44
150213 2904 16:48   Eaddy, Obiche       BM  35
150214 3550 18:31   Solis, Rodolfo      HM  46
150212 4633 23:22   Meyers, Tammy       WF  43
150215 3763 21:26   Leyton, Jim     HM  16
150216 0575 4:46    Orozco, Miguel      HM  32
150222 1276 9:14    Ramos, Pedro        HM  38
150303 4149 22:13   Caldwell, Veronica  HF  33
150303 4149 22:13   Reyes, Yvonne       HF  18
150303 4149 22:13   Childers, Cory      BM  21
150311 4394 22:34   Lewis, Androples    BM  31
150317 0152 1:00    Remer, Jason        WM  28
150324 0352 2:24    Turner, Edward      WM  47
150325 3470 18:03   Vasquez, Carlos     HM  45
150403 3985 20:52   Sims, Angela        WF  45
150404 3427 17:55   Chism, Kendrick     BM  01
150404 3427 17:55   Braxton, Jermiaia   BF  03
150410 3797 20:31   Cooper, Derek       BM  28
150417 4546 22:17   Cassidy, Cassandra  WF  24
150419 2536 14:29   Thomas, Lakeitha    BF  26
150419 2536 14:29   Ball, Legatha       BF  28
150422 0275 2:10    Stepien, Gail       WF  51
150429 4435 21:26   Bugarin, Carlos     HM  31
150502 2397 12:47   Chavez, Norma       HF  47
150503 1040 4:21    Hawkins, DaShawn    BM  29
150505 2132 12:17   McQuade, Christine  WF  63
150506 1150 8:22    Fensch, Fred        WM  87
150507 1243 8:55    Banova, Giancarlo   WM  60
150507 3650 17:38   Cravish, Gary       WM  62
150507 3650 17:38   Cravish, Bonny      WF  56
150509 2883 15:49   Zarwood, Nickey     BM  24
150510 0867 3:44    Cain, Epri      BF  32
150511 2374 14:14   Collins, Jonathan   WM  39
150512 4618 21:38   Washington, Reginald    BM  55
150514 4411 22:58   Gutierrez, Christian    HM  24
150516 0122 0:36    Huff, Zachary       WM  21
150521 3549 17:56   Gonzales, Marissa   BF  25
150525 0476 2:29    Hernandez, Enrique  HM  43
150525 1045 7:11    Suri, Rahmond       BM  31
150529 4978 23:48   Najera, Richard     HM  24
150526 0748 5:09    Carrillo, Israel    HM  12
150607 2089 12:46   Martinez, Aaron     HM  22
150612 3929 20:25   Zimmerman, Jeremy   WM  45
150614 3397 18:39   Hanson, Allen       WM  57
150609 3705 11:02   Carter, Larry       BM  54
150624 1325 9:37    Carraway, Raytwan   BM  27
150624 1325 9:37    Jones, Rabon Terrance   BM  27
150627 4469 23:06   Keyes, Demario      BM  38
150630 1261 9:03    Pro, Willie     BM  33
150701 2518 13:23   Russell, Gregory    WM  49
150718 4524 22:49   Santillano, Daniel  HM  29
150619 0816 6:57    Carlisle, William   WM  78
150721 0981 7:52    Johnson, Charles    BM  38
150727 0692 5:12    Hall, Richard       WM  64
150729 1824 11:55   Luebeck, Mary       WF  84
150713 0462 2:54    Gravely Jerome      BM  38
150802 4168 22:20   Sanchez, Joel       HM  31
150731 3326 18:30   Flores-Guevara, Rene    HM  35
150805 3825 20:20   Jones, Josie        WF  36
150805 4087 21:26   Wright, Shakem      BM  19
150806 2182 13:26   Degroat, Chad       WM  37
150810 0762 6:00    Fritz, David        WM  52
150810 3440 18:50   Klungseth, Clayton  WM  31
150811 0608 5:07    Villafana-Ibarria, Luis HM  30
150811 0608 5:07    Avila-Estrada, Fernando HM  31
150813 2019 12:22   Dinunzio, Francine  WF  62
150815 3018 16:29   Garcia, Jairo       HM  28
150817 0122 0:46    Reyes, Hipolito     HM  32
150819 0234 1:42    Wales, John     WM  51
150819 1430 10:41   Elliot, Eugene      WM  72
150820 3080 17:25   Shilgevorkyan, Avetis   WM  57
150823 2957 17:38   Medrano, Steve      HM  35
150827 4688 22:37   Lamb, Jerelle       BM  30
150831 3292 18:21   Tipton, Todd        WM  50
150902 4458 22:59   Doe, Jane       WF  51
150420 0751 6:31    Hall, Marrie        BF  42
150905 0542 2:39    Garcia, Geovany     HM  17
150907 0008 0:02    Spiller, Iris       BF  35
150808 3317 18:09   Vance, Lisa     WF  47
150911 1672 12:20   Anderson, Aaron     BM  25
150911 1672 12:20   Bullard, Alton      BM  32
150914 3205 19:33   Zepeda, Joey        HM  23
150915 0293 2:23    Gibson, Kelvin      BM  27
150917 3346 20:50   Ortiz-Hernandez, Javier HM  34
150921 2138 14:09   Humphrey, Michael   WM  51
150922 0146 1:04    Patterson, Derrick  BM  46
150922 2008 13:15   Sanchez, Young      AF  71
150926 1368 9:20    Prather, Anthony    BM  41
150923 3894 19:53   Northcott, James    WM  72
151029 3502 19:48   Martorano, Paul     HM  43
151002 0896 7:09    Baglely, Jennifer   WF  39        
151002 0896 7:09    Donoso, Lex     WM  08           
151003 1599 10:33   Harvey, Chad        WM  38
151005 3001 17:07   Gonzalez, Gabriela  HF  41
151017 3670 20:27   Munns, Tony     BM  28
151023 0962 8:20    Green, Elizabeth    BF  48
151026 3840 21:30   Taylor-Mills, Tiyvone   BM  16
151028 3176 17:53   Schneider, Kristopher   WM  33
150712 2524 15:21   Schneider, Rita     WF  79
151112 1395 10:21   Houston, Michael    BM  37
151115 1607 23:50   Doe, John       BM  00  
151116 1716 12:25   Jimenez, Mario      HM  45
151116 3239 19:20   Colbert, Jamel      BM  32
151116 3239 19:20   Robinson, Shelby    WF  21  
151118 2319 14:04   Flores, Jimmy       HM  60
151118 3155 17:47   O'Neil, John        WM  52
151119 2682 16:16   Brown, William      BM  44
151120 3702 20:40   Butler, Terrance    BM  38
151121 1922 13:43   Nunez, Alexander    HM  35
151123 3284 18:52   Taylor, Dwayne      BM  39
151126 0471 4:13    Bankey, Henry       WM  58
151202 4480 22:31   Batts, David        BM  39
151205 1488 10:32   Larry, Dorothy      BF  69
151207 3187 17:50   Young, Morty        BM  37
151207 3187 17:50   Young, Charlita Lasunda BF  35
151207 3187 17:50   Leggett, Deandre    BM  38
151209 1586 10:15   Ryan, Camron        WM  27
151210 0594 5:01    Andebrehan, Yonas   BM  25
151216 1903 12:22   Johnson, Kyth       BM  27
151216 3487 19:14   Clay, Veronica      BF  46
151219 0833 6:50    Galvez-Valle, Jesse HM  38
151220 2947 18:37   Valenxula, Jessica  HF  32
151223 0572 4:59    McGee, Richard      WM  31
151223 1525 11:16   Adams, Marquice     BM  17
151223 2640 16:28   Kaname, Gary        HM  45
151224 3343 22:13   Cordova, David      HM  49

2 个答案:

答案 0 :(得分:0)

如果我理解你的话,我会这样做:

from datetime import datetime

daysofweek = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

with open('test.txt','r') as f:
    lines=f.readlines()[1:]

count = {}
for line in lines:
    value=line.split()
    date = datetime.strptime(value[0], '%y%m%d')
    day = daysofweek[date.weekday()]
    count[day] = count.get(day, 0) + 1

print(count)  

答案 1 :(得分:0)

使用字典跟踪您的日期。由于dayofmurder()返回特定日期,因此您可以使用字典来跟踪特定日期返回的时间,如下所示。

def dayofmurder(date):
    date = date%10000
    month = date//100
    date= date %100
    day=date
    monthlist = [0,31,59,90,120,151,181,212,243,273,304,334]
    daysofweek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    startonday = 4
    startonday = monthlist[month-1]+(day-1)+startonday
    startonday %= 7
    return daysofweek[startonday]


file = open("input.txt" , "r")
lines=file.readlines()[1:]
file.close()
dayOfmurders = {"Sunday": 0 ,"Monday": 0,"Tuesday": 0,"Wednesday": 0,"Thursday":0, "Friday": 0,"Saturday": 0}
for line in lines:
    value=line.split()
    listdays=(dayofmurder(int(value[0])))
    dayOfmurders[listdays] = dayOfmurders[listdays] + 1;

for k, v in dayOfmurders.items():
    print(k, '=', v)

这是你的输出

Sunday = 13
Wednesday = 26
Saturday = 19
Tuesday = 17
Friday = 14
Monday = 25
Thursday = 20