I have strings in multiple lines, but my requirement is to have those strings in one list
['arn:aws:iam::****:saml/Prod']
['arn:aws:iam::***:saml/Test']
['arn:aws:iam::*****:saml/Dev']
Into one list, My expected output is
['arn:aws:iam::****:saml/Prod', 'arn:aws:iam::***:saml/Test', 'arn:aws:iam::*****:saml/Dev']
into one list.
How can I do this? appreciate your help
Assuming "my_list" really is just a list of lists that each include a single string, your easiest answer is a list comprehension:
In [14]: my_list = [
...: ['arn:aws:iam::****:saml/Prod'],
...: ['arn:aws:iam::***:saml/Test'],
...: ['arn:aws:iam::*****:saml/Dev']
...: ]
In [15]: new_list = [item[0] for item in my_list]
In [16]: new_list
Out[16]:
['arn:aws:iam::****:saml/Prod',
'arn:aws:iam::***:saml/Test',
'arn:aws:iam::*****:saml/Dev']
But, to echo the first comment, there could potentially be more context here that might require more dynamic handling, etc.
does someone know why pandas behave differently when column which we use as BY in GROUPBY contains only 1 unique value? Specifically, if there is just 1 value and we return pandas.Series, returned output is basically transposed in comparison to multiple unique values:
dt = pd.date_range('2021-01-01', '2021-01-02 23:00', closed=None, freq='1H')
df = pd.DataFrame({'date':dt.date, 'vals': range(dt.shape[0])}, index=dt)
dt1 = pd.date_range('2021-01-01', '2021-01-01 23:00', closed=None, freq='1H')
df2 = pd.DataFrame({'date':dt1.date, 'vals': range(dt1.shape[0])}, index=dt1)
def f(row, ):
return row['vals']
print(df.groupby('date').apply(f).shape)
print(df2.groupby('date').apply(f).shape)
[out 1] (48,)
[out 2] (1, 24)
Is there some simple parameter I can use to make sure the behavior is consistent? Would it make sense to maybe sumbit it as bug-report due to inconsistency, or is it "expected" (I undestood from previous question that sometimes poor design or small part is not a bug)? (I still love pandas, just these small things can make their usage very painful)
squeeze()
DataFrame.squeeze() and Series.squeeze() can make the shapes consistent:
>>> df.groupby('date').apply(f).squeeze().shape
(48,)
>>> df2.groupby('date').apply(f).squeeze().shape
(24,)
squeeze=True (deprecated)
groupby() has a squeeze param:
squeeze: Reduce the dimensionality of the return type if possible, otherwise return a consistent type.
>>> df.groupby('date', squeeze=True).apply(f).shape
(48,)
>>> df2.groupby('date', squeeze=True).apply(f).shape
(24,)
This has been deprecated since pandas 1.1.0 and will be removed in the future.
I have a list of list like this data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]] I want to get lists having the same 2nd index. i tried this code but i got 9 lists when i just need 3 lists.
data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]]
for i in data:
a=[]
for j in data:
if (i[1]==j[1]):
a.append(j)
print(a)
i expected to get ["date1","a",14,15],["date2","a",14,15],["date3","a",14,15]
["date1","b",14,15],["date2","b",14,15],["date3","b",14,15]
["date1","c",14,15],["date2","c",14,15],["date3","c",14,15]
data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]]
from itertools import groupby
from operator import itemgetter
print(
[list(v) for k,v in groupby(sorted(data, key=itemgetter(1)), key=itemgetter(1))]
)
In order for groupby to work the data has to be sorted.
Depending on your use case, the list instantiation of the iterator might not be needed. Added it to see proper output instead of <itertools._grouper... >
i'm very new to python so please forgive me if my error is blatantly obvious.
The issue I am having is with line 15. I am struggling to check if the input value is NOT in the list "list"
All values currently entered (in and not in the list "list") will return the response "srry".
I know line 15 is at fault because if I create a separate list with no nested lists for the countries: England, French and Mandarin to be called just for line 15, the appropriate response based on input is printed as expected.
Any help would be highly appreciative.
#input
lang = str(input("Please Type in the Language you speak and press enter
('make sure you use a capital letter')"))
#list of countries
list = [["English", "english"], ["French", "french"], ["Mandarin",
"mandarin"]]
#list of responses
lls = ["Hello,", "Bonjour,", "Ni Hao"]
srry = "Sorry, but I don't speak that"
welcmsg = "Welcome to Coventry"
# check if input is not in list
if str(lang) not in list:
print(srry)
#provide appropriate response based on input
elif str(lang) in list[0]:
print(lls[0] + " " + welcmsg)
elif str(lang) in list[1]:
print(lls[1] + " " +welcmsg)
elif str(lang) in list[2]:
print(lls[2])
TLDR; change line 15 to search the list-of-lists:
# check if input is not in list
if lang not in [item for sublist in mylist for item in sublist]:
print(srry)
Details:
The problem is that "list" is a nested-list (a list-of-lists), and so when we check if the input value is not in the list, we're checking if the value is only in the outer-most level of the list, which are still lists.
Consider the following:
list of lists:
>>> mylist = [['a', 'b'], ['x', 'y']]
>>> 'x' in mylist
False
vs. a single-level, flat list:
>>> mylist = ['a', 'b', 'x', 'y']
>>> 'x' in mylist
True
To fix it we can either convert the initial list-of-lists to one "flat" list (single-level), or we'll need to search each sub-list within the "list".
Keeping the code as close to the original and only changing the line 15 list search:
# check if input is not in list
if lang not in [item for sublist in mylist for item in sublist]:
print(srry)
Also, as mentioned in the comment - consider using a different variable name for the "list" that doesn't overwrite the built-in keyword list to prevent unexpected behavior. Here I've used mylist for the "list" name.
Another thing you might consider is using str.lower() when taking user input:
>>> mylist = ["english", "french", "mandarin"]
>>> lang = str(input("language? ").lower())
language? French
>>> lang in mylist
True
The user can still use a capital-letter for the input, but it makes the input validation a bit easier to check with fewer strings to check. Just a side-tip.
Hope this helps!
I have list and i want to replace all the elements os the list with another elements.
Code:
list1 = ['1','1','3','4','5','2','3','4']
dict1 = {'dict1' : ['1','2','3','4','5'] ,'name':['su5','pra4','sa3','ma2','sri1']}
for x in range(len(dict1['dict1'])):
list1 = [word.replace(dict1['dict1'][x],dict1['name'][x]) for word in list1]
print(list1)
Actual Output:
['susri1', 'susri1', 'sa3', 'ma2', 'sri1', 'prama2', 'sa3', 'ma2']
Expected Output:
['su5','su5','sa3','ma2','sri1','pra4','sa3','ma2']
That's a very strange dictionary if you transform the dictionary so you can use it as a direct mapping then this is a relatively easy thing to do, e.g.:
>>> dict2 = dict(zip(dict1['dict1'], dict1['name']))
>>> [dict2[i] for i in list1]
['su5', 'su5', 'sa3', 'ma2', 'sri1', 'pra4', 'sa3', 'ma2']