简体   繁体   中英

How to print an element of a linked list in Python?

For example, say I have

node = {}
node['data'] = ['hi', '8']

newNode = {}
newNode['data'] = ['hello', '6']

and I want to compare the numbers 6 and 8 in node and newNode

if I try doing

print(node[1])

because the numbers are in position 1 of the lists I get an error that says:

KeyError: 1

您可以将它们进行比较:

node["data"][1] == newNode["data"][1]

By printing node[1] , you're actually searching for a key named 1 inside your node dictionary. Instead, since you named it 'data', use node['data'][1] . node['data'] refers to ['hi', '8'] . The following prints True of False for if 8 and 6 is the same.

node = {}
node['data'] = ['hi', '8']
# you can also create the dictionary by doing this:
# node = {'data' :  ['hi', '8']}
# or
# node = dict{'data' =  ['hi', '8']}

newNode = {}
newNode['data'] = ['hello', '6']

# so to compare:

print(node['data'][1]==newNode['data'][1])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM