i spent 6 hours yesterday trying to figure out sorting in python.
i get that:
for k in sorted(djTime.items(), key=lambda x(-x[1], x[0])):
should take every instance of k in djTime and perform something on something else.
i've understood the same of
for line in sorted(djTime):
should take a logfile previously broken into lines by the rstrip('\n') method and see the items as lines
i understand *line* or *k* in any circumstance can be any variable (i.e.) for JohnDoe in sorted(djTime):
i don't understand how or what python is looking for to differentiate between items either in a list or in a dictionary
i believe if you say:
for k, v in sorted(djTime.items())
...and this is a tuple, the k is representative of the key, and the v is representative of the value? but i'm not really clear
do you have to specify 2 variables/objects to sort a tuple? is a single non nested dictionary always a tuple? (i.e. for k, v in sorted(djTime)
do you have to use .items? what if i want to sort by key? can i sort like this: for k, v in sorted(djItems.keys()) can i do it by values? : for k, v in sorted(djItems.values()
as for the remainder of the command up there : x(-x[1], x[0])
i have no idea what the first x is, but the second seems to me to denote the position in the tuple? where counting begins with 0 so x[1] is the second item and x[0] is the first item? kinda backwards?? and the - negative in front of the main -x[1] mean sort from bottom to top
what i'm trying to do here is sort sort ascending (most recent date and time first)
djDate... actually is empty right now, but
djTIme contains the date as a key and the time as a value
i want to take the 50 most current times (out of a list of 1000 dates and times)
as i've struggled with this i've found the 200 most current are actually on today's date, so i'm trying to make it simple and just find a way to sort the values(times) but now i'm realizing times from previous days could overlap so maybe i need to sort by date, then within that sort by time
im having a real hard time with sorting
sequence = {}
print djDate, "djDate"
print djTime.items(), "djTime"
for k, v in sorted(djTime, key=lambda x:(-x[1], x[0])):
print k, v, "\n"
counter = counter +1
sequence[counter] = djTime[k]
if counter == 50:
break
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] Traceback (most recent call last):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <module>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] for k, v in sorted(djTime.items(), key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <lambda>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] for k, v in sorted(djTime.items(), key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:16:15 2019] [client 109.234.112.250] TypeError: bad operand type for unary -: 'str', referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] Traceback (most recent call last):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <module>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] for k, v in sorted(djTime.items(), key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <lambda>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] for k, v in sorted(djTime.items(), key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:17:32 2019] [client 109.234.112.250] TypeError: bad operand type for unary -: 'str', referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] Traceback (most recent call last):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <module>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] for k, v in sorted(djTime, key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] File "/home/tdc484/tdc484s03/pu
blic_html/
project/sn
mpTrap.cgi
", line 110, in <lambda>, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] for k, v in sorted(djTime, key=lambda x:(-x[1], x[0])):, referer:
http://141.194.44.13:8008/~tdc484s03/project/
[Sun Mar 17 08:18:37 2019] [client 109.234.112.250] TypeError: bad operand type for unary -: 'str', referer:
http://141.194.44.13:8008/~tdc484s03/project/
ip addresses have been find/replaced
ASKER