Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python List Help
#1
Hello. I wrote the below code in Python 3. I am having trouble getting the \t out of my strings and making the items in the list stack vertically. Can not seem to figure this out. Please help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#Mardi Gras Event Schedule
#Written by Charles Gibson in Python 3
 
#Begin Application
 
#Text string to give user instructions for input
print("Madi Gras Event Schedule")
print()
print("Dates: 02/10/2018 - 02/13/2018")
print("Format Example mm/dd/yyyy")
print()
#opt function
def opt():
  eventDate = input("Please select a date")
  if (eventDate == "02/10/2018"):
    print(febTenArry)
  if (eventDate == "02/11/2018"):
    print(febElevenArry)
  if (eventDate == "02/12/2018"):
    print(febTwelveArry)
  if (eventDate == "02/13/2018"):
    print(febThirteenArry)
    
 
# Date variables with event information strings
febTenArry = [
"Krewe of Iris Uptown 1000am",
"Krewe of Tucks Uptown 1100am",
"Krewe of NOMTOC Westbank   1045am",
"Krewe of Endymnion Midtown 415pm",
"Krewe of Isis Metairie 630pm"
]
 
febElevenArry = [
"Krewe of Okeanos   Uptown 11:00am",
"Krewe of Mid-City  Uptown 11:45am",
"Krewe of Thoth Uptown 12:00pm",
"Krewe of Bacchus   Uptown 5:15pm",
"Corps de Napoleon  Metairie 5:00pm",
"Krewe of Athena    Metairie 5:30pm",
"Krewe of Pandora   Metairie 6:30pm"
]
 
febTwelveArry = [
"Krewe of Proteus   Uptown 5:15pm",
"Krewe of Orpheus   Uptown 6:00pm"
]
 
febThirteenArry = [
"Krewe of Zulu  Uptown 8:00am",
"Krewe of Rex   Uptown 10:00am",
"Krewe of Elks Orleans  Uptown 12:00pm",
"Krewe of Crescent City Uptown 2:00pm",
"Krewe of Argus Metairie 10:00am",
"Krewe of Elks Jefferson    Metairie 12:00pm",
"Krewe of Jefferson Metairie 2:00pm",
"Krewe of Lyra  Covington 10:00am"
]
#opt function called
opt() 
#while loop to allow user to enter dates multiple times before closing
while input != "exit":
  opt()
#End of Application
Output:
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux Madi Gras Event Schedule Dates: 02/10/2018 - 02/13/2018 Format Example mm/dd/yyyy Please select a date 02/13/2018 ['Krewe of Zulu\tUptown 8:00am', 'Krewe of Rex\tUptown 10:00am', 'Krewe of Elks Orleans\tUptown 12:00pm', 'Krewe of Crescent City\tUptown 2:00pm', 'Krewe of Argus\tMetairie 10:00am', 'Krewe of Elks Jefferson\tMetairie 12:00pm', 'Krewe of Jefferson\tMetairie 2:00pm', 'Krewe of Lyra\tCovington 10:00am'] Please select a date
Reply
#2
you can use replace, example
1
mystring = mystring.replace('\t', ' ')
Reply
#3
The tabs are in your string literals. Do a find and replace to get rid of them, or go to where they are in the string and manually delete them and replace them with a space.

To make the items in the list stack vertically, just do a loop:

1
2
for item in dateArry:
    print(item)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
You use strings to describe your events - which is rather inadequate structure.

You may use dict for an individual eveny - but I would use namedtuple for event records and dict - for event collection,

1
2
3
4
5
6
7
8
9
10
from collections import namedtuple
Event = namedtuple('Event', 'event location time')
 
mardi_gras_events = {            
    "02/10/2018":                                 
        [Event('Krewe of Iris', 'Uptown', '10:00am'),
         Event('Krewe of Tucks', 'Uptown', '11:00am'),
         ...],
    ....
}
(I personally would have used pandas - but that is a complex package, not recommended for newbies).

As for displaying - there's this tabulate package.

I have my own function that serves this purpose (well, it is rather involved)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

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