Creating set from nested list in python [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
marksheet = [['harry',87], ['bob', 76], ['bucky', 98]]
print(set([marks for name, marks in marksheet]))
output: {98, 76, 87}
Can someone please explain how this works?

You're iterating name, marks over marksheet. So you're extracting two values and storing them as name, which you ignore, and marks, which you create a list from. That list you just create is passed to set, which creates a set. You can break the code down step by step:
marksheet = [['harry',87], ['bob', 76], ['bucky', 98]]
In [40]: marksheet
Out[40]: [['harry', 87], ['bob', 76], ['bucky', 98]]
In [41]: l = [marks for name, marks in marksheet]
In [42]: l
Out[42]: [87, 76, 98]
You can also surround the values you're extracting in parentheses to help make it more clear:
In [43]: l = [marks for (name, marks) in marksheet]
In [44]: l
Out[44]: [87, 76, 98]
Some people use _ to denote the returned value is ignored:
In [45]: l = [marks for (_, marks) in marksheet]
In [46]: l
Out[46]: [87, 76, 98]
The above is an example of list comprehension. This is equivalent to:
In [47]: l=[]
In [48]: for (name, marks) in marksheet:
...: l.append(marks)
...:
In [49]: l
Out[49]: [87, 76, 98]
From there you are simply passing the list to set, which can take an iterable. In this case, the list you just created is the iterable:
In [50]: set(l)
Out[50]: {76, 87, 98}

Related

I am trying to extract nested list. Am I coding wrong?

I am trying to extract nested list. Am I coding wrong?
l2=[3,4,5,6,7, [23,456,67,8,78,78], [345,56,87,8,98,9], (234,6657,6), {'key1': "sudh",234:[23,45,656]}]
for i in l2:
if i==list:
break
print (l2[i])
--------------------Out Put-----------------------
{'key1': "sudh",234:[23,45,656]}
Please add expected output in your question
As mentioned in question you need to check type and not it's value you can do as mentioned below:
l2=[3,4,5,6,7, [23,456,67,8,78,78], [345,56,87,8,98,9], (234,6657,6), {'key1': "sudh",234:[23,45,656]}]
for i in l2:
if type(i)==list:
print(i)
# OR
for i in l2:
if isinstance(i,list):
print(i)
Output:
[23, 456, 67, 8, 78, 78]
[345, 56, 87, 8, 98, 9]
PS: for i in l2 is iterating over it's element and it's not some index so you cannot use it as index in l2[i] as i is the element and not index

python3 list element mapping to sum element [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a list 'L'
L = [('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]
I want this list to
[ 3, 4, 5, 6, 5, 6, 7, 7, 8, 9]
which is sum of element.
What is appropriate code? I want shortest code
Use map method?
Seems like homework, but OK.
We need to transform the strings inside the tuples into ints, and then sum them, there are a trillion ways of doing this. This is a general solution so it works even if the tuples in the original list have variable lengths.
First using map as you want
list(map(lambda tup: sum(map(int,tup)), L))
The list call is just used to create a list from the map object.
You can also use list comprehensions
[sum(int(x) for x in tup) for tup in L]
You can also mix and match the map in the comprehension call like this to get the shortest code.
[sum(map(int,tup)) for tup in L]
Here you go. :)
casting = [ (int(i), int(j)) for i,j in L ]
sumVariable = [ sum(i) for i in casting ]
you can try this :
alpha = [int(x[0])+int(x[1]) for x in L]

Count list elements without duplicates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Python and finding my way.
I have this code here:
L = ['l','u','k','e','l','i']
k = [L.count(c) for c in L]
print(k)
And it gives this result:
[2, 1, 1, 1, 2, 1]
which is great but I want to get to this result
[2, 1, 1, 1, 1]
That is, a result in which the 'l' is not counted twice.
How do I do that?
NB: the list L here is from user input, I convert user input to list L so I can't tell beforehand what the user will input but once its typed in, I want to convert the string into a list and count all the elements without counting an element twice.
you can do something like that
L = ['l','u','k','e','l','i']
import collections
s = collections.Counter(L)
s
Counter({'l': 2, 'u': 1, 'k': 1, 'e': 1, 'i': 1})
s.values()
dict_values([2, 1, 1, 1, 1])
Use a dictionary:
L = ['l','u','k','e','l','i']
counts = {}
for l in L:
counts[l] = counts.get(l, 0) + 1
print(counts)
# {'k': 1, 'l': 2, 'e': 1, 'u': 1, 'i': 1}

How to get all ordered sublists from a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list: [1,2,3,4]. It could be much longer. There will be many more combinations.
The values are less important. 1,2,3,4 is more about the sequence or index of the elements (I did not use 0 for the first element).
For now, I want to have:
4-elements: [1,2,3,4]
3-elements: [1,2,3], [1,2,4], [1,3,4], [2,3,4]
or more generally, it will be nice I can specify the number of elements of the sublists.
I'm thinking using loops, pick up each element in order. To get four-elements, will take four loops. As the number of desired elements increases, it will become unmanageable. Just wondering if there is an easier solution.
Thank you!
you could use a dict with itertools.combinations:
from itertools import combinations
l = [1, 2, 3, 4]
{i: list(combinations(l, i)) for i in range(4, 2, -1)}
output:
{4: [(1, 2, 3, 4)], 3: [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]}
each key form the dict represents the number of elements from a combination

How to select an element from a list based on a function argument? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If I have a list of typles as the following:
List_of_tuples[(a, 100), (b,90), (c, 80), (d, 70), (e, 50)]
I want to write a function where I can pass a number as an argument n=2, for example, and it gets the 2 most frequent elements in the list above. If I pass n=3, this means it will get the 3 most frequent elements and so on.
This is exactly what the most_common method of collections.Counter does:
>>> list_of_tuples = [('a', 100), ('b', 90), ('c', 80), ('d', 70), ('e', 50)]
>>> from collections import Counter
>>> counter = Counter(dict(list_of_tuples))
>>> counter.most_common(2)
[('a', 100), ('b', 90)]
The only non-obvious part about this code is that we need to construct the Counter by constructing a dict from the pairs first, otherwise Counter will actually count the tuples themselves.

Resources