We’re rewarding the question askers & reputations are being recalculated! Read more.

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0
votes
3answers
40 views

Python - Insert an element to a list only if some specific element exists in the list

I have a list of strings 'a' and I want to compare my list to a specific string 'b' and if that string is found in some of the elements of the list, I want to insert another element in the list 'a' ...
0
votes
0answers
46 views

How do i remove a lists last index depending on chosen input in python 3x?

Lets say i have two list which is set up like this: deckList = [deckOne, deckTwo, DeckThree, DeckFour, deckFive, deckSix, deckSeven, deckEight] deckNames = ['A','B','C','D','E','F','G','H'] The "...
-1
votes
4answers
41 views

How to call a dict value with the value given as an input variable in python

most_used_languages = { 1: "Mandarin Chinese", 2: "English", 3: "Hindustani", 4: "Spanish", 5: "Arabic", 6: "Malay", 7: "Russian", 8: "Bengali", 9: "Portuguese", ...
0
votes
1answer
45 views

Find all combinations to split a single list into two lists

I have a list which contains few number of elements and I'd like to find all possibilities to split this list into two list. I mean, all combinations mean I wouldn't care about its element order. i.e....
1
vote
1answer
25 views

keyboard.record() append to list

I'm trying to record all key-presses until i press space and then comparing the key-presses to a list. I cant seem to find a way to save the recorded keys in a list. And yes I need to use space key ...
-1
votes
2answers
25 views

Python Even/odd values in a list [on hold]

I'm trying to write a program in python that looks at the user input numbers in a list and if they are all even it will print "all even" or "all odd" if they are all odd numbers, if it's a mix of even ...
-1
votes
0answers
17 views

creating a list for every different variable in a list [duplicate]

Say if I had a list like the following ['a', 'b', 'a', 'c', 'd', 'b']. The following is what I have tried to make a list for each item. list_of_items = ['a', 'b', 'a', 'c', 'd', 'b'] for item in ...
0
votes
0answers
21 views

For x in Arra give me None at the end of Array

I am trying to make a Blackjack game to practice OOP in Python. And I have this clases: class Deck: suits = ('Diamonds', 'Hearts', 'Spades', 'Clubs') ranks = ('Two', 'Three', 'Four', 'Five', '...
-2
votes
1answer
29 views

Seperating the numbers from strings to do the maths and return the string with the results [on hold]

I have some files like this. They are some receipts of restaurants. --------------------------- CompanyID: 000000000000 ProductName: quantity costPerPizza sumCost --------------------------- For ...
-2
votes
2answers
23 views

Compare items of two tuples in different List python

Hello freinds i need your help. I want to compare between two lists of tuples ,if there is more than one identical value between the two tuples I print this result exp : L1 = [('G', 'T'), ('T', 'T')...
0
votes
1answer
18 views

How to overwrite a nested list using conditional list comprehension

I'm trying to check a nested for the bigger number in each and overwrite each nested list with just the value of the number with the biggest value. I have done this using nested loops but I was ...
0
votes
2answers
24 views

sorting a tuple that contains string and numbers in python

I have a list like this; List = [('apple 5', 12), ('apple 3', 2), ('apple 6', 10),('apple 4', 15), ('apple 9', 11), ('apple 7', 14), ('apple 18', 10), ('apple 16', 10),('orange 5', 4), ('orange 4', 7)...
0
votes
2answers
36 views

Split a text file in Python by number of lines

I have a txt file in below format and i want to split this file in such a way that it split the file after 4th line in one list list. Input file: 1 A 2 B 3 C 4 D 5 A 6 B 7 C 8 D 9 A 10 B 11 C 12 D ...
-1
votes
2answers
24 views

Counting the number of elements in a list that contain more 0's than 1's

i'm new to python programming and my task is to tell how many binary values of the list there are where the number of 0's is grater than 1. The data for this task is in a text file, I've opened the ...
0
votes
2answers
30 views

keep track of increment and decrement of values in python tuple

I have a list like below, list_ = [('a', 5), ('a', 6), ('a', 8), ('a', 2), ('a', 5), ('a', 9), ('a', 10), ('b', 3), ('b', 7), ('b', 6), ('b', 2), ('b', 6), ('b', 5), ('b', 10)] I ...
0
votes
1answer
35 views

Search a list of list of strings inside a list of strings in Python

I want to search a list of list of strings inside another list of strings in Python. If match is found, I want to retrieve the matched string of both lists. I want to get partial matches also. Both ...
-11
votes
0answers
44 views

python slicing and average the value in the list [on hold]

i want to calculate the average of the cost of the following list grouped by the the userID. a = ["BC1001:643", "BC1002:789", "BC1003:890", "BC1001:544", "BC1001:321", "BC1002:792"] first value is ...
-3
votes
0answers
29 views

How can we assign or append value in 2d list? [on hold]

I am trying to assign values in 2d list. How do I assign or append the value? I am getting this error: KeyError Traceback (most recent call last) in 33 ui.append(uo) 34 ---> 35 fs[0][0]=0 36 kt[0]...
0
votes
0answers
35 views

Is there any case where appending to a list is a better approach than filling up a list?

(Assuming you know the size of the list beforehand) Comparing these two functions: def foo_1(size=1000): res = [] for i in range(size): res.append(i) def foo_2(size=1000): res = ...
2
votes
3answers
38 views

How can I add a string to every value of a list, and then make a value of that string count up?

I am trying to print values in a list and add strings. I want a value (colx in the string I'm adding) to count up from 0 to the last value of the list. #CODE list1 = ['dog' , 'cat', 'pig', 'cow'] ...
0
votes
1answer
16 views

how to iterate over two lists of different length in python without combining them

Is there away to iterate over two lists of different length at the same time without combining them? I tried itertools.product ( list1, list2) but that's very similar to a nested loop. So the second ...
-1
votes
1answer
20 views

How can i get 3 random numbers as output from a list in python?

Example i have a 10 items list like below, test_list = [1, 4, 5, 2, 7, 3, 6, 8, 9, 10] so my question is how can i get 3 random numbers as output, is there any way to get it?
0
votes
4answers
26 views

subtracting two values inside a list of list

my_list = [['chr1', 65419, 65433], ['chr1', 65520, 65573], ['chr1', 69037, 71585], ['chr1', 69055, 70108], ['chr1', 137621, 139379],['chr2', 65419, 65433], ['chr2', 65520, 65573], ['chr2', 69037, ...
0
votes
3answers
33 views

How to create a list within the for-loop such that all three of my hobbies are within it?

I want to create a for-loop that prompts the user to enter their hobby three times and appends the hobby to a hobbies list. At the end, the hobbies list should contain all three hobbies! This is ...
-2
votes
0answers
20 views

Find index of first occurence of repetative number [duplicate]

Input: a=[0,0,0,0,0,1,2,3,4,0,1,2,0,0,0,0,0,1,0,0] Output: index 0,9,12,18 Input is list of numbers and output is index of first occurence of '0'
-1
votes
2answers
43 views

Matching with “if” condition for the fields, anywhere in list

I have a list of strings that contain comma separated items. data = [ 'dog, cat, mouse, horse', 'apple, mango, orange, banana' ] for item in data: r = item.split(', ') if r[0] =...
0
votes
2answers
41 views

Index of first occurence of zero

I have a list like this, import numpy as np myList = [0.0 , 0.0, 0.0, 2.0, 2.0, 0.0, 2.5, 0.0, 0.0, 0.0, 3.0, 0.0] I can find the index of non zero occurrence like below. I = np.nonzero(myList) ...
-1
votes
0answers
11 views

compare multiple list elements and get equal elements [on hold]

I want to compare multiple list elements and import the same elements into a new list. My problem here is the number of lists (hundreds). Also, when I get the result as a set, it doesn't sound ...
-2
votes
1answer
22 views

Python errors: TypeError: list indices must be integers or slices, not str,

I wrote down the code like this: squares = ['orange', 'orange', 'purple', 'blue ', 'orange'] new_squares = [] i = squares[0] while i=='orange': new_squares.append(squares[i]) i +=1 print(...
-5
votes
0answers
16 views

how to make function for count from the biggest to the smallest contents tuple in list with python

[I have tried to count the contents of the tuple in the list but have not managed to rank from the largest to success c_data is a list of names that contain .csv files]1
0
votes
4answers
47 views

Counting the elements inside a list of list

I have a list of lists as follows [ ['DDX11L1', 'lincRNA', 'chr1', 11869, 14409], ['WASH7P', 'lincRNA', 'chr1', 14404, 29570], ['MIR1302-2HG', 'lincRNA', 'chr1', 29554, 31109], ['FAM138A', '...
0
votes
1answer
16 views

Getting the latest value of duplicate keys in list of dictionaries

I have a list of dictionaries like this UNFILTERED LIST OF DICTIONARIES: [{'team': 'green', 'user': 'bob'}, {'play': '${bla/bla}', 'mentor': 'alice', 'team': 'blue'}, {'team': 'yellow'}] i need to ...
0
votes
0answers
9 views

why do I get list assignment index out of range?

My code for transpose matrix is down below and I am getting the error at line 9, result[i][j]=A[j][i], why am I getting this error, instead of assigning, it works well if I just append, but I don't ...
1
vote
3answers
33 views

Python: filter lines based on field match from another file

I have generated a list with my other python code, which looks like this. there are lines separated by commas and they are in single quotes. I am trying hard to filter the lines based on D: column ...
0
votes
4answers
35 views

Search keys in the dictionary and return both key and value [on hold]

I have this dictionary like {'Mike':'98','Tom':'57'}. How could I do search based on names which is the key in this case and return both key and value? I have roughly heard about create a turple of ...
0
votes
2answers
28 views

how to multiply nested list with list?

i have: dataA=[[1,2,3],[1,2,5]] dataB=[1,2] I want to multiply index [0] dataA with index [0] dataB, and index [1] dataA with index [1] dataB, how to do it. I tried it, but the results didn't match ...
0
votes
2answers
43 views

Python. The way how to find out the element's order based on selected variable in list

I would find out the order of element in the list, which is selected based on any variable. I have variables: first "users", where are names of poeple and second "values", where are any values of the ...
-2
votes
1answer
16 views

how do you reverse an element in a list in python

Like if you have a list as names = ["Alice", "Opoku", "Kti", "Aben"] and you want to reverse a particular entry in the list like Alice as ecilA. How do you do that?
0
votes
0answers
30 views

Compare and group adjacent lists in a list (only python3 ) (only numpy) [on hold]

I wish to know how I can compare a list of different lists using a particular item in the list and grouping them l = [ [ {"b": (100,20,839, 90)}, {'a': "hello"}], [ {"b": (10,20,89, 93)}, {'a': "loco"...
-1
votes
0answers
86 views

argument of type 'int' is not iterable python

I am writing a one level branch predictor, my pht is a multidimensional list and its size is 1024 and I wanted to check the whole list of pht index[0] with a integer value. for that I have written a ...
-4
votes
1answer
53 views

What placeholder is used for list in python?

Is there a format placeholder for a list in Python, e.g., in the same way that "%s" is a format placeholder for a string, and "%d" is a placeholder for an integer? I have a list: result_list = [{"a":...
0
votes
1answer
39 views

Python3 dict contains partial string? [duplicate]

The below function is an IP scanner and returns a list of IP addresses and MAC addresses. I am trying to figure out how to filter out only MAC addresses that contains a specific vendor. For example, ...
0
votes
2answers
35 views

Convert list of 2D lists to Dictionary

I am trying to convert a list of lists to a dictionary. Initial list of the form: test = [['A', 27], ['B', 33], ['C', 42], ['D']] Final dictionary should be of the form: dictionary = {'A': 27, 'B':...
0
votes
1answer
40 views

Python sorting arrays by element contained?

I'm looking to take a list of lists and sort them by an element contained in the contained lists. For clarification, I want to have a list like this code myList = [] myList.append([1, 2, 3]) myList....
2
votes
2answers
31 views

Convert all keys inside [dict] to dict in python

Any elegant way to convert all keys inside dict list to dict? Like this: aa = [ {"option": "-c","params":4}, {"option": "-t", "params": 8}, {"option": "name", "params": "bb"} ] bb = ...
2
votes
2answers
68 views

Why this parameter list doesn't change in Python?

I created a function f which uses a 2-dimension list as parameter, but after this function the list does not change at all. As the code below: def f(t: [[int]]): for eachrow in t: eachrow ...
0
votes
1answer
39 views

Is there a subtle difference between some_list[:] = another_list vs some_list = another_list?

Like the title asks, Is there a subtle difference between some_list[:] = another_list vs some_list = another_list I seen people do both and just ignored it, thinking of them as equivalents, but ...
-1
votes
3answers
48 views

Relplace str in list with n many strings starting from 0

Given a list of lists and an integer, I need to replace every string with n many string. Where n = integer given. Ex.: irv_to_stv_ballot([['NDP', 'CPC'], ['GREEN']], 3) [['NDP0', 'NDP1', 'NDP2', '...
0
votes
0answers
27 views

Unexpected list.append() behavior when using 'sub-lists' in Python [duplicate]

I've got a problem with a project I'm working on. I've managed to discover that the problem is actually with how I thought list appending works in Python. I've recreated the problem I'm having in my ...
0
votes
0answers
17 views

Iterate through JSON object returned as a list with nested dictionary?

I have very limited experience with JSON, but I have a working function that returns a JSON list with three values, the last value being a nested dictionary. I would like to manipulate the list by ...