Related
I have a list of names (say months) in a list. How can I create a dict with same value (say 0) without a comprehension if it is possible in some way?
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
What need is:
months_values = {'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 'May': 0, 'Jun': 0}
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
months_dict = dict.fromkeys(months,0)
month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
dict1=dict.fromkeys(months,0)
print(dict1)
This question already has answers here:
Python Regular Expressions to extract date
(5 answers)
Closed 4 years ago.
I want to extract a part of the string if the matching element is present in the list for example I have a string s and list l1:
s = 'Vipul Singh, Jun 24, 1995'
l1 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Now I want to extract the substring of string s from 'Jun 24, 1995' since 'Jun' is present in list l1.
So this is how I want my substring to be, I tried many regex, str functions but no result.
Note: I have many string of similar type as
vipul singh, Jan 1, 2017, 10:00,
ANI,May 6, 2009, 14:59 IST,
It looks like you just need to extract the dates, and since they share a common format, this is an easy problem for regular expressions.
Try using [a-zA-Z]{3}\s[0-9]{1,2},\s[0-9]{4}
s = """
Vipul Singh, Jun 24, 1995
vipul singh, Jan 1, 2017, 10:00,
ANI,May 6, 2009, 14:59 IST,
"""
import re
dates = re.findall(r'[a-zA-Z]{3}\s[0-9]{1,2},\s[0-9]{4}', s)
print(dates)
Output:
['Jun 24, 1995', 'Jan 1, 2017', 'May 6, 2009']
If you are concerned about matching something like 'ABC 23, 1111', you could only accept valid months as the first 3 letters:
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
dates = re.findall(r'(?:{})\s[0-9]{{1,2}},\s[0-9]{{4}}'.format('|'.join(months)), s)
I am trying to plot a chart with the 1st and 2nd columns of data as bars and then a line overlay for the 3rd column of data.
I have tried the following code but this creates 2 separate charts but I would like this all on one chart.
left_2013 = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
'2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]})
right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]})
right_2014_target = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
'2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]})
df_13_14 = pd.merge(left_2013, right_2014, how='outer')
df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer')
df_13_14_target[['month','2013_val','2014_val','2014_target_val']].head(12)
plt.figure()
df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o')
df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar')
This is what I currently get
The DataFrame plotting methods return a matplotlib AxesSubplot or list of AxesSubplots. (See the docs for plot, or boxplot, for instance.)
You can then pass that same Axes to the next plotting method (using ax=ax) to draw on the same axes:
ax = df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o')
df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar',
ax=ax)
import pandas as pd
import matplotlib.pyplot as plt
left_2013 = pd.DataFrame(
{'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep',
'oct', 'nov', 'dec'],
'2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]})
right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]})
right_2014_target = pd.DataFrame(
{'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep',
'oct', 'nov', 'dec'],
'2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]})
df_13_14 = pd.merge(left_2013, right_2014, how='outer')
df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer')
ax = df_13_14_target[['month', '2014_target_val']].plot(
x='month', linestyle='-', marker='o')
df_13_14_target[['month', '2013_val', '2014_val']].plot(x='month', kind='bar',
ax=ax)
plt.show()
Considering having this type of lists:
month_list = ['Mar', 'Aug', 'Okt', 'Nov']
value_for_each_month = [4, 10, 8, 5]
So, each value belongs to the month in the month_list, e.g. 'Mar' --> 4, 'Aug' --> 10 and so on..
Now, how to fill both lists in Python to achieve this result:
month_list_new = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
value_for_each_month_new = [0, 0, 4, 0, 0, 0, 0, 10, 0, 8, 5, 0]
Create a dictionary mapping month names to values...
>>> month_list = ['Mar', 'Aug', 'Okt', 'Nov']
>>> value_for_each_month = [4, 10, 8, 5]
>>> month_values = dict(zip(month_list, value_for_each_month))
>>> month_values
{'Aug': 10, 'Mar': 4, 'Nov': 5, 'Okt': 8}
... than use that dict in a list comprehension:
>>> month_list_new = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
>>> value_for_each_month_new = [month_values.get(m, 0) for m in month_list_new]
>>> value_for_each_month_new
[0, 0, 4, 0, 0, 0, 0, 10, 0, 0, 5, 0]
I would like to create a list that adds elements alternately from 2 seperate lists in python .
I had the following idea but it doesn't seem to work:
t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3= [len(t1)+len(t2)]
a = 0
while a < len(t1)+len(t2):
t3.extend(t1[a])
t3.extend(t2[a])
a = a + 1
print t3
So basically I would like ['Jan',31,'Feb',28,'Mar',31, ect...]
The shortest solution may be:
list(sum(zip(t2, t1), ()))
In Python you don't need to "reserve capacity" for a list. Just write
t3 = []
In fact, t3 = [len(t1)+len(t2)] doesn't even creates a list with length 24, but creates a list with a single entry [24].
t1[a] and t2[a] are elements you want to add to the list. To add an element, you use the .append method:
t3.append(t1[a])
t3.append(t2[a])
.extend is used to add a list (in fact, any iterable) to a list, e.g.
t3.extend([t1[a], t2[a]])
The problem itself can be solved easily using list comprehensions.
[a for l in zip(t2, t1) for a in l]
There are many other improvements could be made (e.g. use a for loop instead of a while loop). You could take it to http://codereview.stackexchange.com.
(BTW, this code does not handle leap year.)
Here you go:
t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3 = list()
for i, j in zip(t1, t2):
t3.append(i)
t3.append(j)
print(t3)
Just zip the lists and flatten the result.
>>> from itertools import chain
>>> t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
>>> t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
... 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
>>> list(chain(*zip(t2, t1)))
['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]
Without chain:
>>> [x for tup in zip(t2, t1) for x in tup]
['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]
you probably have to read more about python lists and their methods. t3= [len(t1)+len(t2)] this is not necessary at all. I guess you have a C background and trying to initialize the list with size. In python you don't have to initialize the list size (its auto increasing). And the items you have in a list are not stored as per the sequence you have entered them in. Please check tuple in python if you want your items to be in the same sequence.
Happy Coding
t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
arr = []
for i in range(12):
arr.append(t2[i])
arr.append(t1[i])
print(arr)
Output -
['Jan', 31, 'Feb', 28, 'Mar', 31, 'Apr', 30, 'May', 31, 'Jun', 30, 'Jul', 31, 'Aug', 31, 'Sept', 30, 'Oct', 31, 'Nov', 30, 'Dec', 31]
You can alternatively write -
import itertools
arr = list(itertools.chain.from_iterable(zip(t2, t1))
In Python, you can't create lists with a fixed length like you can do with arrays in other languages, so the third line should just be t3 = [].
Also, the extend() function is used to concatenate lists. To add a single new value, you need to use the append() function instead.
Python is dynamic programming language, the type of the identifier is determined when it is assigned value.
so basically you can do in this way:
t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
t2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
t3= []
for a in range(len(t1)):
append.append(t1[a])
apppend.append(t2[a])
print t3