This question already has answers here:
Iterating over dictionaries using 'for' loops
(15 answers)
Closed 5 years ago.
I want to be able to print out just a single character from this dictionary but I haven't been able to figure out the syntax or find it anywhere. Is there a way to do this in vanilla Python 2.7.x given the code below?
dct = {"c":[["1","1","0"],["0","0","0"]], "d":[["1","1","0"],["1","0","0"]],}
for x in dct:
print [x][0][0]
I want the output to be: 11
Any help is much appreciated!
for x in dct:
print(dct[x][0][0])
Related
This question already has answers here:
python order of elements in set
(2 answers)
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 26 days ago.
I have a list a:
a={4941, 4980, 3855, 4763, 4955}
I convert into a list:
b=list(a)
b now becomes [4980, 4955, 4763, 4941, 3855]
Sorry am I missing something? Eventhough set is an unordered structure why should it change the order ? Or is it the list function that is doing this?
Sorry if this is too naive a question! Many thanks.
This question already has answers here:
Zip lists in Python
(10 answers)
Closed 2 years ago.
I have two variables
odds=[1,3,5]
evens=[2,4,6]
and i want to print them in a loop like this:
for i in range(0,2):
print(odds[i])
print(evens[i])
to get an output like
1,2,3,4,5,6.
However the above loop doesn't feel very pythonic. Is there way of doing this more pythonicly? perhaps with a loop thats defined via a line like "for odd in odd and for even in even:"?
for x,y in zip(odds,evens):
print(x,",",y)
This question already has answers here:
Python for-in loop preceded by a variable [duplicate]
(5 answers)
Explanation of how nested list comprehension works?
(11 answers)
Closed 4 years ago.
I am trying to figure out what the subsequent lines of python code actually do:
if (var1 and var1) in [ctl for key, value in list(uof.items()) for ctl, com in list(cd.items()) if com == 'spain']:
my_var= uof_map[var1 ]
I assume it executes some kind of the following logic:
for key, values in list(uof.items()):
for ctl, com in list(values.items()):
if com == 'spain':
But apparently the results do differ. Can someone please point me into the right direction?
Notes:
uof is a dictionary of dictionaries
value is a dictionary
This question already has answers here:
Python list sort in descending order
(6 answers)
Closed 5 years ago.
This is my code:
#这是一个有关旅行的程序
place=['北京天安门','西安兵马俑','香港游乐园','日本秋叶原']
print(place)
print(sorted(place))
print(place)
place.sorted(reverse=true)
print(place)
When I run my code, something Wrong happens.
place.sorted(reverse=true)
or
sorted(place)
Using the 2nd way, how can I give (reverse=true)?
Just use sorted(place, reverse=True).
This question already has answers here:
How are tuples unpacked in for loops?
(8 answers)
How can I add new keys to a dictionary?
(19 answers)
Closed 5 years ago.
Trying to relearn python, working with Introducing Python by Bill Lubanovic. While learning about dictionaries, he uses this for loop to reverse the dictionary e2f to f2e:
e2f = {'dog': 'chien', 'cat': 'chat', 'walrus': 'morse'}
f2e = {}
for english, french in e2f.items():
f2e[french] = english
I really don't understand these for loops, we never defined french or english, and I don't know how this process reverses the dictionary. If someone could explain this for loop it would be greatly appreciated!