Python Forum
[split] help me make this code better please (basic)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] help me make this code better please (basic)
#1
I'm just study Python and fall in very stupid situation
This code every time have different results, looks like if range() returns random result in range:
l = {str(i)+str(i-1) for i in range(20)}
print(l)
Output:
================= RESTART: D:\My Files\Documents\Python\new.py ================= {'10', '1817', '32', '109', '43', '76', '1413', '1110', '21', '1514', '65', '1716', '1918', '87', '1211', '54', '1312', '98', '0-1', '1615'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'65', '98', '1817', '1716', '1211', '10', '109', '1312', '76', '21', '87', '1110', '1918', '1413', '54', '1514', '43', '32', '0-1', '1615'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'1312', '76', '0-1', '98', '65', '1918', '1413', '21', '1817', '32', '87', '1110', '43', '1615', '1716', '109', '1211', '1514', '54', '10'} >>> ================= RESTART: D:\My Files\Documents\Python\new.py ================= {'1312', '87', '54', '21', '1211', '1716', '1918', '1514', '76', '43', '109', '1413', '0-1', '65', '1110', '1817', '32', '98', '1615', '10'} >>>
Reply
#2
what you create is set (i.e. that is because you use curly braces { and }). sets are unordered. That's why the elements are the same, but order in which they are print is different.
use brackets [ and ] to create list or ( and ) to create tuple.

Also don't use names like l, o - they are discouraged because it's easy to confuse with 1 and 0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-19-2020, 12:00 PM)buran Wrote: what you create is set (i.e. that is because you use curly braces { and }). sets are unordered. That's why the elements are the same, but order in which they are print is different.
use brackets [ and ] to create list or ( and ) to create tuple.

Also don't use names like l, o - they are discouraged because it's easy to confuse with 1 and 0

Thanks, I will consider your comments for the future
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] new to python hello world code gives errors giobastidas1970 1 1,460 Mar-11-2023, 12:48 PM
Last Post: jefsummers
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,710 May-06-2020, 05:10 PM
Last Post: riteshp
  How to make this dictionary-generating code more efficient? Mark17 4 2,385 Oct-08-2019, 07:42 PM
Last Post: Mark17
  Code: Creating a basic python game? searching1 5 3,436 Nov-12-2018, 05:18 AM
Last Post: searching1
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,495 Mar-05-2018, 03:48 PM
Last Post: mpd
  Basic code HGOBOI 2 2,765 Mar-05-2018, 12:02 PM
Last Post: Larz60+
  How to make faster this code Ace 1 2,973 Oct-23-2017, 12:11 PM
Last Post: Larz60+
  How can I make faster that code BlueEva00 1 2,698 Oct-18-2017, 03:51 PM
Last Post: ichabod801
  [split] Need help with lists to continue my assignment code cylandur 7 9,468 Oct-11-2016, 03:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020