Python - why is this wrong? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This don't works and I wish to know what I did wrong.
targets= ["a","b","c"]
tmplist= ["d","z","x"]
value = [ (x,y) for x in targets for y in tmplist]
I know this issue can be solved with zip function, but I want to do it without zip. Thanks for any help
EDIT: I'm very sorry for not being clear, I have been distracted.

Luckily my crystal ball is working today so I can guess what you mean when you say it isn't working. Of course, you might have made it easier by actually explaining, but there we go.
If you just want a list of (x, y) pairs then zip is the way to go. The syntax you have does something else: for each element in targets it iterates completely through all elements in tmplist. This is exactly equivalent to:
for x in targets:
for y in tmplist:
value.append((x, y))
So for a pair of lists ['a', 'b', 'c'] and [1, 2, 3] you would get:
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

Related

Python: Sort list consisting of (int, string)-pairs descending by int and ascending by string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How do I sort a list consisting of (int, string)-pairs descending by int and ascending by string?
You can use sorted with a custom key argument. In this case negating the int will cause a descending behavior, then leave the str which will cause ascending behavior. Putting them in a tuple will allow lexicographical sort behavior if the first key (int) is identical.
>>> data = [(1, 'hello'), (7, 'bar'), (4, 'foo'), (4, 'world')]
>>> sorted(data, key=lambda i: (-i[0], i[1]))
[(7, 'bar'), (4, 'foo'), (4, 'world'), (1, 'hello')]

Zip unequal lists, pad beginning of the shorter 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 5 years ago.
Improve this question
There are lots of questions/answers I have found on how to zip unequal lists. However, in all cases the resulting set fills the back side of the shorter list with None. I would like to prepend the shorter list.
list1 = [a, b, c, d, e] (pretend these are numbers)
list2 = [3, 4, 5]
fun(list1, list2) => [(a, None), (b, None), (c, 3), (d, 4), (e, 5)]
Bonus:
fun(list1, list2) => [(a, 0), (b, 0), (c, 3), (d, 4), (e, 5)]
from itertools import zip_longest
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = [3, 4, 5]
result = [tup for tup in zip_longest(list1[::-1], list2[::-1], fillvalue=0)][::-1]
print(result)
# [('a', 0), ('b', 0), ('c', 3), ('d', 4), ('e', 5)]
def left_pad_list(a_list,n,fillval=None):
return [fillval]*(n-len(a_list)) + list(a_list)
I guess ? (then its a matter of determining which is the longer/shorter)
my_lists = [list1,list2,list3]
max_len = max(my_lists,key=len)
my_new_lists = [left_pad_list(x,max_len,0) for x in my_lists]

Container to Use for Sorting in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The task is to create a spam filter using machine learning. In order to do feature selection I have implemented a method that calculates MI for each word but then I want to return N words that have a high MI and choose between them based on how many times they appear in the spam email.
The reason for the additional requirement is that we are using the small lingspam set and there is little difference between the results and there are about 3000 words that share the same top MI value.
We are required to do this in Python and I have currently implemented this using dictionaries but I can't find a container type that would let me do what I need.
You can sort the items of a dictionary (you'll have to use a custom key), where the items are stored as a list.
>>> some_dictionary = {"a": 1, "b": 5, "c": 0, "e": 2}
>>> sorted(some_dictionary.items())
[('a', 1), ('b', 5), ('c', 0), ('e', 2)]
>>> sorted(some_dictionary.items(), key=lambda i:i[1])
[('c', 0), ('a', 1), ('e', 2), ('b', 5)]
>>>
Where .items() lets you get the items in the dictionary (in an arbitrary order):
>>> some_dictionary.items()
dict_items([('a', 1), ('b', 5), ('e', 2), ('c', 0)])
Note that dict_items is an iterable, which just wraps a list in this case.

Aligning two lists with duplicate keys [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This question is similar to "Aligning to Lists in python" question, but I have a problem with using a dictionary because of repeated numbers for potential keys.
Here is an example. Start with these 2 lists:
If I used a dictionary these would be the keys. [5,6,6,1,6,1,6,1,1,2,1,2,1,2,2,1]
[13,14,15,10,16,11,17,12,12,13,13,14,14,15,16,17]
I am able to rearrange the first list the way I want it, which is:
[5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
I want the second list to keep it's same alignment, to the first list and look exactly like this:
[13,14,15,16,17,10,11,12,12,13,14,13,14,15,16,17]
Notice that it matters that the list of potential keys has it's repeated values aligned by position with the corresponding values in the second list.
Like other people below your post, I don't completely understand your problem (could you be more specific about relation you want to obtain?), but maybe zip is the answer for your question:
>>> a = [5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
>>> b = [13,14,15,16,17,10,11,12,12,13,14,13,14,15,16,17]
>>> alignment = zip(a, b)
>>> alignment
[(5, 13), (6, 14), (6, 15), (6, 16), (6, 17), (1, 10), (1, 11), (1, 12), (1, 12), (1, 13), (1, 14), (2, 13), (2, 14), (2, 15), (2, 16), (1, 17)]
Edited:
key_list = [5,6,6,1,6,1,6,1,1,2,1,2,1,2,2,1]
values_list = [13,14,15,10,16,11,17,12,12,13,13,14,14,15,16,17]
zipped_lists = zip(key_list, values_list)
sorted_zip = sorted(zipped_lists)
pattern = [5,6,6,6,6,1,1,1,1,1,1,2,2,2,2,1]
temp_dict = {}
for key, value in sorted_zip:
if key not in temp_dict:
temp_dict[key] = [value]
else:
temp_dict[key].append(value)
final_list = []
for i in pattern:
final_list.append((i, temp_dict[i].pop(0)))
And, of course, final_list is your result.

how to pair values from python list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
list = [1,2,3,4]
I would like to get a result of below and store them in csv file. ( 6 rows total )
1,2
1,3
1,4
2,3
2,4
3,4
Is there a function to this or how can I accomplish and store this in csv file ?
itertools is your friend...
http://docs.python.org/2/library/itertools.html
>>> import itertools
>>> x = [1, 2, 3, 4]
>>> list(itertools.combinations(x, 2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Use itertools.combinations. It's builtin in Python 2.6+.
import itertools
pairs = itertools.combinations(list, 2)
One option is to use itertools.permutations and a list comprehension:
>>> [(x, y) for x, y in itertools.permutations(mylist, 2) if x < y]
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
The condition x < y ensures you only get the permutations where x is lower than y.
The better option is to use itertools.combinations(mylist, 2).
This is simple enough to do it yourself:
l=[1,2,3,4]
for i in range(0,len(l)):
for j in range (i+1,len(l)):
print l[i],l[j]
But solutions using itertools can be generalized much easier.

Categories

Resources