Append a tuple to a list - what's the difference between two ways?
The tuple
function takes only one argument which has to be an iterable
tuple([iterable])
Return a tuple whose items are the same and in the same order as iterable‘s items.
Try making 3,4
an iterable by either using [3,4]
(a list) or (3,4)
(a tuple)
For example
a_list.append(tuple((3, 4)))
will work
How to append a tuple to a numpy array without it being preformed element-wise?
I agree with @user2357112 comment:
appending to NumPy arrays is catastrophically slower than appending to ordinary lists. It's an operation that they are not at all designed for
Here's a little benchmark:
# measure execution time
import timeit
import numpy as np
def f1(num_iterations):
x = np.dtype((np.int32, (2, 1)))
for i in range(num_iterations):
x = np.append(x, (i, i))
def f2(num_iterations):
x = np.array([(0, 0)])
for i in range(num_iterations):
x = np.vstack((x, (i, i)))
def f3(num_iterations):
x = []
for i in range(num_iterations):
x.append((i, i))
x = np.array(x)
N = 50000
print timeit.timeit('f1(N)', setup='from __main__ import f1, N', number=1)
print timeit.timeit('f2(N)', setup='from __main__ import f2, N', number=1)
print timeit.timeit('f3(N)', setup='from __main__ import f3, N', number=1)
I wouldn't use neither np.append nor vstack, I'd just create my python array properly and then use it to construct the np.array
EDIT
Here's the benchmark output on my laptop:
- append: 12.4983000173
- vstack: 1.60663705793
- list: 0.0252208517006
[Finished in 14.3s]
append tuples to a list
result.extend(item)
Append tuples to a tuples
x + ((0,0),)
should give you
((1, 2), (3, 4), (5, 6), (8, 9), (0, 0))
Python has a wonky syntax for one-element tuples: (x,)
It obviously can't use just (x)
, since that's just x
in parentheses, thus the weird syntax. Using ((0, 0),)
, I concatenate your 4-tuple of pairs with a 1-tuple of pairs, rather than a 2-tuple of integers that you have in (0, 0)
.
Julia: Cannot append tuple to array
append!
adds all of the individual elements of another collection to the existing object. Julia raises the error here because (3, 3)
is a collection of two integers and it cannot reconcile an individual integer of type Int64
with the array's Tuple{Int64,Int64}
type.
The method you need is push!
, which will add one or more individual items to an existing collection:
julia> push!(a, (3, 3))
3-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(2, 2)
(3, 3)
The individual item, the tuple (3, 3)
, was successfully pushed onto the array a
.
To accomplish the same task with append!
, the tuple needs to be contained in a collection of some sort itself, such as an array:
julia> append!(a, [(4, 4)])
4-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(2, 2)
(3, 3)
(4, 4)
This is documented on the collections page here.
% Tuple in an array? python (not append tuple into array)
You need to give each string its own variables:
x=10
l=11
list = ["%s bla" % x,"%s bla" % l]
print(list)
Append Multi Dimensional Tuples in Python
What you've shown is a list of tuples. You can just use zip
for this:
In [11]: s1 = pd.Series([1,2,3], index=['a','b','c'])
In [12]: s2 = pd.Series([42, 88, 99], index=['a','b','c'])
In [13]: s1
Out[13]:
a 1
b 2
c 3
dtype: int64
In [14]: s2
Out[14]:
a 42
b 88
c 99
dtype: int64
In [15]: list(zip(s1, s2))
Out[15]: [(1, 42), (2, 88), (3, 99)]
appending tuple list from one to another
Your problem can be simplified as below:
counter = 0
list1 = []
rows = [(1,2),(3,4,),(5,6,),(7,8),(9,10)]
for row in rows:
counter = counter + 1 #after the first loop, counter is a tuple and cannot concatenate with an int
list1.append(row)
if counter % 4 == 0:
for counter in list1: #you redefined counter here which is a tuple
print(counter)
The simple solution is not to use the name counter
in your for
loop of list1:
for item in list1:
print(item)
Related Topics
For-In Loop and Type Casting Only for Objects Which Match Type
Swiftui Coordinator Not Updating the Containing View's Property
How to Convert Hexstring to Bytearray in Swift 3
Injecting a New Stylesheet into a Website via Uiwebview Using iOS8 Swift Xcode 6
Swift: Detecting an Unexpected Nil Value in a Non-Optional at Runtime: Casting as Optional Fails
How to Make an Enum Conform to a Protocol in Swift
How to Show a Window Without Stealing Focus on MACos
How to Find the Index of an Item in a Multidimensional Array Swiftily
Why Is the Swift Compiler Marking This as an Error
Detect Left and Right Click Events on Nsstatusitem (Swift)
Flip Arfaceanchor from Left-Handed to Right-Handed Coordinate System
How Are Hash Collisions Handled
In Swift,There's No Way to Get the Returned Function's Argument Names
Nothing Prints Out in the Console in Command Line Tool Xcode
Xcode & Swift - Window Without Title Bar But with Close, Minimize and Resize Buttons