I just had a quick question about the difference between [:1] and [0]
The following program parses a text document and puts all instances of email addresses in said txt document in a dictionary, and then creates a list of tuples from this dictionary, then sorts the list and prints out the email with the most instances. Please read the last 2 lines of code on this program:
emails = dict()
fname = input('Enter file name: ')
try:
fhand = open(fname)
except:
print("sorry can't do that")
quit()
for line in fhand:
if not line.startswith("From "):
continue
line = line.split()
emails[line[1]] = emails.get(line[1], 0) + 1
tmp = list()
for k, v in emails.items():
newtup = (v, k)
tmp.append(newtup)
tmp = sorted(tmp, reverse = True)
for v, k in tmp[:1]:
print(k, v)
Specifically, the last 2 lines of this program:
for v, k in tmp[:1]:
print(k, v)
Works perfectly. But when I tried to do the same thing with this syntax:
for v, k in tmp[0]:
print(k, v)
I get the following traceback:
TypeError: cannot unpack non-iterable int object
I guess that [0]
is in some way, not the same as [:1]
? Why does tmp[:1]
work but tmp[0]
doesn't in a for loop?
I have no complaints since my program is running perfectly with the tmp[:1]
syntax, but why doesn't it work with the tmp[0]
syntax? Are they not the same thing?
Thanks for your time and for reading this if you've come this far!
Emmm, here is an example.
>> tmp = [1, 2, 3]
>> tmp[:1]
[1]
>> tmp[0]
1
>> type([1])
list
>> type(1)
int
As you see, [:1]
and [0]
produces different stuff. [:1]
makes list, [0]
returns first element. Why you didn't try it just yourself?
Also made an example just for you:)
>> tmp = [(1,1),(2,2),(3,3)]
>> tmp[:1]
[(1, 1)]
>> tmp[0]
(1, 1)
>> for k,v in [(1, 1)]: pass
>> for k,v in (1, 1): pass
TypeError: cannot unpack non-iterable int object
Iteration like this does not make sence at all: for k,v in (1, 1):
.
You need to read about Python list indexing and slicing, as that is the difference between the two.
tmp[:1]:
This is list slicing syntax. This general form of this syntax is like this,
list_object[start_idx: end_index]
This returns a sliced list with elements starting from(including) start_idx and ending with end_idx(not inclusive, so one less than this value).
So in your question tmp[:1] returns a sublist(which is considered an iterable ) with elements at 0. Since this is an iterable it is acceptable to iterate over in a for loop.
tmp[0]:
This is list indexing syntax. The general form is,
list_object[element_idex]
This returns an element at that index. The returned value may/may not be an iterable (it depends on what the list is composed of). In your case, tmp[0] returns the first element of the list.
Ok. Makes sense.
>>> tmp = [1, 2, 3]
>>> tmp[0]
1
>>> tmp[:1]
[1]
where tmp[0] could be any object, like something that can't be iterated through, like in my case, a tuple.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.