Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the tips for dealing with lists and dictionaries in PYTHON

Shulou Source: shulou.com Published: 2022-06-01 18:24:20 09月11日 Update

What are the tips for dealing with lists and dictionaries in PYTHON? for this question, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

# if statement in the line print "Hello" if True else "World" > Hello# rounding down print 5.0 World 2 > print 5 to the power of 2 > 3 floating point division print .3 / .1 > 2.9999999999999996print / .1 > 2.iteration of both lists nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] for teama, teambin zip (nfc) simultaneously Afc): print teama + "vs." + teamb > Packers vs. Ravens > 49ers vs. Patriots# indexed list iteration teams = ["Packers", "49ers", "Ravens", "Patriots"] for index, teamin enumerate (teams): printindex, team > 0 Packers > 1 49ers > > 2 Ravens > 3 Patriots# use the following instead of numbers = [1prit 2, 3, 4, 5) even = [number for numberin numbers if number%2 = 0] # dictionary derivation teams = [Packers "," 49ers "," Ravens " "Patriots"] print {key: value for value, keyin enumerate (teams)} > > {'49erslists: 1,' Ravens': 2, 'Patriots': 3,' Packers': 0} # initialize the values of the list items = [0] * 3print items >] # convert the list to the string teams = ["Packers", "49ers", "Ravens", "Patriots"] print "," .join (teams) > 'Packers, 49ers, Ravens " Patriots'# do not use the following way: data = {'user': 1,' name': 'Max',' three': 4} try: is_admin = data ['admin'] except KeyError: is_admin = False# replace data = {' user': 1, 'name':' Max', 'three': 4} is_admin = data.get (' admin' False) answers to questions about PYTHON's tips for handling lists and dictionaries are shared here. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Tags: Dictionaries questions techniques processing more help answers iterations easy easy two content at the same time characters strings guys buddies ways methods points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Redmi Shulou Technology MySQL