How to insert the contents of one list into another - list

I am trying to combine the contents of two lists, in order to later perform processing on the entire data set. I initially looked at the built in insert function, but it inserts as a list, rather than the contents of the list.
I can slice and append the lists, but is there a cleaner / more Pythonic way of doing what I want than this:
array = ['the', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
addition = ['quick', 'brown']
array = array[:1] + addition + array[1:]

You can do the following using the slice syntax on the left hand side of an assignment:
>>> array = ['the', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> array[1:1] = ['quick', 'brown']
>>> array
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
That's about as Pythonic as it gets!

The extend method of list object does this, but at the end of the original list.
addition.extend(array)

insert(i,j), where i is the index and j is what you want to insert, does not add as a list. Instead it adds as a list item:
array = ['the', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
array.insert(1,'brown')
The new array would be:
array = ['the', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

Leveraging the splat operator / list unpacking for lists you can do it using
array = ['the', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
addition = ['quick', 'brown']
# like this
array2 = ['the', *addition, 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
# or like this
array = [ *array[:1], *addition, *array[1:]]
print(array)
print(array2)
to get
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
The operator got introduces with PEP 448: Additional Unpacking Generalizations.

Related

Transform list to the dictionary

I have a list with a strings
['scene-task-v001-user', 'scene-task-v002-user', 'scene-explo-v001-user', 'scene-train-v001-user', 'scene-train-v002-user']
strings created by regular expression
'(?P<scene>\w+)-(?P<task>\w+)-v(?P<ver>\d{3,})-(?P<user>\w+)'
I need to create dictionary where key its a task group and values contain all ver groups with the same task
{'task': ['001', '002'], 'explo': ['001'], 'train': ['001', '002']}
How to do it?
Thanks!
First of all, ('t-1', 't-2', 's-1', 'z-1', 'z-2') is a tuple, not a list. In addition, {'t': {'1', '2'}, 's': {'1'}, 'z': {'1', '2'}} is wrong expression, a form of the values would be a list here, not {}. I corrected this issue in my codes below.
Instead of using regular expression, you can loop the list and split by '-' inside the loop to get keys and values, as follows:
from collections import defaultdict
l = ('t-1', 't-2', 's-1', 'z-1', 'z-2')
d = defaultdict(list)
for item in l:
key, val = item.split('-')
d[key].append(val)
print(d) # defaultdict(<class 'list'>, {'t': ['1', '2'], 's': ['1'], 'z': ['1', '2']})
print(d['t']) # ['1', '2']
Using regular expressions to get keys and values for a dictionary:
from collections import defaultdict
import re
l = ('t-1', 't-2', 's-1', 'z-1', 'z-2')
d = defaultdict(list)
for item in l:
key_patten = re.compile('\w-')
val_patten = re.compile('-\w')
key = key_patten.search(item).group().replace('-', '')
val = val_patten.search(item).group().replace('-', '')
d[key].append(val)
print(d) # defaultdict(<class 'list'>, {'t': ['1', '2'], 's': ['1'], 'z': ['1', '2']})
print(d['t']) # ['1', '2']

How to get key for a value in a list

Suppose I had a dictionary like this:
d = {'Q1': ['10', '11', '12'], 'Q2': ['01', '02', '03'],
'Q3': ['04', '05', '06'], 'Q4': ['07', '08', '09'] }
And a variable: s = '04'
My goal is to find the key in d that corresponds to s in the list of values. So, I want to return 'Q3'.
for k, v in d.items():
for item in v:
if s == item:
print(d[v])
The above throws a TypeError: unhashable type: 'list. Without changing the structure of the dictionary, how can I accomplish my goal?
You can use in to check if an element is in a list, without an additional for on the list:
d = {'Q1': ['10', '11', '12'], 'Q2': ['01', '02', '03'],
'Q3': ['04', '05', '06'], 'Q4': ['07', '08', '09']}
s = '04'
for k, v in d.items():
if s in v:
print(k, d[k])
output:
Q3 ['04', '05', '06']
You are getting the TypeError because you are trying to use a list (v) as a dictionary key (d[v]), but since lists are unhashable, they can't be keys, instead use d[k] which is both correct and intuitive (since k is key).

store values from loop in a list of lists or a list of tuples

there!
I try to output all the possible part-of-speech(pos) of each word in the text. However, I need to print the output as "a list of lists" or "a list of tuples" for the further use.
Anyone can help, many thanks!
import nltk
from nltk.tokenize import word_tokenize
text = "I can answer those question ." # original text
tokenized_text = word_tokenize(text) # word tokenization
wsj = nltk.corpus.treebank.tagged_words()
cfd1 = nltk.ConditionalFreqDist(wsj) # find all possible pos of each word
i = 0
while i< len(tokenized_text):
pos_only = list(cfd1[tokenized_text[i]])
y = pos_only
print(y)
i+=1
my output is
['NNP', 'PRP']
['MD', 'NN']
['NN', 'VB']
['DT']
['NN', 'VBP', 'VB']
['.']
my expected output is
[['NNP', 'PRP'], ['MD', 'NN'], ['NN', 'VB'], ['DT'], ['NN', 'VBP', 'VB'], ['.']]
or
[('NNP', 'PRP'), ('MD', 'NN'), ('NN', 'VB'), ('DT'), ('NN', 'VBP', 'VB'), ('.')]
I think you will need to create an empty list and append elements during iteration. I assumed print(y) outputs ['NNP', 'PRP'] etc. Then you should convert y to a tuple and append it to the list during iteration. This piece of code should do it.
alist = []
i = 0
while i < len(tokenized_text):
pos_only = list(cfd1[tokenized_text[i]])
y = pos_only
alist.append(tuple(y))
i += 1
print(alist)

How do I append a dictionary item to a pre-existing dictionary in Python?

I have a .csv file containing tweets and their sentiment polarity. Using DictReader, I see each row in the file in the format:
Sentiment: '0', SentimentText: 'xyz'
Now I want to add each row of the file to a pre-existing dictionary such that the structure of the dictionary at the end is:
{{Sentiment: '0', SentimentText: 'xyz'},
{Sentiment: '1', SentimentText: 'abc'}...#so on until eof}
Is there any way that this is possible?
EDIT: So far, this is what I have achieved. This basically makes a list of dictionaries:
dataset = []
with open('SentimentAnalysisDataset.csv') as csvfile:
reader = csv.DictReader(csvfile)
count = 1
for row in reader:
data = [{'Text': row['SentimentText'], 'Polarity': row['Sentiment']}]
tuple = {str(count): data}
count = count + 1
dataset.append(tuple)
This:
{{Sentiment: '0', SentimentText: 'xyz'},
{Sentiment: '1', SentimentText: 'abc'}...#so on until eof}
Is not a valid dictionary structure. If you wish to use a list, this will work:
[{Sentiment: '0', SentimentText: 'xyz'},
{Sentiment: '1', SentimentText: 'abc'}...#so on until eof]
Otherwise, you're probably looking for a structure like this:
{'0': 'xyz',
'1': 'abc',
...}
In order to do that, you should update the existing dictionary like so:
existing_dict = {'0': 'xyz',
'1': 'abc'}
existing_dict[row['Sentiment']] = row['SentimentText']
# OR
new_dict = {row['Sentiment']: row['SentimentText'],
... # For all sentiments in file
}
existing_dict.update(new_dict)

I have a list of tuples, how can I take out a specific element?

I have a list of tuples like:
>>>list
[('the', 248),
('I', 81),
...
('I', 81)]
I want to take out a specific element like ('to',248), how should I index the element and get it?
>>> l =[('the', 248), ('I', 81), ('I', 81)]
>>> x = [i[1] for i in l]
>>> x
[248, 81, 81]
I'm not sure what you exactly mean by this question, but if you want to take an element out with specific qualities, you would just use index() (no matter the data type):
# x = [("string0",0),("string1",1),("string2",2),("string3",3)]
# Example outputs:
>>> x.index(("string0",0))
0
>>> x.index(("string2",2))
2