'Creating a Tuple with a variable and Boolean Python
I'm supposed to add a variable and a boolean to a new Tuple - the actual assignment is below, with my code. I know tuples are immutable - this is the first time I've tried to make one. Additionally, I can't find anything about inserting the variable and a boolean. Thanks in advance!
My code is just created a new list. This is the desired result:
[('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]
Assignment:
The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.
import string
nums = string.digits
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
is_num = []
for item in chars:
if item in string.digits:
is_num.insert(item, bool)
elif item not in string.digits:
is_num.insert(item, bool)
Solution 1:[1]
the easiest way is that you should have casted the nums in to a list. i mean num =list(num) before parsing it in
import string
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
nums = string.digits
nums = list(nums)
is_num = []
for char in chars:
if char in nums:
is_num.append((char, True))
else:
is_num.append((char, False))
print(is_num)
Solution 2:[2]
From what I, by the "persian calendar" you mean, the months/lunar cycle in the persian calendar including the day and month names, correct?
This is what I believe you will have to do to get this to work.
- As you can see, Hugo uses the GoLang date-time system: https://gohugo.io/functions/format/#gos-layout-string
- which is here: https://golang.org/pkg/time/#ANSIC
- It doesn't seem this has a native Lunar calendar system (I call this Lunar calendar because I'm not used to the Persian calendar, but my own cultures, which is Lunar and different than the European system).
- But, this can be possibly added: https://pkg.go.dev/github.com/yaa110/go-persian-calendar
But, as this could be QUITE some work, it could be much simpler if you say EXACTLY what you are trying to do... other than (automatically convert html files to persian calendar).
The answer to that is, sure. Just Grep the string...
But I don't think that's what you are really looking for.
Best of luck!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Rogelio |
