Picking value from a list to create another [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 2 years ago.
Improve this question
I am trying to write a code that picks values in from a list to create to separate lists.
This is my code so far;
list = [6,2,9,10]
for x, y in zip(list, list):
print (x,y)
Output:
6,2
9,10
But what I want is:
[6,9]
[2,10]

You can zip slices of your original list to achieve your goal:
list = [6,2,9,10]
for x, y in zip(list[:2], list[2:]):
print (x,y)
6 9
2 10
This is not well-generalized - if you had a specific intent like "split my list into a first-half and second-half, and match the elements of those sub-lists" that would be more clear, but hopefully this helps you achieve what you want.

Assuming you want the elements at even index and odd index split into separate lists, you could do the following.
List1 =[1, 2,3,4,5]
even_list= List1[::2]
Odd_list = List1[1::2]

Related

Generate a list from list of dicts if value not exists in another list [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 2 years ago.
Improve this question
im triying to filter a list of dicts using the elements of a list
a=[{"item_id": "ITEM2090", "seller_id":1009954},
{"item_id": "ITEM2050", "seller_id":1009920},
{"item_id": "ITEM2032", "seller_id":1009960},
{"item_id": "ITEM2080", "seller_id":1009954}]
b=["ITEM2032","ITEM2060","ITEM2070","ITEM2090"]
expected result (the two dict from list a wich values for item_id not exists in list b):
c=[{"item_id": "ITEM2050", "seller_id":1009920},
{"item_id": "ITEM2080", "seller_id":1009954}]
I've tried:
c=[x["item_id"] for x in a if x["item_id"] not in b]
My problem is that it returns a list of the item_id values, not a list of dicts as I would like.
c = [item for item in a if item["item_id"] not in b]
Will be better to make "b" as a set in case of a large amount of items.

How to use strings as an ordering sequence? [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
I'd like to know how I can use a string like order = '8927391' (with 8 being largest and 1 being smallest) and print out an answer according to the order, for example:
Let's say if_no_is_higher(no1, no2) is a function that I have defined already to print yes if the first number is higher than the second, and no if the first number is smaller.
if_no_is_higher(8, 9)
returns
yes
because according to the order 8 is higher than 9
Create a custom order and use the index of that collection to check for your condition:
a = '8927391'
custom_order = [int(item) for item in a]
min(8, 9, key=custom_order.index)

How to calculate how many elements which contain a certain string in 2D array? [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
I have a 2D array like this:
[[aaa-1-2,
aaa-3-4,
bbb-y-t,
aaa-5-t],
[bbb-1-2,
dfs-3-4,
bbb-a-9,
yui-5-t]]
I want to write a function or something to get the sum of the elements which contain 'aaa' (in [aaa-1-2, aaa-3-4, bbb-y-t, aaa-5-t]), and the sum of 'bbb' in [bbb-1-2, dfs-3-4, bbb-a-9, yui-5-t].
In this example, the sum should be 3+2=5, I only know how to calculate the sum of the exact same elements between two arrays, but not sure how to solve this, can someone help me with this? thanks.
For a general solution where you have n prefixes corresponding to n sublists, you can use enumerate to figure out which sublist you are in and lookup the appropriate prefix to use, then sum the tests:
l = [["aaa-1-2", "aaa-3-4","bbb-y-t","aaa-5-t"],
["bbb-1-2", "dfs-3-4", "bbb-a-9","yui-5-t"]]
prefix = ['aaa', 'bbb']
sum(prefix[i] in s for i, sub in enumerate(l) for s in sub)
# 5
If you have more sublists and more stings you can just add to the prefix list.

Store a list of list on each node of a list [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 4 years ago.
Improve this question
I wanted to know that is it possible to create a list which can store a list of list on each node.
Basically if there is a list X = [A,B,C,D]
Is it possible that each of A, B , C and D be list of lists of themselves?
Can anyone demonstrate how this can be done?
Yes, lists can hold other lists. You can use the append method of list to add other lists to any list. For example:
a_list = []
a_list.append([1,2])
a_list.append([3,4])
print(a_list)
which gives us
[[1, 2], [3, 4]]

Python: IndexError: list index out of range and Class objects [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 2 years ago.
Improve this question
I hope to 2-D list. And each element in the list is set to unique class object. But I get the error(please see the screenshot). How can I get it done? Thank you!
Here before you assigning values to 2-d array Vertion you have to create an empty list with the exact number of rows and columns you want. Then only you can assign values to each each columns in each rows. Here you can take a look :
Vertion = [[0 for x in range(128)] for y in range(2)]
Update :
So for assign values to columns in each rows you can try like this :
for i in range(2):
for j in range(128):
Vertion[i][j] = Block()
Vertion = [[], []]
as Vertion is a list with two empty list, so the for loop should be like this
for i in range(len(Vertion)):
for j in range(128):
Vertion[i].append(Block())

Categories

Resources